Created
February 12, 2015 08:50
-
-
Save nubela/c1a6a8b2e2425487c133 to your computer and use it in GitHub Desktop.
MNIST/Pylearn2 Simple 2-layer (Sigmod/Softmax) MLP trainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This trains a simple 2-layered MLP for classifying handwritten digits (MNIST dataset) | |
For this script to work, you need to have a ./mnist folder that contains the MNIST dataset. | |
The dataset can be downloaded using the script in the following pylearn2 folder: | |
<workspace>/pylearn2/pylearn2/scripts/datasets/download_mnist.py | |
After training the model (neural network containing layers), it will save trained model to disk. | |
""" | |
import struct | |
from digits.main_old import save_model | |
import numpy as np | |
from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix | |
from pylearn2.models import mlp | |
from pylearn2.termination_criteria import EpochCounter | |
from pylearn2.training_algorithms import sgd | |
from pylearn2.utils import serial | |
TRAINING_IMAGES_PATH = "mnist/train-images-idx3-ubyte" | |
TRAINING_LABEL_PATH = "mnist/train-labels-idx1-ubyte" | |
MODEL_SAVE_PATH = "./model.saved" | |
def read_mnist_images(): | |
dtype = 'float32' | |
with open(TRAINING_IMAGES_PATH, 'rb') as f: | |
_, number, rows, cols = struct.unpack('>iiii', f.read( | |
16)) # gotta do this line first to move the file pointer to AFTER the first 16 bytes | |
array = np.fromfile(f, dtype='uint8').reshape((number, rows, cols)) | |
dtype = np.dtype(dtype) | |
array = array.astype(dtype) | |
return array | |
def get_training_inputs(): | |
topo_view = read_mnist_images() | |
m, r, c = topo_view.shape | |
topo_view = topo_view.reshape(m, r, c, 1) | |
# rearrange axis to pylearn2's default (in this case, not needed, but added for documentation) | |
axes = ['b', 0, 1, 'c'] | |
default = ('b', 0, 1, 'c') | |
topo_view = topo_view.transpose(*[default.index(axis) for axis in axes]) | |
return topo_view | |
def get_training_labels(): | |
with open(TRAINING_LABEL_PATH, 'rb') as f: | |
_, number = struct.unpack('>ii', | |
f.read( | |
8)) # gotta do this line first to move the file pointer to AFTER the first 8 | |
# bytes | |
label_array = np.fromfile(f, dtype='uint8') | |
label_array = np.atleast_2d(label_array).transpose() | |
return label_array | |
def save_model(model): | |
model_path = "./model.saved" | |
serial.save(model_path, model) | |
if __name__ == "__main__": | |
# build dataset | |
training_dataset = DenseDesignMatrix(topo_view=get_training_inputs(), y=get_training_labels(), | |
axes=['b', 0, 1, 'c'], | |
y_labels=10) | |
# build layer | |
hidden_layer = mlp.Sigmoid(layer_name="hidden", dim=500, sparse_init=15) #sigmo | |
output_layer = mlp.Softmax(10, "output", irange=.1) #softmax layer, 10 outputs | |
layers = [hidden_layer, output_layer] | |
# init trainer | |
trainer = sgd.SGD(learning_rate=.05, batch_size=10, termination_criterion=EpochCounter(400)) | |
# init neural network | |
neural_network_model = mlp.MLP(layers, nvis=784) # 784 inputs | |
trainer.setup(neural_network_model, training_dataset) | |
# begin training the network with dataset | |
print "Begin training network.." | |
while True: | |
trainer.train(dataset=training_dataset) | |
neural_network_model.monitor.report_epoch() | |
neural_network_model.monitor() | |
if not trainer.continue_learning(neural_network_model): | |
print "Training ends." | |
break | |
print "Saving trained network.." | |
save_model(neural_network_model) | |
print "Network saved" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment