Commit 9be2ebde authored by Elif Ceylan's avatar Elif Ceylan
Browse files

GROUPWORK_create train.py to train the network

parent cfac83f7
# Internal
# Internal
from optim import SGD
import modules as n
def train_model(model, train_input, train_target, criterion, lr, gamma, mini_batch_size=100, nb_epochs=100, optimizer=None):
if type(model.mods[-1]).__name__ == 'Sigmoid':
threshold = 0.5
elif type(model.mods[-1]).__name__ == 'Tanh':
threshold = 0
loss_list = []
nb_errors = 0
for e in range(nb_epochs):
acc_loss = 0
nb_errors = 0
for b in range(0, train_input.size(0), mini_batch_size):
## forward
output = model.forward(train_input.narrow(0, b, mini_batch_size))
y = train_target.narrow(0, b, mini_batch_size)
## loss
loss = criterion.forward(output.squeeze(), y)
acc_loss = acc_loss + loss.item()
## zero grad
if optimizer is not None:
optimizer.zero_grad()
else:
model.zero_grad()
## gradient of loss
g_loss = criterion.backward(output.squeeze(), y)
## backward
model.backward(g_loss)
## gradient step
if optimizer is not None:
optimizer.step()
else:
model.update_params(lr)
# if(e==(nb_epochs-1)):
for k in range(mini_batch_size):
output[output<=threshold]=0
output[output>threshold]=1
if y[k] != output[k]:
nb_errors = nb_errors + 1
print(f'acc: {1 - nb_errors/train_input.size(0)}')
accuracy_e = 1 - acc_loss/train_input.size(0)
print(e, accuracy_e)
loss_list.append(acc_loss)
return nb_errors, loss_list
def test_model(model, test_input, test_target, mini_batch_size):
nb_errors = 0
for b in range(0, test_input.size(0), mini_batch_size):
output = model.forward(test_input.narrow(0, b, mini_batch_size))
output[output<=0]=0
output[output>0]=1
y = test_target.narrow(0, b, mini_batch_size)
for k in range(mini_batch_size):
if y[k] != output[k]:
nb_errors = nb_errors + 1
return nb_errors
\ No newline at end of file
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