Skip to content

Commit

Permalink
Generated all figures and replaced traintestloop functions with early…
Browse files Browse the repository at this point in the history
… stopping functions. Also now starting to comment all code
  • Loading branch information
ilennaj committed Sep 1, 2020
1 parent c29af93 commit fd35f95
Show file tree
Hide file tree
Showing 25 changed files with 20,657 additions and 3,889 deletions.
99 changes: 92 additions & 7 deletions custompackage/.ipynb_checkpoints/traintestloop-checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,37 @@
from torch.optim import Optimizer


def train_test_ktree(model, trainloader, testloader, epochs=10, randorder=False):
def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, randorder=False, patience=60):
'''
Trains and tests k-tree models
Inputs: model, trainloader, validloader, testloader, epochs, randorder, patience
Outputs: train loss_curve, train acc_curve, test ave_loss, test accuracy, trained model
'''
# Initialize loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# to track training loss and accuracy as model trains
loss_curve = []
acc_curve = []

# to track the validation loss as the model trains
valid_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []

#
if randorder == True:
ordering = torch.randperm(len(trainloader.dataset.tensors[0][0]))


early_stopping = EarlyStopping(patience=patience, verbose=False)

for epoch in range(epochs): # loop over the dataset multiple times

running_loss = 0.0
running_acc = 0.0

model.train()

for i, data in enumerate(trainloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
Expand Down Expand Up @@ -61,7 +76,36 @@ def train_test_ktree(model, trainloader, testloader, epochs=10, randorder=False)
loss_curve.append(running_loss/3)
acc_curve.append(running_acc/3)
running_loss = 0.0


######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
# calculate the loss
loss = criterion(output, labels.float().reshape(-1,1))
# record validation loss
valid_losses.append(loss.item())

valid_loss = np.average(valid_losses)


# early_stopping needs the validation loss to check if it has decresed,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)

if early_stopping.early_stop:
print("Early stopping")
break

# load the last checkpoint with the best model
# model.load_state_dict(torch.load('checkpoint.pt'))

print('Finished Training, %d epochs' % (epoch+1))

correct = 0
Expand Down Expand Up @@ -90,17 +134,29 @@ def train_test_ktree(model, trainloader, testloader, epochs=10, randorder=False)
else:
return(loss_curve, acc_curve, loss, accuracy, model)

def train_test_fc(model, trainloader, testloader, epochs=10):
def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patience=60):
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)


# to track the validation loss as the model trains
valid_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []


loss_curve = []
acc_curve = []

early_stopping = EarlyStopping(patience=patience, verbose=False)


for epoch in range(epochs): # loop over the dataset multiple times

running_loss = 0.0
running_acc = 0.0

model.train()

for i, data in enumerate(trainloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
Expand All @@ -125,7 +181,36 @@ def train_test_fc(model, trainloader, testloader, epochs=10):
acc_curve.append(running_acc/3)
running_loss = 0.0
running_acc = 0.0


######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
# calculate the loss
loss = criterion(output, labels.float().reshape(-1,1))
# record validation loss
valid_losses.append(loss.item())

valid_loss = np.average(valid_losses)


# early_stopping needs the validation loss to check if it has decresed,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)

if early_stopping.early_stop:
print("Early stopping")
break

# load the last checkpoint with the best model
# model.load_state_dict(torch.load('checkpoint.pt'))

print('Finished Training, %d epochs' % (epoch+1))

correct = 0
Expand Down
99 changes: 92 additions & 7 deletions custompackage/traintestloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,37 @@
from torch.optim import Optimizer


def train_test_ktree(model, trainloader, testloader, epochs=10, randorder=False):
def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, randorder=False, patience=60):
'''
Trains and tests k-tree models
Inputs: model, trainloader, validloader, testloader, epochs, randorder, patience
Outputs: train loss_curve, train acc_curve, test ave_loss, test accuracy, trained model
'''
# Initialize loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# to track training loss and accuracy as model trains
loss_curve = []
acc_curve = []

# to track the validation loss as the model trains
valid_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []

#
if randorder == True:
ordering = torch.randperm(len(trainloader.dataset.tensors[0][0]))


early_stopping = EarlyStopping(patience=patience, verbose=False)

for epoch in range(epochs): # loop over the dataset multiple times

running_loss = 0.0
running_acc = 0.0

model.train()

for i, data in enumerate(trainloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
Expand Down Expand Up @@ -61,7 +76,36 @@ def train_test_ktree(model, trainloader, testloader, epochs=10, randorder=False)
loss_curve.append(running_loss/3)
acc_curve.append(running_acc/3)
running_loss = 0.0


######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
# calculate the loss
loss = criterion(output, labels.float().reshape(-1,1))
# record validation loss
valid_losses.append(loss.item())

valid_loss = np.average(valid_losses)


# early_stopping needs the validation loss to check if it has decresed,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)

if early_stopping.early_stop:
print("Early stopping")
break

# load the last checkpoint with the best model
# model.load_state_dict(torch.load('checkpoint.pt'))

print('Finished Training, %d epochs' % (epoch+1))

correct = 0
Expand Down Expand Up @@ -90,17 +134,29 @@ def train_test_ktree(model, trainloader, testloader, epochs=10, randorder=False)
else:
return(loss_curve, acc_curve, loss, accuracy, model)

def train_test_fc(model, trainloader, testloader, epochs=10):
def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patience=60):
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)


# to track the validation loss as the model trains
valid_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []


loss_curve = []
acc_curve = []

early_stopping = EarlyStopping(patience=patience, verbose=False)


for epoch in range(epochs): # loop over the dataset multiple times

running_loss = 0.0
running_acc = 0.0

model.train()

for i, data in enumerate(trainloader):
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
Expand All @@ -125,7 +181,36 @@ def train_test_fc(model, trainloader, testloader, epochs=10):
acc_curve.append(running_acc/3)
running_loss = 0.0
running_acc = 0.0


######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
# calculate the loss
loss = criterion(output, labels.float().reshape(-1,1))
# record validation loss
valid_losses.append(loss.item())

valid_loss = np.average(valid_losses)


# early_stopping needs the validation loss to check if it has decresed,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)

if early_stopping.early_stop:
print("Early stopping")
break

# load the last checkpoint with the best model
# model.load_state_dict(torch.load('checkpoint.pt'))

print('Finished Training, %d epochs' % (epoch+1))

correct = 0
Expand Down
Loading

0 comments on commit fd35f95

Please sign in to comment.