Skip to content

Instantly share code, notes, and snippets.

@jayendra13
Created March 17, 2015 12:39
Show Gist options
  • Save jayendra13/fae78568ebff5f92aec6 to your computer and use it in GitHub Desktop.
Save jayendra13/fae78568ebff5f92aec6 to your computer and use it in GitHub Desktop.
import numpy
import os
import time
import cPickle as pickle
import sys
from skimage import io, transform
from lasagne import layers
from lasagne.updates import nesterov_momentum
from nolearn.lasagne import NeuralNet
from sklearn.metrics import confusion_matrix
from datasets import load_train_data
def build_model():
# use the cuda-convnet implementations of conv and max-pool layer
#Conv2DLayer = layers.cuda_convnet.Conv2DCCLayer
#MaxPool2DLayer = layers.cuda_convnet.MaxPool2DCCLayer
Conv2DLayer = layers.Conv2DLayer
MaxPool2DLayer = layers.MaxPool2DLayer
net = NeuralNet(
layers=[
('input', layers.InputLayer),
('conv1', Conv2DLayer),
('pool1', MaxPool2DLayer),
('conv2', Conv2DLayer),
('pool2', MaxPool2DLayer),
('hidden4', layers.DenseLayer),
('output', layers.DenseLayer),
],
input_shape=(None, 1, 100, 100),
conv1_num_filters=1, conv1_filter_size=(3, 3), pool1_ds=(2, 2),
conv2_num_filters=1, conv2_filter_size=(2, 2), pool2_ds=(2, 2),
hidden4_num_units=50,
output_num_units=5, output_nonlinearity=None,
update_learning_rate=0.01,
update_momentum=0.9,
regression=False,
max_epochs=5,
verbose=1)
return net
if __name__ == '__main__':
X,Y = load_train_data()
X = X.astype('float32')
print 'X dtype %s'%(X.dtype)
print 'Y dtype %s'%(Y.dtype)
print 'X shape %s'%(X.shape)
print 'Y shape %s'%(Y.shape)
#print 'elepsed time is %f'%(end_-start_)
net = build_model()
net.fit(X, Y)
yhat = net.predict(X)
print(confusion_matrix(Y, yhat))
# Training for 1000 epochs will take a while. We'll pickle the
# trained model so that we can load it back later:
with open('net.pickle', 'wb') as f:
pickle.dump(net, f, -1)
@msegala
Copy link

msegala commented Mar 17, 2015

If this is a classification problem you need
output_nonlinearity=nonlinearities.softmax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment