Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebc3279

Browse files
committed
edit
1 parent 5619330 commit ebc3279

10 files changed

Lines changed: 345 additions & 19 deletions

pytorchTUT/301_regression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1)
1818

1919
# torch can only train on Variable, so convert them to Variable
20-
x, y = Variable(x, requires_grad=False), Variable(y, requires_grad=False)
20+
x, y = Variable(x), Variable(y)
2121

2222
# plt.scatter(x.data.numpy(), y.data.numpy())
2323
# plt.show()

pytorchTUT/302_classification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
y = torch.cat((y0, y1), ).type(torch.LongTensor) # LongTensor = 64-bit integer
2424

2525
# torch can only train on Variable, so convert them to Variable
26-
x, y = Variable(x, requires_grad=False), Variable(y, requires_grad=False)
26+
x, y = Variable(x), Variable(y)
2727

2828
# plt.scatter(x.data.numpy(), y.data.numpy())
2929
# plt.show()

pytorchTUT/401_CNN.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
# convert test data into Variable, pick 2000 samples to speed up testing
4646
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
47-
test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1)).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
47+
test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1), volatile=True).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
4848
test_y = test_data.test_labels[:2000]
4949

5050

pytorchTUT/402_RNN_classifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
# convert test data into Variable, pick 2000 samples to speed up testing
4949
test_data = dsets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor())
50-
test_x = Variable(test_data.test_data).type(torch.FloatTensor)[:2000]/255. # shape (2000, 28, 28) value in range(0,1)
50+
test_x = Variable(test_data.test_data, volatile=True).type(torch.FloatTensor)[:2000]/255. # shape (2000, 28, 28) value in range(0,1)
5151
test_y = test_data.test_labels.numpy().squeeze()[:2000] # covert to numpy array
5252

5353

pytorchTUT/404_autoencoder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ def forward(self, x):
120120
plt.draw()
121121
plt.pause(0.05)
122122

123-
plt.savefig('4-4-4.png')
124123
plt.ioff()
125124
plt.show()
126125

pytorchTUT/405_DQN_Reinforcement_learning.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def learn(self):
9494

9595
dqn = DQN()
9696

97+
print('\nCollecting experience...')
9798
for i_episode in range(400):
9899
s = env.reset()
99100
ep_r = 0
@@ -117,11 +118,12 @@ def learn(self):
117118
ep_r += r
118119
if dqn.memory_counter > MEMORY_CAPACITY:
119120
dqn.learn()
121+
if done:
122+
print('Ep: ', i_episode,
123+
'| Ep_r: ', round(ep_r, 2),
124+
)
120125

121126
if done:
122-
print('Ep: ', i_episode,
123-
'| Ep_r: ', round(ep_r, 2),
124-
)
125127
break
126128

127129
s = s_

pytorchTUT/503_dropout.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
# training data
1919
x = torch.unsqueeze(torch.linspace(-1, 1, N_SAMPLES), 1)
2020
y = x + 0.3*torch.normal(torch.zeros(N_SAMPLES, 1), torch.ones(N_SAMPLES, 1))
21-
x, y = Variable(x, requires_grad=False), Variable(y, requires_grad=False)
21+
x, y = Variable(x), Variable(y)
2222

2323
# test data
2424
test_x = torch.unsqueeze(torch.linspace(-1, 1, N_SAMPLES), 1)
2525
test_y = test_x + 0.3*torch.normal(torch.zeros(N_SAMPLES, 1), torch.ones(N_SAMPLES, 1))
26-
test_x, test_y = Variable(test_x, requires_grad=False), Variable(test_y, requires_grad=False)
26+
test_x, test_y = Variable(test_x, volatile=True), Variable(test_y, volatile=True)
2727

2828
# show data
2929
plt.scatter(x.data.numpy(), y.data.numpy(), c='magenta', s=50, alpha=0.5, label='train')
@@ -55,18 +55,16 @@
5555

