Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from convnet3d import ConvLayer
- from convnet3d import PoolLayer
- from mlp import LogRegr
- from mlp import HiddenLayer
- import theano
- from theano import tensor as T
- import numpy as np
- import os
- import sys
- import timeit
- import pandas as pd
- ''' LOAD DATASET '''
- dtrain_x=pd.read_csv('train_dataset_permuted.txt', sep=',',header=None)
- dtrain_y=pd.read_csv('train_class_permuted.txt', sep=',',header=None)
- dtest_x=pd.read_csv('test_data_full.txt', sep=',',header=None)
- dtest_y=pd.read_csv('test_class_full.txt', sep=',',header=None)
- dvalid_x=pd.read_csv('valid_data_full.txt', sep=',',header=None)
- dvalid_y=pd.read_csv('valid_class_full.txt', sep=',',header=None)
- train_set_x = theano.shared(np.asarray(dtrain_x,dtype=theano.config.floatX),borrow=True)
- train_set_y = T.cast(theano.shared(np.asarray(dtrain_y,dtype=theano.config.floatX),borrow=True),'int32')
- test_set_x = theano.shared(np.asarray(dtest_x,dtype=theano.config.floatX),borrow=True)
- test_set_y = T.cast(theano.shared(np.asarray(dtest_y,dtype=theano.config.floatX),borrow=True),'int32')
- valid_set_x = theano.shared(np.asarray(dvalid_x,dtype=theano.config.floatX),borrow=True)
- valid_set_y = T.cast(theano.shared(np.asarray(dvalid_y,dtype=theano.config.floatX),borrow=True),'int32')
- ''' CREATE MINIBATCHES '''
- batch_size=100
- n_train_batches = train_set_x.get_value(borrow=True).shape[0]
- n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
- n_test_batches = test_set_x.get_value(borrow=True).shape[0]
- n_train_batches /= batch_size
- n_valid_batches /= batch_size
- n_test_batches /= batch_size
- ''' LAYERS '''
- rng1 = np.random.RandomState(1234)
- x=T.tensor3('x',dtype=theano.config.floatX)
- y=T.ivector('y')
- nkerns=[10,10]
- learning_rate=0.01
- layer_0_input=x.reshape(batch_size,1,28,28,28)
- layer0=ConvLayer(layer_0_input, 1, nkerns[0], (5,5,5), (28,28,28), 100, T.tanh,"Conv1")
- layer1=PoolLayer(layer0.output, (2,2,2))
- layer2=ConvLayer(layer1.output, nkerns[0], nkerns[1], (5,5,5), (12,12,12), 100, T.tanh,"Conv2")
- layer3=PoolLayer(layer2.output, (2,2,2))
- layer4_input=layer3.output.flatten(2)
- layer4=HiddenLayer(layer4_input, nkerns[1]*4*4*4, 500, T.tanh)
- layer5=LogRegr(layer4.output, 500, 10, rng1)
- ''' PARAMS, COST, GRAD, UPDATES, ERRORS '''
- cost=layer5.negative_log_likelihood(y)
- params=layer5.params+layer4.params+layer3.params+layer2.params+layer1.params+layer0.params
- grads=T.grad[cost,params]
- updates=[(param_i, param_i - learning_rate*grad_i)
- for param_i,grad_i in zip(params,grads)]
- index=T.lscalar()
- ''' DEFINE TRAIN, TEST & VALIDATION MODEL '''
- test_model = theano.function(
- [index],
- layer5.errors(y),
- givens={
- x: test_set_x[index * batch_size: (index + 1) * batch_size],
- y: test_set_y[index * batch_size: (index + 1) * batch_size]}
- )
- validate_model = theano.function(
- [index],
- layer5.errors(y),
- givens={
- x: valid_set_x[index * batch_size: (index + 1) * batch_size],
- y: valid_set_y[index * batch_size: (index + 1) * batch_size]
- }
- )
- train_model = theano.function(
- [index],
- cost,
- updates=updates,
- givens={
- x: train_set_x[index * batch_size: (index + 1) * batch_size],
- y: train_set_y[index * batch_size: (index + 1) * batch_size]
- }
- )
- ###############
- # TRAIN MODEL #
- ###############
- print '... training'
- # early-stopping parameters
- patience = 10000 # look as this many examples regardless
- patience_increase = 2 # wait this much longer when a new best is # found
- improvement_threshold = 0.995 # a relative improvement of this much is considered significant
- validation_frequency = min(n_train_batches, patience / 2)
- # go through this many minibatche before checking the network on the validation set; in this case we check every epoch
- best_validation_loss = np.inf
- best_iter = 0
- test_score = 0.
- start_time = timeit.default_timer()
- epoch = 0
- n_epochs=10
- done_looping = False
- while (epoch < n_epochs) and (not done_looping):
- epoch = epoch + 1
- for minibatch_index in xrange(n_train_batches):
- iter = (epoch - 1) * n_train_batches + minibatch_index
- if iter % 100 == 0:
- print 'training @ iter = ', iter
- cost_ij = train_model(minibatch_index)
- if (iter + 1) % validation_frequency == 0:
- # compute zero-one loss on validation set
- validation_losses = [validate_model(i) for i
- in xrange(n_valid_batches)]
- this_validation_loss = np.mean(validation_losses)
- print('epoch %i, minibatch %i/%i, validation error %f %%' %
- (epoch, minibatch_index + 1, n_train_batches,
- this_validation_loss * 100.))
- # if we got the best validation score until now
- if this_validation_loss < best_validation_loss:
- #improve patience if loss improvement is good enough
- if this_validation_loss < best_validation_loss * \
- improvement_threshold:
- patience = max(patience, iter * patience_increase)
- # save best validation score and iteration number
- best_validation_loss = this_validation_loss
- best_iter = iter
- # test it on the test set
- test_losses = [
- test_model(i)
- for i in xrange(n_test_batches)
- ]
- test_score = np.mean(test_losses)
- print((' epoch %i, minibatch %i/%i, test error of '
- 'best model %f %%') %
- (epoch, minibatch_index + 1, n_train_batches,
- test_score * 100.))
- if patience <= iter:
- done_looping = True
- break
- end_time = timeit.default_timer()
- print('Optimization complete.')
- print('Best validation score of %f %% obtained at iteration %i, '
- 'with test performance %f %%' %
- (best_validation_loss * 100., best_iter + 1, test_score * 100.))
- print >> sys.stderr, ('The code for file ' +
- os.path.split(__file__)[1] +
- ' ran for %.2fm' % ((end_time - start_time) / 60.))
Add Comment
Please, Sign In to add comment