Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the type hint of variables bound inside (e.g., ax in Softmax) #481

Open
ordabayevy opened this issue Mar 6, 2021 · 1 comment
Open
Labels
question Further information is requested

Comments

@ordabayevy
Copy link
Member

What should be the type hint for ax in this Softmax function?

  1. Funsor works but then then ax cannot be a str
  2. Value[str] can only be a str and not Variable
  3. Bound works both as str and Variable but ax is not actually bound
@make_funsor
def Softmax(
    x: Funsor,
    ax: ? # Funsor or Bound or Value[str]
) -> Fresh[lambda x: x]:
    return x.exp() / x.exp().reduce(ops.add, ax)
@fritzo
Copy link
Member

fritzo commented Mar 6, 2021

You'll need ax: Bound and ax2: Fresh[lambda ax: ax]. As we discussed yesterday, @make_funsor currently does not support binding-and-return of a dimension with the same name, so for now you'll need to rename the dimension. I believe @eb8680 is planning to address that as part of alpha-renaming work.

@make_funsor
def Softmax(
    x: Funsor,
    ax: Bound,
    ax2: Fresh[lambda ax: ax],
) -> Fresh[lambda x: x]:
    x = x(**{ax: ax2})  # rename to work around alpha renaming limitations
    y = x - x.reduce(ops.logaddexp, ax2)
    return y.exp()

@fritzo fritzo added the question Further information is requested label Mar 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants