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

Add ScaleTensor #96

Open
wants to merge 10 commits into
base: feature/improve_loss_functions
Choose a base branch
from

Conversation

HCookie
Copy link
Member

@HCookie HCookie commented Oct 18, 2024

ScaleTensor

  • Allows dynamic setup of scalars
  • Each scalar can be assigned to operate on a dimension or set of dimensions
  • Scalar is then resolved at scale time, allowing relative indices
    Examples
    --------
    >>> tensor = torch.randn(3, 4, 5)
    >>> scalars = ScaleTensor((0, torch.randn(3)), (1, torch.randn(4)))
    >>> scaled_tensor = scalars.scale(tensor)
    >>> scalars.get_scalar(tensor.ndim).shape
    torch.Size([3, 4, 1])
    >>> scalars.add_scalar(-1, torch.randn(5))
    >>> scalars.get_scalar(tensor.ndim).shape
    torch.Size([3, 4, 5])

HCookie and others added 3 commits October 18, 2024 15:28
- Allows dynamic setup of scalars
- Rework variable_scaler to use it
- Consolidate usage of name
- Add subset_by_dim
- Add resolve
- Resolve ruff issues
- Fix docs
- Provide add_scalar in BaseWeighedLoss
@HCookie HCookie marked this pull request as ready for review October 18, 2024 18:25
- Add scalar tests
- Allow a user to set `add_scalar_{key}`
- scale_by_variable_scaling -> scale
@HCookie HCookie self-assigned this Oct 21, 2024
Comment on lines +93 to +100
if len(self.scalar) == 0:
return x

if feature_indices is None:
return x * self.variable_scaling
return x * self.variable_scaling[..., feature_indices]
scalar = self.scalar.get_scalar(x.ndim)

if feature_indices is None or "variable_scaling" not in self.scalar:
return x * scalar
return x * scalar[..., feature_indices]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the piece of code be in the ScaleTensor class ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdym? The scalar indexing? I would say it depends on the feature_indices, so it belongs in the loss function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants