Skip to content

Commit abee521

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 3e2d0b8 + 759aa01 commit abee521

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

tutorial-contents/401_CNN.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self):
6565
out_channels=16, # n_filters
6666
kernel_size=5, # filter size
6767
stride=1, # filter movement/step
68-
padding=2, # if want same width and length of this image after con2d, padding=(kernel_size-1)/2 if stride=1
68+
padding=2, # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1
6969
), # output shape (16, 28, 28)
7070
nn.ReLU(), # activation
7171
nn.MaxPool2d(kernel_size=2), # choose max value in 2x2 area, output shape (16, 14, 14)
@@ -115,7 +115,7 @@ def plot_with_labels(lowDWeights, labels):
115115

116116
if step % 50 == 0:
117117
test_output, last_layer = cnn(test_x)
118-
pred_y = torch.max(test_output, 1)[1].data.squeeze().numpy()
118+
pred_y = torch.max(test_output, 1)[1].data.numpy()
119119
accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
120120
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
121121
if HAS_SK:
@@ -129,6 +129,6 @@ def plot_with_labels(lowDWeights, labels):
129129

130130
# print 10 predictions from test data
131131
test_output, _ = cnn(test_x[:10])
132-
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
132+
pred_y = torch.max(test_output, 1)[1].data.numpy()
133133
print(pred_y, 'prediction number')
134134
print(test_y[:10].numpy(), 'real number')

tutorial-contents/402_RNN_classifier.py

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

5252

5353
class RNN(nn.Module):
@@ -94,13 +94,13 @@ def forward(self, x):
9494

9595
if step % 50 == 0:
9696
test_output = rnn(test_x) # (samples, time_step, input_size)
97-
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
97+
pred_y = torch.max(test_output, 1)[1].data.numpy()
9898
accuracy = float((pred_y == test_y).astype(int).sum()) / float(test_y.size)
9999
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
100100

101101
# print 10 predictions from test data
102102
test_output = rnn(test_x[:10].view(-1, 28, 28))
103-
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
103+
pred_y = torch.max(test_output, 1)[1].data.numpy()
104104
print(pred_y, 'prediction number')
105105
print(test_y[:10], 'real number')
106106

tutorial-contents/403_RNN_regressor.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
LR = 0.02 # learning rate
2121

2222
# show data
23-
steps = np.linspace(0, np.pi*2, 100, dtype=np.float32)
24-
x_np = np.sin(steps) # float32 for converting torch FloatTensor
23+
steps = np.linspace(0, np.pi*2, 100, dtype=np.float32) # float32 for converting torch FloatTensor
24+
x_np = np.sin(steps)
2525
y_np = np.cos(steps)
2626
plt.plot(steps, y_np, 'r-', label='target (cos)')
2727
plt.plot(steps, x_np, 'b-', label='input (sin)')
@@ -55,7 +55,13 @@ def forward(self, x, h_state):
5555
# instead, for simplicity, you can replace above codes by follows
5656
# r_out = r_out.view(-1, 32)
5757
# outs = self.out(r_out)
58+
# outs = outs.view(-1, TIME_STEP, 1)
5859
# return outs, h_state
60+
61+
# or even simpler, since nn.Linear can accept inputs of any dimension
62+
# and returns outputs with same dimension except for the last
63+
# outs = self.out(r_out)
64+
# return outs
5965

6066
rnn = RNN()
6167
print(rnn)
@@ -71,8 +77,8 @@ def forward(self, x, h_state):
7177
for step in range(100):
7278
start, end = step * np.pi, (step+1)*np.pi # time range
7379
# use sin predicts cos
74-
steps = np.linspace(start, end, TIME_STEP, dtype=np.float32)
75-
x_np = np.sin(steps) # float32 for converting torch FloatTensor
80+
steps = np.linspace(start, end, TIME_STEP, dtype=np.float32) # float32 for converting torch FloatTensor
81+
x_np = np.sin(steps)
7682
y_np = np.cos(steps)
7783

7884
x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis]) # shape (batch, time_step, input_size)

tutorial-contents/502_GPU.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def forward(self, x):
6868
test_output = cnn(test_x)
6969

7070
# !!!!!!!! Change in here !!!!!!!!! #
71-
pred_y = torch.max(test_output, 1)[1].cuda().data.squeeze() # move the computation in GPU
71+
pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU
7272

7373
accuracy = torch.sum(pred_y == test_y).type(torch.FloatTensor) / test_y.size(0)
7474
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.cpu().numpy(), '| test accuracy: %.2f' % accuracy)
@@ -77,7 +77,7 @@ def forward(self, x):
7777
test_output = cnn(test_x[:10])
7878

7979
# !!!!!!!! Change in here !!!!!!!!! #
80-
pred_y = torch.max(test_output, 1)[1].cuda().data.squeeze() # move the computation in GPU
80+
pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU
8181

8282
print(pred_y, 'prediction number')
8383
print(test_y[:10], 'real number')

0 commit comments

Comments
 (0)