```
In [76]:
```from sklearn import datasets
import cgt
import matplotlib.pyplot as plt
import numpy as np
from cgt import nn
%matplotlib inline

```
In [87]:
```digits = datasets.load_digits()

```
In [88]:
```digits.data

```
Out[88]:
```

```
In [89]:
```digits.data.shape

```
Out[89]:
```

```
In [90]:
```plt.gray()
plt.matshow(digits.images[0])

```
Out[90]:
```

```
In [91]:
```len(digits.target)

```
Out[91]:
```

Ok, we've had a little peek at our dataset, lets prep it for our model.

```
In [92]:
```randinds = np.random.permutation(len(digits.target))

```
In [93]:
```# shuffle the values
from sklearn.utils import shuffle
data, targets = shuffle(digits.data, digits.target, random_state=0)

```
In [101]:
```# scale the data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(data)
data_scaled = scaler.transform(data)

```
In [102]:
```from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data_scaled, targets, test_size=0.20, random_state=0)

```
In [103]:
```X_train.shape, y_train.shape

```
Out[103]:
```

Prep is done, time for the model.

```
In [166]:
```from cgt.distributions import categorical
def model(X, y):
# relu(W*x + b)
np.random.seed(0)
h1 = nn.rectify(nn.Affine(64, 512, weight_init=nn.IIDGaussian(std=.1))(X))
h2 = nn.rectify(nn.Affine(512, 512, weight_init=nn.IIDGaussian(std=.1))(h1))
# softmax probabilities
probs = nn.softmax(nn.Affine(512, 10)(h2))
# our prediction is the highest probability
ypreds = cgt.argmax(probs, axis=1)
acc = cgt.cast(cgt.equal(ypreds, y), cgt.floatX).mean()
cost = -categorical.loglik(y, probs).mean()
return cost, acc

```
In [167]:
```X = cgt.matrix(name='X', fixed_shape=(None, 64))
y = cgt.vector(name='y', dtype='i8')
cost, acc = model(X, y)

We've defined the cost and accuracy functions, time to train our model.

```
In [168]:
```learning_rate = 1e-3
epochs = 100
batch_size = 64
# get all the weight parameters for our model
params = nn.get_parameters(cost)
# train via SGD, use 1e-3 as the learning rate
updates = nn.sgd(cost, params, learning_rate)

```
In [169]:
```# Functions
trainf = cgt.function(inputs=[X,y], outputs=[], updates=updates)
cost_and_accf = cgt.function(inputs=[X,y], outputs=[cost,acc])

```
In [170]:
```import time
for i in xrange(epochs):
t1 = time.time()
for srt in xrange(0, X_train.shape[0], batch_size):
end = batch_size+srt
trainf(X_train[srt:end], y_train[srt:end])
elapsed = time.time() - t1
costval, accval = cost_and_accf(X_test, y_test)
print("Epoch {} took {}, test cost = {}, test accuracy = {}".format(i+1, elapsed, costval, accval))

```
```

```
In [ ]:
```