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
import numpy as np
inputs = []
outputs = []
outputs2 = []
for index,matrix in enumerate(sp.matrices):
output = [0.0 for x in range(9)]
output[index]=1.0
outputs.append(output)
outputs2.append([index+1])
input = [float(item) for sublist in matrix for item in sublist]
inputs.append(input)
pass
X = np.array(inputs)
y = np.array(outputs)
y2 = np.array(outputs2)
Defining the Model
from sklearn.linear_model import SGDClassifier
from sklearn.multioutput import MultiOutputClassifier
sgd = SGDClassifier(loss="hinge")
sgd.fit(X,y2)
test = sgd.predict(inputs)
print(test)
One Hot Encoding
If you are using one hot encoding you need to wrap the model in a MultiOutputClassifier before running.
from sklearn.linear_model import SGDClassifier
from sklearn.multioutput import MultiOutputClassifier
sgd = SGDClassifier(loss="hinge")
clf = MultiOutputClassifier(sgd)
clf.fit(X,y)
test = clf.predict(inputs)
print(test)
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
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.multioutput import MultiOutputClassifier
inputs = []
outputs = []
outputs2 = []
for index,matrix in enumerate(sp.matrices):
output = [0.0 for x in range(9)]
output[index]=1.0
outputs.append(output)
outputs2.append([index+1])
input = [float(item) for sublist in matrix for item in sublist]
inputs.append(input)
pass
X = np.array(inputs)
y = np.array(outputs)
y2 = np.array(outputs2)
sgd = SGDClassifier(loss="hinge")
sgd.fit(X,y2)
test = sgd.predict(inputs)
print(test)
sgd = SGDClassifier(loss="hinge")
clf = MultiOutputClassifier(sgd)
clf.fit(X,y)
test = clf.predict(inputs)
print(test)
pass