## Low-level Programming with PyTorch¶

In [1]:
import torch
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

def get_test(M):
batch_xs = np.zeros((0, 28*28))
batch_y_s = np.zeros( (0, 10))

test_k =  ["test"+str(i) for i in range(10)]
for k in range(10):
batch_xs = np.vstack((batch_xs, ((np.array(M[test_k[k]])[:])/255.)  ))
one_hot = np.zeros(10)
one_hot[k] = 1
batch_y_s = np.vstack((batch_y_s,   np.tile(one_hot, (len(M[test_k[k]]), 1))   ))
return batch_xs, batch_y_s

def get_train(M):
batch_xs = np.zeros((0, 28*28))
batch_y_s = np.zeros( (0, 10))

train_k =  ["train"+str(i) for i in range(10)]
for k in range(10):
batch_xs = np.vstack((batch_xs, ((np.array(M[train_k[k]])[:])/255.)  ))
one_hot = np.zeros(10)
one_hot[k] = 1
batch_y_s = np.vstack((batch_y_s,   np.tile(one_hot, (len(M[train_k[k]]), 1))   ))
return batch_xs, batch_y_s

train_x, train_y = get_train(M)
test_x, test_y = get_test(M)

dim_x = 28*28
dim_h = 20
dim_out = 10

Let's display some of the training data

In [2]:
plt.imshow(train_x[100].reshape((28,28)), cmap = plt.cm.gray)
Out[2]:
<matplotlib.image.AxesImage at 0x7f76b44c6610>
In [3]:
train_y[100]
Out[3]:
array([ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
In [4]:
plt.imshow(train_x[101].reshape((28,28)), cmap = plt.cm.gray)
Out[4]:
<matplotlib.image.AxesImage at 0x7f76b43c0950>
In [5]:
train_y[100]
Out[5]:
array([ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
In [6]:
plt.imshow(train_x[50000].reshape((28,28)), cmap = plt.cm.gray)
Out[6]:
<matplotlib.image.AxesImage at 0x7f76b3e94810>
In [7]:
train_y[50000]
Out[7]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.])

Now, let's define Variables containing the training data

In [8]:
dtype_float = torch.FloatTensor

In [9]:

Note that everything is accessible right away:

Let's now define the the model. Note that since we'll want to reuse it for different inputs, we'll want it to be in a function (or really in a class -- we'll show how to do that later). First, we'll remind ourselves of the dimensions of the data

In [10]:
x.data.shape, b0.data.shape, W0.data.shape
Out[10]:
(torch.Size([60000, 784]), torch.Size([1, 20]), torch.Size([784, 20]))
In [11]:
b1.data.shape, W1.data.shape, y.data.shape
Out[11]:
(torch.Size([1, 10]), torch.Size([20, 10]), torch.Size([60000, 10]))
In [12]:
def model(x, b0, W0, b1, W1):
h = torch.nn.ReLU()(torch.matmul(x, W0) + b0.repeat(x.data.shape[0], 1))
out = torch.matmul(h, W1) + b1.repeat(h.data.shape[0], 1)
return out
In [13]:
y_out = model(x, b0, W0, b1, W1)
In [ ]:

In [ ]:

In [14]:
################################################################################
#Subsample the training set for faster training

train_idx = np.random.permutation(range(train_x.shape[0]))[:1000]
y_out = model(x, b0, W0, b1, W1)
#################################################################################
In [15]:
logSoftMax = torch.nn.LogSoftmax() # We'll be too lazy to define this one by hand
loss = -torch.mean(torch.sum(y * logSoftMax(y_out), 1))
In [16]:
loss
Out[16]:
Variable containing:
48.6093
[torch.FloatTensor of size 1]
In [17]:
learning_rate = 1e-1

for t in range(1000):
y_out = model(x, b0, W0, b1, W1)
loss = -torch.mean(torch.sum(y * logSoftMax(y_out), 1))
loss.backward()

#print(loss.data.numpy())
In [18]:
In [19]:
y_test_out = model(x_test_all_var, b0, W0, b1, W1).data.numpy()
In [20]:
np.argmax(y_test_out, 1)
Out[20]:
array([0, 3, 0, ..., 4, 9, 4])
In [21]:
np.argmax(y_test_out[:100], 1)
Out[21]:
array([0, 3, 0, 0, 0, 8, 1, 0, 3, 3, 8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0,
0, 0, 0, 0, 9, 0, 0, 0, 3, 0, 0, 3, 0, 8, 3, 8, 0, 0, 4, 3, 5, 8, 0,
3, 9, 2, 3, 0, 0, 1, 0, 3, 0, 0, 0, 0, 3, 0, 5, 9, 4, 0, 4, 0, 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