Skip to content

Commit

Permalink
feat: trained first lstm model
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepLearning VM committed Feb 17, 2020
1 parent 987885b commit 21aa3ab
Show file tree
Hide file tree
Showing 2 changed files with 1,940 additions and 1,990 deletions.
18 changes: 9 additions & 9 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def __init__(self,input_size, hidden_size, num_layers, cuda=False):
hidden_size=hidden_size,
num_layers=num_layers,
batch_first= True,
dropout = 0.2)
self.drop1 = nn.Dropout(p=0.2)
dropout = 0)

self.fc2 = nn.Linear(hidden_size, 50)
self.drop2 = nn.Dropout(p=0.2)
Expand All @@ -43,10 +42,10 @@ def init_hidden(self, batch_size):

def forward(self, x):
self.hidden = self.init_hidden(x.shape[0])
x = x.unsqueeze(1)
# x = x.unsqueeze(1)

x, self.hidden = self.lstm1(x, self.hidden)
x = self.drop1(x)
# x = self.drop1(x)

x = self.drop2(F.relu(self.fc2(x)))

Expand Down Expand Up @@ -74,8 +73,9 @@ def fit(self, trainloader, validationloader, loss, optim, lr, epochs, val_per_ba
self.optimizer.zero_grad()

output = self.forward(data)
loss = self.criterion(output, target.unsqueeze(1))

# loss = self.criterion(output.view(-1, 1), target.view(-1))
loss = self.criterion(output, target)

loss.backward()
self.optimizer.step()

Expand All @@ -91,7 +91,7 @@ def fit(self, trainloader, validationloader, loss, optim, lr, epochs, val_per_ba
if self.cuda:
data, target = data.cuda(), target.cuda()
ps = self.forward(data)
testloss += self.criterion(ps, target.unsqueeze(1)).item()
testloss += self.criterion(ps, target).item()
testloss = testloss / len(validationloader)
trainloss = trainloss / len(trainloader)

Expand Down Expand Up @@ -132,10 +132,10 @@ def save(self, path, save_optim=False):
print(path, " - dir Already exists")
finally:
NAME = 'model_cuda.pt' if self.cuda else 'model_cpu.pt'
torch.save(self.state_dict(), os.path.join(path, NAME))
torch.save(self.state_dict(), os.path.join('model', path, NAME))
print('Model saved in', path)
if save_optim:
NAME = 'optim_cuda.pt' if self.cuda else 'optim_cpu.pt'
torch.save(self.optimizer.state_dict(), os.path.join(path, NAME))
torch.save(self.optimizer.state_dict(), os.path.join('model', path, NAME))
print('Optimizer saved in', path)
return
Loading

0 comments on commit 21aa3ab

Please sign in to comment.