-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
105 lines (70 loc) · 3.45 KB
/
train.py
1
2
3
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import torch
from torch import nn
import pandas as pd
from torch.utils.data import DataLoader
##
def train_mlp(mlp, trainloader, optimizer, loss_function, seed, epoch_number, batch_size):
# Set fixed random number seed
torch.manual_seed(seed)
# Run the training loop
#iterate over the entire training dataset for a fixed number of epochs
for epoch in range(0, epoch_number): # 5 epochs at maximum
# Print epoch
print(f'Starting epoch {epoch+1}')
# Iterate over the DataLoader for training data (iterate over all the batches)
for i, data in enumerate(trainloader, 0):
# Get and prepare inputs
inputs, targets = data
# perform some conversions (e.g. Floating point conversion and reshaping) on the inputs and targets in the current batch
inputs, targets = inputs.float(), targets.float()
targets = targets.reshape((targets.shape[0], 1))
# Zero the gradients: knowledge of previous improvements (especially important in batch > 0 for every epoch) is no longer available
optimizer.zero_grad()
# Perform forward pass
outputs = mlp(inputs).cuda()
# Compute loss
loss = loss_function(outputs, targets)
# Perform backward pass
loss.backward()
# Perform optimization
optimizer.step()
# Process is complete.
print('Training process has finished.')
print('number of epochs ', epoch+1 )
return mlp
#%%
def train_mlp_mp(mlp, trainloader, seed, epoch_number, batch_size, filename_model):
# Set fixed random number seed
torch.manual_seed(seed)
loss_function = nn.MSELoss() # alternatively I can choose the nn.L1Loss(), or any pre-defined custom function trial and error will tell me the optimal loss function
#loss_function = functions.custom_loss_function
## choose ADAM otimiser (standard) with common learning rate equal to 1e-4
optimizer = torch.optim.Adam(mlp.parameters(), lr=1e-4)
# Run the training loop
#iterate over the entire training dataset for a fixed number of epochs
for epoch in range(0, epoch_number): # 5 epochs at maximum
# Print epoch
print(f'Starting epoch {epoch+1}')
# Iterate over the DataLoader for training data (iterate over all the batches)
for i, data in enumerate(trainloader, 0):
# Get and prepare inputs
inputs, targets = data
# perform some conversions (e.g. Floating point conversion and reshaping) on the inputs and targets in the current batch
inputs, targets = inputs.float(), targets.float()
targets = targets.reshape((targets.shape[0], 1))
# Zero the gradients: knowledge of previous improvements (especially important in batch > 0 for every epoch) is no longer available
optimizer.zero_grad()
# Perform forward pass
outputs = mlp(inputs).cuda()
# Compute loss
loss = loss_function(outputs, targets)
# Perform backward pass
loss.backward()
# Perform optimization
optimizer.step()
# Process is complete.
print('Training process has finished.')
print('number of epochs ', epoch+1 )
# save the model
torch.save(mlp, filename_model)
return mlp