5656
optimizer_ofit = torch.optim.Adam(net_overfitting.parameters(), lr=0.01)
5757
optimizer_drop = torch.optim.Adam(net_dropped.parameters(), lr=0.01)
58-
loss_func_ofit = torch.nn.MSELoss()
59-
loss_func_drop = torch.nn.MSELoss()
58+
loss_func = torch.nn.MSELoss()
6059

6160
plt.ion() # something about plotting
6261
plt.show()
6362

6463
for t in range(500):
6564
pred_ofit = net_overfitting(x)
6665
pred_drop = net_dropped(x)
67-
68-
loss_ofit = loss_func_ofit(pred_ofit, y)
69-
loss_drop = loss_func_drop(pred_drop, y)
66+
loss_ofit = loss_func(pred_ofit, y)
67+
loss_drop = loss_func(pred_drop, y)
7068

7169
optimizer_ofit.zero_grad()
7270
optimizer_drop.zero_grad()
@@ -76,9 +74,9 @@
7674
optimizer_drop.step()
7775

7876
if t % 10 == 0:
79-
# change to eval mode
77+
# change to eval mode in order to fix drop out effect
8078
net_overfitting.eval()
81-
net_dropped.eval()
79+
net_dropped.eval() # parameters for dropout differ from train mode
8280

