-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_loss_update_params.py
56 lines (39 loc) · 1.51 KB
/
compute_loss_update_params.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
import torch
from partition_mats import PartitionMats
from ss_loss import SSLoss
def ComputeLossUpdateParams(data, model, n, m, p, optimizer = None):
'''
Computee the loss, update gradients, and get the output of the model
Args:
data: input data to model
target: true labels
Returns:
output: output of model
loss: loss value from data
'''
output = None
loss = None
# If in training mode, update weights, otherwise do not
if model.training:
# Call the forward pass on the model. The data model() automatically calls model.forward()
output = model(data)
# Partition out relevent matrices
Sxp,Su,Sx,A,B,C,K,ic = PartitionMats(data,output,n,m,p)
# Calculate loss
loss, diff = SSLoss(Sxp,Su,Sx,A,B,C,K,ic)
# Main backward pass to Update gradients
optimizer.zero_grad()
loss.backward() # Compute gradients of all the parameters wrt the loss
optimizer.step() # Takes a optimization step
return output, loss
else:
# Do not update gradients
with torch.no_grad():
output = model(data)
# Partition out relevent matrices
Sxp,Su,Sx,A,B,C,K = PartitionMats(data,output,n,m)
# Calculate loss
loss, diff = SSLoss(Sxp,Su,Sx,A,B,C,K)
print(f'Sx = \n{Sx}')
print(f'diff = \n{diff}')
return output, loss