def train_model(no_epochs): if torch.cuda.is_available(): dev = "cuda:0" else: dev = "cpu" device = torch.device(dev) print(device) batch_size = 256 data_loaders = Data_Loaders(batch_size) model = Action_Conditioned_FF() model.to(device) loss_function = nn.BCEWithLogitsLoss() losses = [] min_loss = model.evaluate(model, data_loaders.test_loader, loss_function) losses.append(min_loss) learning_rate = 0.1 optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) for epoch_i in range(no_epochs): model.train() epoch_loss = 0 epoch_acc = 0 # sample['input'] and sample['label'] for idx, sample in enumerate(data_loaders.train_loader): inpt = sample['input'].to(device) labels = sample['label'].to(device) labels = labels.unsqueeze(1) optimizer.zero_grad() outputs = model(inpt) loss = loss_function(outputs, labels) loss.backward() optimizer.step() epoch_loss += loss.item() print(f'| Epoch: {epoch_i+1}', end=" | ") print(f'Loss: {epoch_loss/len(data_loaders.train_loader):.4f} |') #print(f'Acc: {epoch_acc/len(data_loaders.train_loader):.3f}') model.eval() with torch.no_grad(): test_loss = model.evaluate(model, data_loaders.test_loader, loss_function) print(f'------- Test Loss: {test_loss:.4f} -------') losses.append(test_loss) PATH = f"saved/weights/weights_{test_loss:.3f}.pkl" torch.save(model.state_dict(), PATH, _use_new_zipfile_serialization=False)
def train_model(no_epochs): batch_size = 16 data_loaders = Data_Loaders(batch_size) model = Action_Conditioned_FF() loss_function = torch.nn.BCEWithLogitsLoss() # loss_function = torch.nn.MSELoss() losses = [] min_loss = model.evaluate(model, data_loaders.test_loader, loss_function) losses.append(min_loss) # print(min_loss) optimizer = Adam(model.parameters(), lr=0.001) prev_loss = float('inf') for epoch_i in tqdm(range(no_epochs)): model.train() for idx, sample in enumerate(data_loaders.train_loader ): # sample['input'] and sample['label'] x, y = sample['input'], sample['label'] optimizer.zero_grad() y_hat = model.forward(x) # loss = loss_function(y_hat.unsqueeze(dim=0), y.long()) loss = loss_function(y_hat.reshape(1).float(), y.float()) loss.backward() optimizer.step() total_loss = model.evaluate(model, data_loaders.test_loader, loss_function) if total_loss < prev_loss: torch.save(model.state_dict(), "saved/saved_model.pkl", _use_new_zipfile_serialization=False) prev_loss = total_loss print() print(total_loss) losses.append( model.evaluate(model, data_loaders.test_loader, loss_function)) # print('e') torch.save(model.state_dict(), "saved/saved_model.pkl", _use_new_zipfile_serialization=False)
def train_model(no_epochs): batch_size = 16 data_loaders = Data_Loaders(batch_size) model = Action_Conditioned_FF() PATH = 'C:/Users/Kenji Mah/Desktop/assignment_part4/saved/saved_model.pkl' loss_function = nn.MSELoss() losses = [] min_loss = model.evaluate(model, data_loaders.test_loader, loss_function) losses.append(min_loss) summary(model) learning_rate = 0.01 optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) for epoch_i in range(no_epochs): for idx, sample in enumerate(data_loaders.train_loader): # output = model.forward(sample['input']) optimizer.zero_grad() output = model(torch.tensor(sample['input'], dtype=torch.float32)) loss = loss_function( output, torch.tensor(sample['label'], dtype=torch.float32)) loss.backward() optimizer.step() losses.append( model.evaluate(model, data_loaders.train_loader, loss_function)) #torch.save(model.state_dict(), PATH,_use_new_zipfile_serialization=False) test = model.evaluate(model, data_loaders.test_loader, loss_function) plt.plot(range(0, no_epochs + 1), losses) plt.title('Test Loss') plt.xlabel('Epochs') plt.ylabel('Loss (MSE)') plt.xticks(range(0, no_epochs + 1)) print(test)
def train_model(no_epochs): batch_size = 32 data_loaders = dl.Data_Loaders(batch_size) model = Action_Conditioned_FF() loss_fn = nn.BCELoss() optimizer = torch.optim.Adam(model.model.parameters(), lr=0.001) test_losses = [] train_losses = [] for epoch_i in range(no_epochs): #print(f"epoch # {epoch_i + 1}") model.train() loss_test = model.evaluate(model, data_loaders.test_loader, loss_fn) print('test') print(loss_test) test_losses.append(loss_test) l=[] for idx, sample in enumerate(data_loaders.train_loader): # sample['input'] and sample['label'] input, label = sample['input'].float(), sample['label'].float() #forward step out = model.forward(input) loss_train = loss_fn(out, label.view(-1,1)) l.append(loss_train) #backpropagation optimizer.zero_grad() loss_train.backward() optimizer.step() print('train') print(sum(l)/len(l)) train_losses.append(sum(l) / len(l)) torch.save(model.state_dict(), 'saved_model.pkl', _use_new_zipfile_serialization=False)