In [1]:
import theano
from theano import tensor as T
import numpy as np
from load import mnist # mnist function from load.py
# using unzipped files from http://yann.lecun.com/exdb/mnist/
In [2]:
## Convert array to correct type for theano loadings onto GPU
def floatX(X):
return np.asarray(X, dtype=theano.config.floatX)
In [3]:
## Initialize parameters as random weights and floatX type for theano
def init_weights(shape):
return theano.shared(floatX(np.random.randn(*shape) * 0.01))
In [6]:
## Our model matrix format
## Get the dot product of our weight and fature matrices.
## Then run through the softmax function to get a probabilities distribution.
def model(X, w):
return T.nnet.softmax(T.dot(X, w))
In [7]:
## Load data matrices with our custom load.py function, mnist()
## onehot argument is for a matrix of dummy variables for each of the 10 digits.
## 1 for one digit, 0 for the rest, for each of the variables.
train_x, test_x, train_y, test_y = mnist(onehot=True)
In [8]:
## Now using matrix types
X = T.fmatrix()
Y = T.fmatrix()
In [9]:
w = init_weights((784, 10))
In [11]:
# Prob of y, given x. Multiply the weights (w) by x features to predict y.
py_x = model(X, w)
# Choose the y with the highest probability as our prediction
y_pred = T.argmax(py_x, axis=1)
In [ ]: