update to torch 0.4
This commit is contained in:
@ -3,11 +3,10 @@ View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/
|
||||
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
|
||||
|
||||
Dependencies:
|
||||
torch: 0.1.11
|
||||
torch: 0.4
|
||||
matplotlib
|
||||
"""
|
||||
import torch
|
||||
from torch.autograd import Variable
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# torch.manual_seed(1) # reproducible
|
||||
@ -18,12 +17,10 @@ N_HIDDEN = 300
|
||||
# training data
|
||||
x = torch.unsqueeze(torch.linspace(-1, 1, N_SAMPLES), 1)
|
||||
y = x + 0.3*torch.normal(torch.zeros(N_SAMPLES, 1), torch.ones(N_SAMPLES, 1))
|
||||
x, y = Variable(x), Variable(y)
|
||||
|
||||
# test data
|
||||
test_x = torch.unsqueeze(torch.linspace(-1, 1, N_SAMPLES), 1)
|
||||
test_y = test_x + 0.3*torch.normal(torch.zeros(N_SAMPLES, 1), torch.ones(N_SAMPLES, 1))
|
||||
test_x, test_y = Variable(test_x, volatile=True), Variable(test_y, volatile=True)
|
||||
|
||||
# show data
|
||||
plt.scatter(x.data.numpy(), y.data.numpy(), c='magenta', s=50, alpha=0.5, label='train')
|
||||
@ -85,8 +82,8 @@ for t in range(500):
|
||||
plt.scatter(test_x.data.numpy(), test_y.data.numpy(), c='cyan', s=50, alpha=0.3, label='test')
|
||||
plt.plot(test_x.data.numpy(), test_pred_ofit.data.numpy(), 'r-', lw=3, label='overfitting')
|
||||
plt.plot(test_x.data.numpy(), test_pred_drop.data.numpy(), 'b--', lw=3, label='dropout(50%)')
|
||||
plt.text(0, -1.2, 'overfitting loss=%.4f' % loss_func(test_pred_ofit, test_y).data[0], fontdict={'size': 20, 'color': 'red'})
|
||||
plt.text(0, -1.5, 'dropout loss=%.4f' % loss_func(test_pred_drop, test_y).data[0], fontdict={'size': 20, 'color': 'blue'})
|
||||
plt.text(0, -1.2, 'overfitting loss=%.4f' % loss_func(test_pred_ofit, test_y).data.numpy(), fontdict={'size': 20, 'color': 'red'})
|
||||
plt.text(0, -1.5, 'dropout loss=%.4f' % loss_func(test_pred_drop, test_y).data.numpy(), fontdict={'size': 20, 'color': 'blue'})
|
||||
plt.legend(loc='upper left'); plt.ylim((-2.5, 2.5));plt.pause(0.1)
|
||||
|
||||
# change back to train mode
|
||||
|
||||
Reference in New Issue
Block a user