scaler
TorchMinMaxScaler
¶
A class for scaling data using min-max normalization with PyTorch tensors. This scaler calculates the minimum and maximum values in the dataset to scale the data within a given range.
Attributes:
Name | Type | Description |
---|---|---|
min |
Tensor
|
The minimum values computed over the fitted data. |
max |
Tensor
|
The maximum values computed over the fitted data. |
Examples:
>>> import torch
>>> from spotpython.utils.scaler import TorchMinMaxScaler
>>> scaler = TorchMinMaxScaler()
# Given a tensor
>>> tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
# Fit and transform the tensor using the scaler
>>> scaled_tensor = scaler.fit_transform(tensor)
>>> print(scaled_tensor)
# The output will be a tensor with values scaled between 0 and 1.
Source code in spotpython/utils/scaler.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
__init__()
¶
Initializes the TorchMinMaxScaler class without any predefined min and max.
Source code in spotpython/utils/scaler.py
112 113 114 115 116 117 |
|
fit(x)
¶
Compute the minimum and maximum value of the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a torch tensor. |
Source code in spotpython/utils/scaler.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
fit_transform(x)
¶
Fit the scaler to the input tensor and then scale the tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The scaled tensor. |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a torch tensor. |
Source code in spotpython/utils/scaler.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
transform(x)
¶
Scale the input tensor using the computed minimum and maximum values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor to be scaled. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The scaled tensor. |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a torch tensor. |
RuntimeError
|
If the scaler has not been fitted before transforming data. |
Source code in spotpython/utils/scaler.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
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 |
Tensor
|
The mean value computed over the fitted data. |
std |
Tensor
|
The standard deviation computed over the fitted data. |
Examples:
>>> import torch
>>> from spotpython.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)
# Using fit_transform method to fit and transform in one step
>>> another_tensor = torch.rand((10, 3))
>>> scaled_tensor = scaler.fit_transform(another_tensor)
>>> print(scaled_tensor)
Source code in spotpython/utils/scaler.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
__init__()
¶
Initializes the TorchStandardScaler class without any pre-defined mean and std.
Source code in spotpython/utils/scaler.py
31 32 33 34 35 36 |
|
fit(x)
¶
Compute the mean and standard deviation of the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor, expected shape [n_samples, n_features] |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a torch tensor. |
Source code in spotpython/utils/scaler.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
fit_transform(x)
¶
Fit the scaler to the input tensor and then scale the tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor, expected shape [n_samples, n_features] |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The scaled tensor. |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a torch tensor. |
Source code in spotpython/utils/scaler.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
transform(x)
¶
Scale the input tensor using the computed mean and standard deviation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor to be transformed, expected shape [n_samples, n_features] |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The scaled tensor. |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a torch tensor. |
RuntimeError
|
If the scaler has not been fitted before transforming data. |
Source code in spotpython/utils/scaler.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|