-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
192 lines (157 loc) · 8.53 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import torch
from validate import validate
from models.masks import ParticleMask, SpecificParticleMask, KinematicMask
from argparse import ArgumentParser
import os
def train(train_loader, val_loader, models, device, optimizer, criterion, model_type, output_vars, zero_padded=[], mask=None, epochs:range=None, loss_min:int=999, save_path:str='./saved_models', model_name:str=''):
# Create an outputs folder to store config files
os.makedirs('./outputs/' + model_name, exist_ok=True)
if len(epochs) <= 0:
print("Num epochs <= 0")
return 0
if model_type == 'autoencoder':
tae = models[0]
for epoch in epochs:
tae.train()
running_loss = 0.0
for batch_idx, batch in enumerate(train_loader):
# Move the data to the device
inputs, _ = batch
inputs = inputs.to(device)
if mask is not None:
if mask == 0:
mask_layer = ParticleMask(output_vars+(output_vars%3))
else:
mask_layer = KinematicMask(mask)
# Mask input data
masked_inputs = mask_layer(inputs)
# Zero the gradients
optimizer.zero_grad()
# Forward pass
outputs = tae(masked_inputs)
outputs = torch.reshape(outputs, (outputs.size(0),
outputs.size(1) * outputs.size(2)))
# Flatten last axes and compute loss
if output_vars == 3:
inputs = inputs[:,:,:-1]
inputs = torch.reshape(inputs, (inputs.size(0),
inputs.size(1) * inputs.size(2)))
loss = criterion.compute_loss(outputs, inputs, zero_padded=[4])
elif output_vars == 4:
inputs = torch.reshape(inputs, (inputs.size(0),
inputs.size(1) * inputs.size(2)))
loss = criterion.compute_loss(outputs, inputs, zero_padded=zero_padded)
# Backward pass
loss.backward()
# Update the parameters
optimizer.step()
# Update running loss
running_loss += loss.item()
# Print running loss every 500 batches
if (batch_idx + 1) % 500 == 0:
print(f"Epoch [{epoch+1}/{epochs[-1] + 1}], Batch [{batch_idx+1}/{len(train_loader)}], Loss: {running_loss / 500:.4f}")
running_loss = 0.0
loss_min = validate(val_loader, models, device, criterion, model_type, output_vars, mask, epoch, epochs[-1] + 1, loss_min, save_path, model_name)
return loss_min
elif model_type == 'classifier partial':
tae, classifier = models[0], models[1]
for epoch in epochs:
tae.eval()
classifier.train()
running_loss = 0.0
for batch_idx, batch in enumerate(train_loader):
# Move the data to the device
inputs, labels = batch
inputs = inputs.to(device)
labels = labels.to(device)
if mask is not None:
if mask == 0:
mask_layer = ParticleMask(output_vars+(output_vars%3))
else:
mask_layer = KinematicMask(mask)
# Mask input data
masked_inputs = mask_layer(inputs)
# Forward pass for autoencoder
outputs = tae(masked_inputs)
# Reset trivial values
mask_999 = (masked_inputs[:, :, 3] == 999).float()
outputs[:,:,3:5] = torch.nn.functional.softmax(outputs[:,:,3:5], dim=2)
outputs[:, :, 3] = (1 - mask_999) * outputs[:, :, 3] + mask_999 * 1
outputs[:, :, 4] = (1 - mask_999) * outputs[:, :, 4]
masked_inputs[:,:,3:5] = torch.nn.functional.softmax(masked_inputs[:,:,3:5], dim=2)
masked_inputs[:, :, 3] = (1 - mask_999) * masked_inputs[:, :, 3] + mask_999 * 1
masked_inputs[:, :, 4] = (1 - mask_999) * masked_inputs[:, :, 4]
# Flatten last axis
outputs = torch.reshape(outputs, (outputs.size(0),
outputs.size(1) * outputs.size(2)))
masked_inputs = torch.reshape(masked_inputs, (masked_inputs.size(0),
masked_inputs.size(1) * masked_inputs.size(2)))
# Zero the gradients
optimizer.zero_grad()
# Forward pass for classifier
outputs_2 = classifier(torch.cat((outputs, masked_inputs), axis=1)).squeeze(1)
# Caclulate the loss
loss = criterion(outputs_2, labels.float())
# Backward pass
loss.backward()
# Update the parameters
optimizer.step()
# Update running loss
running_loss += loss.item()
# Print running loss every 500 batches
if (batch_idx + 1) % 500 == 0:
print(f"Epoch [{epoch+1}/{epochs[-1] + 1}], Batch [{batch_idx+1}/{len(train_loader)}], Loss: {running_loss / 500:.4f}")
running_loss = 0.0
loss_min = validate(val_loader, models, device, criterion, model_type, output_vars, mask, epoch, epochs[-1] + 1, loss_min, save_path, model_name)
return loss_min
elif model_type == 'classifier full':
tae, classifier = models[0], models[1]
for epoch in epochs:
tae.eval()
classifier.train()
running_loss = 0.0
for batch_idx, batch in enumerate(train_loader):
# Move the data to the device
inputs, labels = batch
inputs = inputs.to(device)
labels = labels.to(device)
outputs = torch.zeros(inputs.size(0), 6, output_vars+(output_vars%3)).to(device)
for i in range(6):
if mask is not None:
if mask == 0:
mask_layer = SpecificParticleMask(output_vars+(output_vars%3), i)
else:
mask_layer = KinematicMask(mask)
# Mask input data
masked_inputs = mask_layer(inputs)
# Forward pass for autoencoder
temp_outputs = tae(masked_inputs)
outputs[:,i,:] = temp_outputs[:,i,:]
# Reset trivial values
mask_999 = (masked_inputs[:, :, 3] == 999).float()
outputs[:,:,3:5] = torch.nn.functional.softmax(outputs[:,:,3:5], dim=2)
outputs[:, :, 3] = (1 - mask_999) * outputs[:, :, 3] + mask_999 * 1
outputs[:, :, 4] = (1 - mask_999) * outputs[:, :, 4]
# Flatten last axes of tensors
outputs = torch.reshape(outputs, (outputs.size(0),
outputs.size(1) * outputs.size(2)))
inputs = torch.reshape(inputs, (inputs.size(0),
inputs.size(1) * inputs.size(2)))
# Zero the gradients
optimizer.zero_grad()
# Forward pass for classifier
outputs_2 = classifier(torch.cat((outputs, inputs), axis=1)).squeeze(1)
# Caclulate the loss
loss = criterion(outputs_2, labels.float())
# Backward pass
loss.backward()
# Update the parameters
optimizer.step()
# Update running loss
running_loss += loss.item()
# Print running loss every 500 batches
if (batch_idx + 1) % 500 == 0:
print(f"Epoch [{epoch+1}/{epochs[-1] + 1}], Batch [{batch_idx+1}/{len(train_loader)}], Loss: {running_loss / 500:.4f}")
running_loss = 0.0
loss_min = validate(val_loader, models, device, criterion, model_type, output_vars, mask, epoch, epochs[-1] + 1, loss_min, save_path, model_name)
return loss_min