Last active
July 25, 2019 02:40
-
-
Save heffo42/fa8fe31004f182b4c64d58cb1c761a21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Build the model and add to graphics card | |
model = ModelRNNBasic().cuda() | |
# Define Loss, Optimizer | |
lr=1e-3 | |
opt = torch.optim.Adam(model.parameters(), lr=lr) | |
def loss_func(input,target): | |
return F.cross_entropy(input.permute(0,2,1), target) | |
losses = [] | |
accuracy = [] | |
#each epoch | |
n_epochs = 10 | |
for epoch in range(1,n_epochs+1): | |
data = DataLM(train_list,bptt=70) | |
#each batch | |
for x,y in data: | |
#opt = torch.optim.Adam(model.parameters(), lr) | |
y_hat = model(x) | |
loss = loss_func(y_hat, y) | |
loss.backward() | |
opt.step() | |
opt.zero_grad() | |
_, predicted = torch.max(y_hat[:,-1,:], 1) | |
correct = (predicted == y[:,-1]).sum().item() | |
losses.append(loss.item()) | |
accuracy.append(correct / 64) | |
print('Epoch: {:2}/{} | Loss: {:.4f} | Accuracy: {:.4f}'.format(epoch, n_epochs,np.mean(losses[-3:]),np.mean(accuracy[-3:]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment