| 31 | |
| 32 | |
| 33 | class NormalizationMethod(ABC): |
| 34 | def __init__(self, *args, **kwargs): |
| 35 | pass |
| 36 | |
| 37 | def normalize(self, data: np.ndarray, axis=0, *args, **kwargs): |
| 38 | return data |
| 39 | |
| 40 | def inverse_normalize(self, normalized_data: np.ndarray): |
| 41 | return normalized_data |
| 42 | |
| 43 | def stored_values(self): |
| 44 | return dict() |
| 45 | |
| 46 | def __copy__(self): |
| 47 | return type(self)(**self.stored_values()) |
| 48 | |
| 49 | def copy(self): |
| 50 | return self.__copy__() |
| 51 | |
| 52 | def change_input_type(self, new_type): |
| 53 | for attribute, value in self.__dict__.items(): |
| 54 | if new_type in [torch.Tensor, torch.TensorType]: |
| 55 | if isinstance(value, np.ndarray): |
| 56 | setattr(self, attribute, torch.from_numpy(value)) |
| 57 | elif new_type == np.ndarray: |
| 58 | if torch.is_tensor(value): |
| 59 | setattr(self, attribute, value.numpy().cpu()) |
| 60 | else: |
| 61 | setattr(self, attribute, new_type(value)) |
| 62 | |
| 63 | def apply_torch_func(self, fn): |
| 64 | """ |
| 65 | Function to be called to apply a torch function to all tensors of this class, e.g. apply .to(), .cuda(), ..., |
| 66 | Just call this function from within the model's nn.Module._apply() |
| 67 | """ |
| 68 | for attribute, value in self.__dict__.items(): |
| 69 | if torch.is_tensor(value): |
| 70 | setattr(self, attribute, fn(value)) |
| 71 | |
| 72 | |
| 73 | class Z_Normalizer(NormalizationMethod): |