8381
# plotting
8482
plt.cla()
@@ -88,8 +86,8 @@
8886
plt.scatter(test_x.data.numpy(), test_y.data.numpy(), c='cyan', s=50, alpha=0.3, label='test')
8987
plt.plot(test_x.data.numpy(), test_pred_ofit.data.numpy(), 'r-', lw=3, label='overfitting')
9088
plt.plot(test_x.data.numpy(), test_pred_drop.data.numpy(), 'b--', lw=3, label='dropout(50%)')
91-
plt.text(0, -1.2, 'overfitting loss=%.4f' % loss_func_ofit(test_pred_ofit, test_y).data[0], fontdict={'size': 20, 'color': 'red'})
92-
plt.text(0, -1.5, 'dropout loss=%.4f' % loss_func_drop(test_pred_drop, test_y).data[0], fontdict={'size': 20, 'color': 'blue'})
89+
plt.text(0, -1.2, 'overfitting loss=%.4f' % loss_func(test_pred_ofit, test_y).data[0], fontdict={'size': 20, 'color': 'red'})
90+
plt.text(0, -1.5, 'dropout loss=%.4f' % loss_func(test_pred_drop, test_y).data[0], fontdict={'size': 20, 'color': 'blue'})
9391
plt.legend(loc='upper left')
9492
plt.ylim((-2.5, 2.5))
9593
plt.pause(0.1)
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
"""
2+
Know more, visit 莫烦Python: https://morvanzhou.github.io/tutorials/
3+
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
4+
5+
Dependencies:
6+
torch: 0.1.11
7+
matplotlib
8+
numpy
9+
"""
10+
import torch
11+
from torch.autograd import Variable
12+
from torch import nn
13+
from torch.nn import init
14+
import torch.utils.data as Data
15+
import torch.nn.functional as F
16+
import matplotlib.pyplot as plt
17+
import numpy as np
18+
19+
torch.manual_seed(1) # reproducible
20+
np.random.seed(1)
21+
22+
# Hyper parameters
23+
N_SAMPLES = 2000
24+
BATCH_SIZE = 64
25+
EPOCH = 12
26+
LR = 0.03
27+
N_HIDDEN = 8
28+
ACTIVATION = F.tanh
29+
B_INIT = -0.2 # use a bad bias constant initializer
30+
31+
# training data
32+
x = np.linspace(-7, 10, N_SAMPLES)[:, np.newaxis]
33+
noise = np.random.normal(0, 2, x.shape)
34+
y = np.square(x) - 5 + noise
35+
36+
# test data
37+
test_x = np.linspace(-7, 10, 200)[:, np.newaxis]
38+
noise = np.random.normal(0, 2, test_x.shape)
39+
test_y = np.square(test_x) - 5 + noise
40+
41+
train_x, train_y = torch.from_numpy(x).float(), torch.from_numpy(y).float()
42+
test_x = Variable(torch.from_numpy(test_x).float(), volatile=True) # not for computing gradients
43+
test_y = Variable(torch.from_numpy(test_y).float(), volatile=True)
44+
45+
train_dataset = Data.TensorDataset(data_tensor=train_x, target_tensor=train_y)
46+
train_loader = Data.DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2,)
47+
48+
# show data
49+
plt.scatter(train_x.numpy(), train_y.numpy(), c='#FF9359', s=50, alpha=0.2, label='train')
50+
plt.legend(loc='upper left')
51+
plt.show()
52+
53+
class Net(nn.Module):
54+
def __init__(self, batch_normalization=False):
55+
super(Net, self).__init__()
56+
self.do_bn = batch_normalization
57+
self.fcs = []
58+
self.bns = []
59+
self.bn_input = nn.BatchNorm1d(1, momentum=0.5) # for input data
60+
61+
for i in range(N_HIDDEN): # build hidden layers and BN layers
62+
input_size = 1 if i == 0 else 10
63+
fc = nn.Linear(input_size, 10)
64+
setattr(self, 'fc%i' % i, fc) # IMPORTANT set layer to the Module
65+
self._set_init(fc) # parameters initialization
66+
self.fcs.append(fc)
67+
if self.do_bn:
68+
bn = nn.BatchNorm1d(10, momentum=0.5)
69+
setattr(self, 'bn%i' % i, bn) # IMPORTANT set layer to the Module
70+
self.bns.append(bn)
71+
72+
self.predict = nn.Linear(10, 1) # output layer
73+
self._set_init(self.predict) # parameters initialization
74+
75+
def _set_init(self, layer):
76+
init.normal(layer.weight, mean=0., std=.1)
77+
init.constant(layer.bias, B_INIT)
78+
79+
def forward(self, x):
80+
pre_activation = [x]
81+
if self.do_bn: x = self.bn_input(x) # input batch normalization
82+
layer_input = [x]
83+
for i in range(N_HIDDEN):
84+
x = self.fcs[i](x)
85+
pre_activation.append(x)
86+
if self.do_bn: x = self.bns[i](x) # batch normalization
87+
x = ACTIVATION(x)
88+
layer_input.append(x)
89+
out = self.predict(x)
90+
return out, layer_input, pre_activation
91+
92+
nets = [Net(batch_normalization=False), Net(batch_normalization=True)]
93+
94+
print(*nets) # print net architecture
95+
96+
opts = [torch.optim.Adam(net.parameters(), lr=LR) for net in nets]
97+
98+
loss_func = torch.nn.MSELoss()
99+
100+
f, axs = plt.subplots(4, N_HIDDEN+1, figsize=(10, 5))
101+
plt.ion() # something about plotting
102+
plt.show()
103+
104+
def plot_histogram(l_in, l_in_bn, pre_ac, pre_ac_bn):
105+
for i, (ax_pa, ax_pa_bn, ax, ax_bn) in enumerate(zip(axs[0, :], axs[1, :], axs[2, :], axs[3, :])):
106+
[a.clear() for a in [ax_pa, ax_pa_bn, ax, ax_bn]]
107+
if i == 0:
108+
p_range = (-7, 10)
109+
the_range = (-7, 10)
110+
else:
111+
p_range = (-4, 4)
112+
the_range = (-1, 1)
113+
ax_pa.set_title('L' + str(i))
114+
ax_pa.hist(pre_ac[i].data.numpy().ravel(), bins=10, range=p_range, color='#FF9359', alpha=0.5)
115+
ax_pa_bn.hist(pre_ac_bn[i].data.numpy().ravel(), bins=10, range=p_range, color='#74BCFF', alpha=0.5)
116+
ax.hist(l_in[i].data.numpy().ravel(), bins=10, range=the_range, color='#FF9359')
117+
ax_bn.hist(l_in_bn[i].data.numpy().ravel(), bins=10, range=the_range, color='#74BCFF')
118+
for a in [ax_pa, ax, ax_pa_bn, ax_bn]:
119+
a.set_yticks(())
120+
a.set_xticks(())
121+
ax_pa_bn.set_xticks(p_range)
122+
ax_bn.set_xticks(the_range)
123+
axs[0, 0].set_ylabel('PreAct')
124+
axs[1, 0].set_ylabel('BN PreAct')
125+
axs[2, 0].set_ylabel('Act')
126+
axs[3, 0].set_ylabel('BN Act')
127+
plt.pause(0.01)
128+
129+
# training
130+
losses = [[], []] # recode loss for two networks
131+
for epoch in range(EPOCH):
132+
print('Epoch: ', epoch)
133+
layer_inputs, pre_acts = [], []
134+
for net, l in zip(nets, losses):
135+
net.eval() # set eval mode to fix moving_mean and moving_var
136+
pred, layer_input, pre_act = net(test_x)
137+
l.append(loss_func(pred, test_y).data[0])
138+
layer_inputs.append(layer_input)
139+
pre_acts.append(pre_act)
140+
net.train() # free moving_mean and moving_var
141+
plot_histogram(*layer_inputs, *pre_acts) # plot histogram
142+
143+
for step, (b_x, b_y) in enumerate(train_loader):
144+
b_x, b_y = Variable(b_x), Variable(b_y)
145+
for net, opt in zip(nets, opts): # train for each network
146+
pred, _, _ = net(b_x)
147+
loss = loss_func(pred, b_y)
148+
opt.zero_grad()
149+
loss.backward()
150+
opt.step() # it will also learn the parameters in Batch Normalization
151+
152+
153+
plt.ioff()
154+
155+
# plot training loss
156+
plt.figure(2)
157+
plt.plot(losses[0], c='#FF9359', lw=3, label='Original')
158+
plt.plot(losses[1], c='#74BCFF', lw=3, label='Batch Normalization')
159+
plt.xlabel('step')
160+
plt.ylabel('test loss')
161+
plt.ylim((0, 2000))
162+
plt.legend(loc='best')
163+
164+
# evaluation
165+
# set net to eval mode to freeze the parameters in batch normalization layers
166+
[net.eval() for net in nets] # set eval mode to fix moving_mean and moving_var
167+
preds = [net(test_x)[0] for net in nets]
168+
plt.figure(3)
169+
plt.plot(test_x.data.numpy(), preds[0].data.numpy(), c='#FF9359', lw=4, label='Original')
170+
plt.plot(test_x.data.numpy(), preds[1].data.numpy(), c='#74BCFF', lw=4, label='Batch Normalization')
171+
plt.scatter(test_x.data.numpy(), test_y.data.numpy(), c='r', s=50, alpha=0.2, label='train')
172+
plt.legend(loc='best')
173+
plt.show()

pytorchTUT/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ If you speak Chinese, you can watch my [Youtube channel](https://www.youtube.com
3737
* [Why torch dynamic](https://github.com/MorvanZhou/tutorials/blob/master/pytorchTUT/501_why_torch_dynamic_graph.py)
3838
* [Train on GPU](https://github.com/MorvanZhou/tutorials/blob/master/pytorchTUT/502_GPU.py)
3939
* [Dropout](https://github.com/MorvanZhou/tutorials/blob/master/pytorchTUT/503_dropout.py)
40+
* [Batch Normalization](https://github.com/MorvanZhou/tutorials/blob/master/pytorchTUT/504_batch_normalization.py)
4041

4142
### [Regression](https://github.com/MorvanZhou/tutorials/blob/master/pytorchTUT/301_regression.py)
4243

0 commit comments

Comments
 (0)