fix bug for windows

This commit is contained in:
Morvan Zhou
2018-05-04 11:39:52 +10:00
parent 46c516fc10
commit 68396f5972

View File

@ -43,25 +43,26 @@ class Net(torch.nn.Module):
x = self.predict(x) # linear output x = self.predict(x) # linear output
return x return x
# different nets if __name__ == '__main__':
net_SGD = Net() # different nets
net_Momentum = Net() net_SGD = Net()
net_RMSprop = Net() net_Momentum = Net()
net_Adam = Net() net_RMSprop = Net()
nets = [net_SGD, net_Momentum, net_RMSprop, net_Adam] net_Adam = Net()
nets = [net_SGD, net_Momentum, net_RMSprop, net_Adam]
# different optimizers # different optimizers
opt_SGD = torch.optim.SGD(net_SGD.parameters(), lr=LR) opt_SGD = torch.optim.SGD(net_SGD.parameters(), lr=LR)
opt_Momentum = torch.optim.SGD(net_Momentum.parameters(), lr=LR, momentum=0.8) opt_Momentum = torch.optim.SGD(net_Momentum.parameters(), lr=LR, momentum=0.8)
opt_RMSprop = torch.optim.RMSprop(net_RMSprop.parameters(), lr=LR, alpha=0.9) opt_RMSprop = torch.optim.RMSprop(net_RMSprop.parameters(), lr=LR, alpha=0.9)
opt_Adam = torch.optim.Adam(net_Adam.parameters(), lr=LR, betas=(0.9, 0.99)) opt_Adam = torch.optim.Adam(net_Adam.parameters(), lr=LR, betas=(0.9, 0.99))
optimizers = [opt_SGD, opt_Momentum, opt_RMSprop, opt_Adam] optimizers = [opt_SGD, opt_Momentum, opt_RMSprop, opt_Adam]
loss_func = torch.nn.MSELoss() loss_func = torch.nn.MSELoss()
losses_his = [[], [], [], []] # record loss losses_his = [[], [], [], []] # record loss
# training # training
for epoch in range(EPOCH): for epoch in range(EPOCH):
print('Epoch: ', epoch) print('Epoch: ', epoch)
for step, (batch_x, batch_y) in enumerate(loader): # for each training step for step, (batch_x, batch_y) in enumerate(loader): # for each training step
b_x = Variable(batch_x) b_x = Variable(batch_x)
@ -75,11 +76,11 @@ for epoch in range(EPOCH):
opt.step() # apply gradients opt.step() # apply gradients
l_his.append(loss.data[0]) # loss recoder l_his.append(loss.data[0]) # loss recoder
labels = ['SGD', 'Momentum', 'RMSprop', 'Adam'] labels = ['SGD', 'Momentum', 'RMSprop', 'Adam']
for i, l_his in enumerate(losses_his): for i, l_his in enumerate(losses_his):
plt.plot(l_his, label=labels[i]) plt.plot(l_his, label=labels[i])
plt.legend(loc='best') plt.legend(loc='best')
plt.xlabel('Steps') plt.xlabel('Steps')
plt.ylabel('Loss') plt.ylabel('Loss')
plt.ylim((0, 0.2)) plt.ylim((0, 0.2))
plt.show() plt.show()