Overview
This example demonstrates using pytorch to create a neural network that learns to recognize the digits 1,2,3,4,5,6,7,8,9. Each digit can be represented as a set of pixels, and the goal of the exercise is to train a machine learning algorithm to recognize each digit from its pixels.For this example, we will use simple representations of each digit, displayed below.
These digits are an obvious simplification. In fact, it is fairly easy to write a function that would recognize these characters without having to use machine learning, however, we use this simplification in order to easily demonstrate the concepts.
Representation
The digits are encoded as arrays of arrays. The following shows how the digit seven is encoded.
matrix7 = [
[1,1,1,1],
[0,0,0,1],
[0,0,1,0],
[0,1,0,0],
[1,0,0,0]
]
Processing the Data
inputs = []
outputs = []
for index,matrix in enumerate(sp.matrices):
output = [0.0 for x in range(9)]
output[index]=1.0
outputs.append(output)
input = [float(item) for sublist in matrix for item in sublist]
inputs.append(input)
pass
X = torch.tensor(inputs)
y = torch.tensor(outputs)
Defining the Model
network = nn.Sequential(
nn.Linear(20, 9),
nn.Tanh(),
nn.Linear(9,9),
nn.Sigmoid()
)
opt = op.SGD(network.parameters(), lr=0.01)
err = nn.CrossEntropyLoss()
Full Script
import lib.neural_network.torch as tc
import lib.neural_network.sample as sp
import torch
import torch.optim as op
import torch.nn as nn
inputs = []
outputs = []
for index,matrix in enumerate(sp.matrices):
output = [0.0 for x in range(9)]
output[index]=1.0
outputs.append(output)
input = [float(item) for sublist in matrix for item in sublist]
inputs.append(input)
pass
X = torch.tensor(inputs)
y = torch.tensor(outputs)
network = nn.Sequential(
nn.Linear(20, 9),
nn.Tanh(),
nn.Linear(9,9),
nn.Sigmoid()
)
opt = op.SGD(network.parameters(), lr=0.01)
err = nn.CrossEntropyLoss()
tc.train(network, opt, err, X, y, 100000)
test2 = network(X)
print(test2)
pass