utils.scaler
utils.scaler
Classes
| Name | Description |
|---|---|
| TorchStandardScaler | A class for scaling data using standardization with torch tensors. |
TorchStandardScaler
utils.scaler.TorchStandardScaler()A class for scaling data using standardization with torch tensors. This scaler computes the mean and standard deviation on a dataset so that it can later be used to scale the data using the computed mean and standard deviation.
Attributes
| Name | Type | Description |
|---|---|---|
| mean | torch.Tensor |
The mean value computed over the fitted data. |
| std | torch.Tensor |
The standard deviation computed over the fitted data. |
Examples
>>> import torch
>>> from spotoptim.utils.scaler import TorchStandardScaler
>>> # Create a sample tensor
>>> tensor = torch.rand((10, 3)) # Random tensor with shape (10, 3)
>>> scaler = TorchStandardScaler()
>>> # Fit the scaler to the data
>>> scaler.fit(tensor)
>>> # Transform the data using the fitted scaler
>>> transformed_tensor = scaler.transform(tensor)
>>> print(transformed_tensor.shape)
torch.Size([10, 3])
>>> # Using fit_transform method to fit and transform in one step
>>> another_tensor = torch.rand((10, 3))
>>> scaled_tensor = scaler.fit_transform(another_tensor)Methods
| Name | Description |
|---|---|
| fit | Compute the mean and standard deviation of the input tensor. |
| fit_transform | Fit the scaler to the input tensor and then scale the tensor. |
| transform | Scale the input tensor using the computed mean and standard deviation. |
fit
utils.scaler.TorchStandardScaler.fit(x)Compute the mean and standard deviation of the input tensor.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| x | torch.Tensor |
The input tensor, expected shape [n_samples, n_features] | required |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If the input is not a torch tensor. |
fit_transform
utils.scaler.TorchStandardScaler.fit_transform(x)Fit the scaler to the input tensor and then scale the tensor.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| x | torch.Tensor |
The input tensor, expected shape [n_samples, n_features] | required |
Returns
| Name | Type | Description |
|---|---|---|
torch.Tensor |
torch.Tensor: The scaled tensor. |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If the input is not a torch tensor. |
transform
utils.scaler.TorchStandardScaler.transform(x)Scale the input tensor using the computed mean and standard deviation.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| x | torch.Tensor |
The input tensor to be transformed, expected shape [n_samples, n_features] | required |
Returns
| Name | Type | Description |
|---|---|---|
torch.Tensor |
torch.Tensor: The scaled tensor. |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If the input is not a torch tensor. | |
| RuntimeError | If the scaler has not been fitted before transforming data. |