Skip to content

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
class 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:
        min (torch.Tensor): The minimum values computed over the fitted data.
        max (torch.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.
    """

    def __init__(self):
        """
        Initializes the TorchMinMaxScaler class without any predefined min and max.
        """
        self.min = None
        self.max = None

    def fit(self, x: torch.Tensor) -> None:
        """
        Compute the minimum and maximum value of the input tensor.

        Args:
            x (torch.Tensor): The input tensor.

        Raises:
            TypeError: If the input is not a torch tensor.
        """
        if not torch.is_tensor(x):
            raise TypeError("Input should be a torch tensor")
        self.min = x.min(dim=0, keepdim=True).values
        self.max = x.max(dim=0, keepdim=True).values

    def transform(self, x: torch.Tensor) -> torch.Tensor:
        """
        Scale the input tensor using the computed minimum and maximum values.

        Args:
            x (torch.Tensor): The input tensor to be scaled.

        Returns:
            torch.Tensor: The scaled tensor.

        Raises:
            TypeError: If the input is not a torch tensor.
            RuntimeError: If the scaler has not been fitted before transforming data.
        """
        if not torch.is_tensor(x):
            raise TypeError("Input should be a torch tensor")
        if self.min is None or self.max is None:
            raise RuntimeError("Must fit scaler before transforming data")
        x = (x - self.min) / (self.max - self.min + 1e-7)
        return x

    def fit_transform(self, x: torch.Tensor) -> torch.Tensor:
        """
        Fit the scaler to the input tensor and then scale the tensor.

        Args:
            x (torch.Tensor): The input tensor.

        Returns:
            torch.Tensor: The scaled tensor.

        Raises:
            TypeError: If the input is not a torch tensor.
        """
        self.fit(x)
        return self.transform(x)

__init__()

Initializes the TorchMinMaxScaler class without any predefined min and max.

Source code in spotpython/utils/scaler.py
112
113
114
115
116
117
def __init__(self):
    """
    Initializes the TorchMinMaxScaler class without any predefined min and max.
    """
    self.min = None
    self.max = None

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
def fit(self, x: torch.Tensor) -> None:
    """
    Compute the minimum and maximum value of the input tensor.

    Args:
        x (torch.Tensor): The input tensor.

    Raises:
        TypeError: If the input is not a torch tensor.
    """
    if not torch.is_tensor(x):
        raise TypeError("Input should be a torch tensor")
    self.min = x.min(dim=0, keepdim=True).values
    self.max = x.max(dim=0, keepdim=True).values

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
def fit_transform(self, x: torch.Tensor) -> torch.Tensor:
    """
    Fit the scaler to the input tensor and then scale the tensor.

    Args:
        x (torch.Tensor): The input tensor.

    Returns:
        torch.Tensor: The scaled tensor.

    Raises:
        TypeError: If the input is not a torch tensor.
    """
    self.fit(x)
    return self.transform(x)

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
def transform(self, x: torch.Tensor) -> torch.Tensor:
    """
    Scale the input tensor using the computed minimum and maximum values.

    Args:
        x (torch.Tensor): The input tensor to be scaled.

    Returns:
        torch.Tensor: The scaled tensor.

    Raises:
        TypeError: If the input is not a torch tensor.
        RuntimeError: If the scaler has not been fitted before transforming data.
    """
    if not torch.is_tensor(x):
        raise TypeError("Input should be a torch tensor")
    if self.min is None or self.max is None:
        raise RuntimeError("Must fit scaler before transforming data")
    x = (x - self.min) / (self.max - self.min + 1e-7)
    return x

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
class 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:
        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 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)
    """

    def __init__(self):
        """
        Initializes the TorchStandardScaler class without any pre-defined mean and std.
        """
        self.mean = None
        self.std = None

    def fit(self, x: torch.Tensor) -> None:
        """
        Compute the mean and standard deviation of the input tensor.

        Args:
            x (torch.Tensor): The input tensor, expected shape [n_samples, n_features]

        Raises:
            TypeError: If the input is not a torch tensor.
        """
        if not torch.is_tensor(x):
            raise TypeError("Input should be a torch tensor")
        self.mean = x.mean(dim=0, keepdim=True)
        self.std = x.std(dim=0, unbiased=False, keepdim=True)

    def transform(self, x: torch.Tensor) -> torch.Tensor:
        """
        Scale the input tensor using the computed mean and standard deviation.

        Args:
            x (torch.Tensor): The input tensor to be transformed, expected shape [n_samples, n_features]

        Returns:
            torch.Tensor: The scaled tensor.

        Raises:
            TypeError: If the input is not a torch tensor.
            RuntimeError: If the scaler has not been fitted before transforming data.
        """
        if not torch.is_tensor(x):
            raise TypeError("Input should be a torch tensor")
        if self.mean is None or self.std is None:
            raise RuntimeError("Must fit scaler before transforming data")
        x = (x - self.mean) / (self.std + 1e-7)
        return x

    def fit_transform(self, x: torch.Tensor) -> torch.Tensor:
        """
        Fit the scaler to the input tensor and then scale the tensor.

        Args:
            x (torch.Tensor): The input tensor, expected shape [n_samples, n_features]

        Returns:
            torch.Tensor: The scaled tensor.

        Raises:
            TypeError: If the input is not a torch tensor.
        """
        self.fit(x)
        return self.transform(x)

__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
def __init__(self):
    """
    Initializes the TorchStandardScaler class without any pre-defined mean and std.
    """
    self.mean = None
    self.std = None

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
def fit(self, x: torch.Tensor) -> None:
    """
    Compute the mean and standard deviation of the input tensor.

    Args:
        x (torch.Tensor): The input tensor, expected shape [n_samples, n_features]

    Raises:
        TypeError: If the input is not a torch tensor.
    """
    if not torch.is_tensor(x):
        raise TypeError("Input should be a torch tensor")
    self.mean = x.mean(dim=0, keepdim=True)
    self.std = x.std(dim=0, unbiased=False, keepdim=True)

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
def fit_transform(self, x: torch.Tensor) -> torch.Tensor:
    """
    Fit the scaler to the input tensor and then scale the tensor.

    Args:
        x (torch.Tensor): The input tensor, expected shape [n_samples, n_features]

    Returns:
        torch.Tensor: The scaled tensor.

    Raises:
        TypeError: If the input is not a torch tensor.
    """
    self.fit(x)
    return self.transform(x)

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
def transform(self, x: torch.Tensor) -> torch.Tensor:
    """
    Scale the input tensor using the computed mean and standard deviation.

    Args:
        x (torch.Tensor): The input tensor to be transformed, expected shape [n_samples, n_features]

    Returns:
        torch.Tensor: The scaled tensor.

    Raises:
        TypeError: If the input is not a torch tensor.
        RuntimeError: If the scaler has not been fitted before transforming data.
    """
    if not torch.is_tensor(x):
        raise TypeError("Input should be a torch tensor")
    if self.mean is None or self.std is None:
        raise RuntimeError("Must fit scaler before transforming data")
    x = (x - self.mean) / (self.std + 1e-7)
    return x