Dateien nach „tools“ hochladen
This commit is contained in:
parent
8b432157d8
commit
e3d1fad205
5 changed files with 148 additions and 0 deletions
13
tools/L1NormLayer.py
Normal file
13
tools/L1NormLayer.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
import torch
|
||||
|
||||
|
||||
class L1NormLayer(torch.nn.Module):
|
||||
|
||||
epsilon: float
|
||||
|
||||
def __init__(self, epsilon: float = 10e-20) -> None:
|
||||
super().__init__()
|
||||
self.epsilon = epsilon
|
||||
|
||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||||
return input / (input.sum(dim=1, keepdim=True) + self.epsilon)
|
8
tools/append_parameter.py
Normal file
8
tools/append_parameter.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
import torch
|
||||
|
||||
|
||||
def append_parameter(
|
||||
module: torch.nn.Module, parameter_list: list[torch.nn.parameter.Parameter]
|
||||
):
|
||||
for netp in module.parameters():
|
||||
parameter_list.append(netp)
|
31
tools/data_loader.py
Normal file
31
tools/data_loader.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import torch
|
||||
|
||||
|
||||
def data_loader(
|
||||
pattern: torch.Tensor,
|
||||
labels: torch.Tensor,
|
||||
worker_init_fn,
|
||||
generator,
|
||||
batch_size: int = 128,
|
||||
shuffle: bool = True,
|
||||
torch_device: torch.device = torch.device("cpu"),
|
||||
) -> torch.utils.data.dataloader.DataLoader:
|
||||
|
||||
assert pattern.ndim >= 3
|
||||
|
||||
pattern_storage: torch.Tensor = pattern.to(torch_device).type(torch.float32)
|
||||
if pattern_storage.ndim == 3:
|
||||
pattern_storage = pattern_storage.unsqueeze(1)
|
||||
pattern_storage /= pattern_storage.max()
|
||||
|
||||
label_storage: torch.Tensor = labels.to(torch_device).type(torch.int64)
|
||||
|
||||
dataloader = torch.utils.data.DataLoader(
|
||||
torch.utils.data.TensorDataset(pattern_storage, label_storage),
|
||||
batch_size=batch_size,
|
||||
shuffle=shuffle,
|
||||
worker_init_fn=worker_init_fn,
|
||||
generator=generator,
|
||||
)
|
||||
|
||||
return dataloader
|
64
tools/loss_function.py
Normal file
64
tools/loss_function.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
import torch
|
||||
|
||||
|
||||
# loss_mode == 0: "normal" SbS loss function mixture
|
||||
# loss_mode == 1: cross_entropy
|
||||
def loss_function(
|
||||
h: torch.Tensor,
|
||||
labels: torch.Tensor,
|
||||
loss_mode: int = 0,
|
||||
number_of_output_neurons: int = 10,
|
||||
loss_coeffs_mse: float = 0.0,
|
||||
loss_coeffs_kldiv: float = 0.0,
|
||||
) -> torch.Tensor | None:
|
||||
|
||||
assert loss_mode >= 0
|
||||
assert loss_mode <= 1
|
||||
|
||||
assert h.ndim == 2
|
||||
|
||||
if loss_mode == 0:
|
||||
|
||||
# Convert label into one hot
|
||||
target_one_hot: torch.Tensor = torch.zeros(
|
||||
(
|
||||
labels.shape[0],
|
||||
number_of_output_neurons,
|
||||
),
|
||||
device=h.device,
|
||||
dtype=h.dtype,
|
||||
)
|
||||
|
||||
target_one_hot.scatter_(
|
||||
1,
|
||||
labels.to(h.device).unsqueeze(1),
|
||||
torch.ones(
|
||||
(labels.shape[0], 1),
|
||||
device=h.device,
|
||||
dtype=h.dtype,
|
||||
),
|
||||
)
|
||||
|
||||
my_loss: torch.Tensor = ((h - target_one_hot) ** 2).sum(dim=0).mean(
|
||||
dim=0
|
||||
) * loss_coeffs_mse
|
||||
|
||||
my_loss = (
|
||||
my_loss
|
||||
+ (
|
||||
(target_one_hot * torch.log((target_one_hot + 1e-20) / (h + 1e-20)))
|
||||
.sum(dim=0)
|
||||
.mean(dim=0)
|
||||
)
|
||||
* loss_coeffs_kldiv
|
||||
)
|
||||
|
||||
my_loss = my_loss / (abs(loss_coeffs_kldiv) + abs(loss_coeffs_mse))
|
||||
|
||||
return my_loss
|
||||
|
||||
elif loss_mode == 1:
|
||||
my_loss = torch.nn.functional.cross_entropy(h, labels.to(h.device))
|
||||
return my_loss
|
||||
else:
|
||||
return None
|
32
tools/make_optimize.py
Normal file
32
tools/make_optimize.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import torch
|
||||
|
||||
|
||||
def make_optimize(
|
||||
parameters: list[list[torch.nn.parameter.Parameter]],
|
||||
lr_initial: list[float],
|
||||
eps=1e-10,
|
||||
) -> tuple[
|
||||
list[torch.optim.Adam | None],
|
||||
list[torch.optim.lr_scheduler.ReduceLROnPlateau | None],
|
||||
]:
|
||||
list_optimizer: list[torch.optim.Adam | None] = []
|
||||
list_lr_scheduler: list[torch.optim.lr_scheduler.ReduceLROnPlateau | None] = []
|
||||
|
||||
assert len(parameters) == len(lr_initial)
|
||||
|
||||
for i in range(0, len(parameters)):
|
||||
if len(parameters[i]) > 0:
|
||||
list_optimizer.append(torch.optim.Adam(parameters[i], lr=lr_initial[i]))
|
||||
else:
|
||||
list_optimizer.append(None)
|
||||
|
||||
for i in range(0, len(list_optimizer)):
|
||||
if list_optimizer[i] is not None:
|
||||
pass
|
||||
list_lr_scheduler.append(
|
||||
torch.optim.lr_scheduler.ReduceLROnPlateau(list_optimizer[i], eps=eps) # type: ignore
|
||||
)
|
||||
else:
|
||||
list_lr_scheduler.append(None)
|
||||
|
||||
return (list_optimizer, list_lr_scheduler)
|
Loading…
Reference in a new issue