Commit aa1b88c9 authored by Mira Arabi Haddad's avatar Mira Arabi Haddad
Browse files

GROUPWORK_Finalize train.py

parent 1e34c19f
from net import *
## Train Model, works for both Basic + Weight Sharing Networks
def train_model(model, train_input, train_target, lr, criterion, mini_batch_size, nb_epochs = 100):
def train_model(model, train_input, train_target, lr, criterion, mini_batch_size, nb_epochs):
loss_list = []
acc_list = []
eta = 1e-2
for e in range(nb_epochs):
acc_loss = 0
......@@ -15,14 +16,14 @@ def train_model(model, train_input, train_target, lr, criterion, mini_batch_size
loss = criterion(output.view(-1), y.float())
acc_loss = acc_loss + loss.item()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=0)
optimizer = torch.optim.Adam(model.parameters(), lr, weight_decay=0)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
for p in model.parameters():
p -= lr * p.grad
p -= eta * p.grad
output_b = (output.view(-1)>0.5).float()
for k in range(mini_batch_size):
......@@ -50,144 +51,40 @@ def test_model(model, test_input, test_target, mini_batch_size):
accuracy = 1 - nb_errors/test_input.size(0)
return accuracy
# def compute_nb_errors_test(model, input, target, mini_batch_size, nb_epochs):
# loss_list = []
# criterion = nn.BCELoss()
# nb_errors = 0
# for e in range(nb_epochs):
# acc_loss = 0
# for b in range(0, input.size(0), mini_batch_size):
# output = model(input.narrow(0, b, mini_batch_size))
# output_b = (output.view(-1)>0.5).float()
# y = target.narrow(0, b, mini_batch_size)
# loss = criterion(output.view(-1), y.float())
# acc_loss = acc_loss + loss.item()
# if(e==(nb_epochs-1)):
# for k in range(mini_batch_size):
# if target[b+k] != output_b[k]:
# nb_errors = nb_errors + 1
# loss_list.append(acc_loss)
# return nb_errors, loss_list
# ## Classification
# def train_model_class(model, train_input, train_target, mini_batch_size, nb_epochs = 100):
# criterion = nn.CrossEntropyLoss()
# eta = 1e-1
# for e in range(nb_epochs):
# acc_loss = 0
# for b in range(0, train_input.size(0), mini_batch_size):
# output= model(train_input[:,0].narrow(0, b, mini_batch_size), train_input[:,1].narrow(0, b, mini_batch_size))
# # y = train_target.narrow(0, b, mini_batch_size)
# y1 = train_target[:,0].narrow(0, b, mini_batch_size)
# y2 = train_target[:,1].narrow(0, b, mini_batch_size)
# loss1 = criterion(output[:,0:10], y1)
# loss2 = criterion(output[:,10:20], y2)
# loss = loss1 + loss2
# acc_loss = acc_loss + loss.item()
# # add optimizer
# optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=0)
# # model.zero_grad()
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# with torch.no_grad():
# for p in model.parameters():
# p -= eta * p.grad
# # print(e, acc_loss)
# return output
# def compute_nb_errors_class(model, input, target, mini_batch_size):
# nb_errors = 0
# for b in range(0, input.size(0), mini_batch_size):
# output1, output2= model(input[:,0].narrow(0, b, mini_batch_size), input[:,1].narrow(0, b, mini_batch_size))
# y1 = target[:,0].narrow(0, b, mini_batch_size)
# y2 = target[:,1].narrow(0, b, mini_batch_size)
# _, predicted_classes1 = output1.max(1)
# _, predicted_classes2 = output2.max(1)
# for k in range(mini_batch_size):
# if y1[k] != predicted_classes1[k]:
# nb_errors+=1
# if y2[k] != predicted_classes2[k]:
# nb_errors+=1
# return nb_errors
# ## Comparison
# # def train_model_comp(model, class_input1, class_input2, train_target, mini_batch_size, nb_epochs = 100):
# def train_model_comp(model, class_input, train_target, mini_batch_size, nb_epochs = 100):
# criterion = nn.BCELoss()
# eta = 1e-1
# for e in range(nb_epochs):
# acc_loss = 0
# for b in range(0, class_input.size(0), mini_batch_size):
# # output = model(class_input1.narrow(0, b, mini_batch_size), class_input2.narrow(0, b, mini_batch_size))
# output = model(class_input.narrow(0, b, mini_batch_size))
# y = train_target.narrow(0, b, mini_batch_size)
# loss = criterion(output.view(-1), y.float())
# acc_loss = acc_loss + loss.item()
# # add optimizer
# optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=0.9)
# optimizer.zero_grad()
# # model.zero_grad()
# loss.backward()
# optimizer.step()
# with torch.no_grad():
# for p in model.parameters():
# p -= eta * p.grad
# print(e, acc_loss)
# def compute_nb_errors_comp(model, input, target, mini_batch_size):
# nb_errors = 0
# for b in range(0, input.size(0), mini_batch_size):
# # output= model(input[:,0:10].narrow(0, b, mini_batch_size), input[:,10:20].narrow(0, b, mini_batch_size))
# output = model(input.narrow(0, b, mini_batch_size))
# y = target.narrow(0, b, mini_batch_size)
# for k in range(mini_batch_size):
# if output[k] != y[k]:
# nb_errors+=1
# return nb_errors
## Train Model WSAL, works for Weight Sharing with Auxiliary Loss
def train_model_WSAL(model, train_input, train_target, train_classes, lr, criterion_class, criterion_comp, mini_batch_size, nb_epochs):
loss_list = []
acc_list = []
eta = 1e-1
for e in range(nb_epochs):
acc_loss = 0
nb_errors = 0
for b in range(0, train_input.size(0), mini_batch_size):
# ouput of classification, res from comparison
out, res = model(train_input.narrow(0, b, mini_batch_size))
# classes for images 1
y1 = train_classes[:,0].narrow(0, b, mini_batch_size)
# classes for images 2
y2 = train_classes[:,1].narrow(0, b, mini_batch_size)
# classification loss for images 1
loss1 = criterion_class(out[:,0:10], y1)
# classification loss for images 1
loss2 = criterion_class(out[:,10:20], y2)
y = train_target.narrow(0, b, mini_batch_size)
# comparison loss
loss3 = criterion_comp(res.view(-1), y.float())
loss = 0.5*loss1 + 0.5*loss2 + 1.0*loss3
loss = 0.05*loss1 + 0.05*loss2 + 0.1*loss3
acc_loss = acc_loss + loss.item()
optimizer = torch.optim.Adam(model.parameters(), lr, weight_decay=0.9)
optimizer = torch.optim.Adam(model.parameters(), lr, weight_decay=1e-5)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
for p in model.parameters():
p -= lr * p.grad
p -= eta * p.grad
output_b = (res.view(-1)>0.5).float()
for k in range(mini_batch_size):
......@@ -202,6 +99,7 @@ def train_model_WSAL(model, train_input, train_target, train_classes, lr, criter
return loss_list, acc_list
## Test Model WSAL, works for Weight Sharing with Auxiliary Loss
def test_model_WSAL(model, test_input, test_target, mini_batch_size):
nb_errors = 0
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment