From a9ef65ed9ac8ea1786425b8ea99060793162a212 Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Fri, 9 Nov 2018 17:26:27 +0800 Subject: [PATCH 1/6] [Bug fix] 401_CNN.py 1. fix typo 2. remove .squeeze for 1-D tensor --- tutorial-contents/401_CNN.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tutorial-contents/401_CNN.py b/tutorial-contents/401_CNN.py index 1f9daea..8016158 100644 --- a/tutorial-contents/401_CNN.py +++ b/tutorial-contents/401_CNN.py @@ -65,7 +65,7 @@ class CNN(nn.Module): out_channels=16, # n_filters kernel_size=5, # filter size stride=1, # filter movement/step - padding=2, # if want same width and length of this image after con2d, padding=(kernel_size-1)/2 if stride=1 + padding=2, # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1 ), # output shape (16, 28, 28) nn.ReLU(), # activation nn.MaxPool2d(kernel_size=2), # choose max value in 2x2 area, output shape (16, 14, 14) @@ -115,7 +115,7 @@ for epoch in range(EPOCH): if step % 50 == 0: test_output, last_layer = cnn(test_x) - pred_y = torch.max(test_output, 1)[1].data.squeeze().numpy() + pred_y = torch.max(test_output, 1)[1].data.numpy() accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0)) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy) if HAS_SK: @@ -129,6 +129,6 @@ plt.ioff() # print 10 predictions from test data test_output, _ = cnn(test_x[:10]) -pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() +pred_y = torch.max(test_output, 1)[1].data.numpy() print(pred_y, 'prediction number') print(test_y[:10].numpy(), 'real number') From 2748b02bd64343ddbef52c083250b4f045a1abf0 Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Mon, 12 Nov 2018 14:18:49 +0800 Subject: [PATCH 2/6] fill up the missing view() in forward() in comment --- tutorial-contents/403_RNN_regressor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tutorial-contents/403_RNN_regressor.py b/tutorial-contents/403_RNN_regressor.py index c8b19ca..4a959a6 100644 --- a/tutorial-contents/403_RNN_regressor.py +++ b/tutorial-contents/403_RNN_regressor.py @@ -55,6 +55,7 @@ class RNN(nn.Module): # instead, for simplicity, you can replace above codes by follows # r_out = r_out.view(-1, 32) # outs = self.out(r_out) + # outs = outs.view(-1, TIME_STEP, 1) # return outs, h_state rnn = RNN() From 61d6dc6757bb7db7ef45d765efdcf88cdd227b4c Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Mon, 12 Nov 2018 14:24:09 +0800 Subject: [PATCH 3/6] supplement another method --- tutorial-contents/403_RNN_regressor.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tutorial-contents/403_RNN_regressor.py b/tutorial-contents/403_RNN_regressor.py index 4a959a6..2010ded 100644 --- a/tutorial-contents/403_RNN_regressor.py +++ b/tutorial-contents/403_RNN_regressor.py @@ -57,6 +57,11 @@ class RNN(nn.Module): # outs = self.out(r_out) # outs = outs.view(-1, TIME_STEP, 1) # return outs, h_state + + # or even simpler, since nn.Linear can accept inputs of any dimension + # and returns outputs with same dimension except for the last + # outs = self.out(r_out) + # return outs rnn = RNN() print(rnn) From 7411f602fb7786eb6551952547ac0e0436437068 Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Tue, 13 Nov 2018 09:13:44 +0800 Subject: [PATCH 4/6] 402 - remove squeeze for 1-D array --- tutorial-contents/402_RNN_classifier.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tutorial-contents/402_RNN_classifier.py b/tutorial-contents/402_RNN_classifier.py index 9ef11f7..3bb8231 100644 --- a/tutorial-contents/402_RNN_classifier.py +++ b/tutorial-contents/402_RNN_classifier.py @@ -47,7 +47,7 @@ train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_ # convert test data into Variable, pick 2000 samples to speed up testing test_data = dsets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor()) test_x = test_data.test_data.type(torch.FloatTensor)[:2000]/255. # shape (2000, 28, 28) value in range(0,1) -test_y = test_data.test_labels.numpy().squeeze()[:2000] # covert to numpy array +test_y = test_data.test_labels.numpy()[:2000] # covert to numpy array class RNN(nn.Module): @@ -94,13 +94,13 @@ for epoch in range(EPOCH): if step % 50 == 0: test_output = rnn(test_x) # (samples, time_step, input_size) - pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() + pred_y = torch.max(test_output, 1)[1].data.numpy() accuracy = float((pred_y == test_y).astype(int).sum()) / float(test_y.size) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy) # print 10 predictions from test data test_output = rnn(test_x[:10].view(-1, 28, 28)) -pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() +pred_y = torch.max(test_output, 1)[1].data.numpy() print(pred_y, 'prediction number') print(test_y[:10], 'real number') From 256559c12ae58fefb73810af94f357f609ded5db Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Tue, 13 Nov 2018 09:34:19 +0800 Subject: [PATCH 5/6] 403 - move the comment to right place --- tutorial-contents/403_RNN_regressor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tutorial-contents/403_RNN_regressor.py b/tutorial-contents/403_RNN_regressor.py index c8b19ca..5ef4666 100644 --- a/tutorial-contents/403_RNN_regressor.py +++ b/tutorial-contents/403_RNN_regressor.py @@ -20,8 +20,8 @@ INPUT_SIZE = 1 # rnn input size LR = 0.02 # learning rate # show data -steps = np.linspace(0, np.pi*2, 100, dtype=np.float32) -x_np = np.sin(steps) # float32 for converting torch FloatTensor +steps = np.linspace(0, np.pi*2, 100, dtype=np.float32) # float32 for converting torch FloatTensor +x_np = np.sin(steps) y_np = np.cos(steps) plt.plot(steps, y_np, 'r-', label='target (cos)') plt.plot(steps, x_np, 'b-', label='input (sin)') @@ -71,8 +71,8 @@ plt.ion() # continuously plot for step in range(100): start, end = step * np.pi, (step+1)*np.pi # time range # use sin predicts cos - steps = np.linspace(start, end, TIME_STEP, dtype=np.float32) - x_np = np.sin(steps) # float32 for converting torch FloatTensor + steps = np.linspace(start, end, TIME_STEP, dtype=np.float32) # float32 for converting torch FloatTensor + x_np = np.sin(steps) y_np = np.cos(steps) x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis]) # shape (batch, time_step, input_size) From 5d6d4f056e359beee64d22c53cbd76e95e67b45a Mon Sep 17 00:00:00 2001 From: keineahnung2345 Date: Tue, 13 Nov 2018 10:05:44 +0800 Subject: [PATCH 6/6] 502 - remove squeeze for 1-D tensor --- tutorial-contents/502_GPU.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorial-contents/502_GPU.py b/tutorial-contents/502_GPU.py index 58cd07b..383e5ba 100644 --- a/tutorial-contents/502_GPU.py +++ b/tutorial-contents/502_GPU.py @@ -68,7 +68,7 @@ for epoch in range(EPOCH): test_output = cnn(test_x) # !!!!!!!! Change in here !!!!!!!!! # - pred_y = torch.max(test_output, 1)[1].cuda().data.squeeze() # move the computation in GPU + pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU accuracy = torch.sum(pred_y == test_y).type(torch.FloatTensor) / test_y.size(0) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.cpu().numpy(), '| test accuracy: %.2f' % accuracy) @@ -77,7 +77,7 @@ for epoch in range(EPOCH): test_output = cnn(test_x[:10]) # !!!!!!!! Change in here !!!!!!!!! # -pred_y = torch.max(test_output, 1)[1].cuda().data.squeeze() # move the computation in GPU +pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU print(pred_y, 'prediction number') print(test_y[:10], 'real number')