shome

conv3d2d theano error

Jan 19th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. from convnet3d import ConvLayer
  2. from convnet3d import PoolLayer
  3. from mlp import LogRegr
  4. from mlp import HiddenLayer
  5. import theano
  6. from theano import tensor as T
  7. import numpy as np
  8. import os
  9. import sys
  10. import timeit
  11. import pandas as pd
  12.  
  13. ''' LOAD DATASET '''
  14.  
  15. dtrain_x=pd.read_csv('train_dataset_permuted.txt', sep=',',header=None)
  16. dtrain_y=pd.read_csv('train_class_permuted.txt', sep=',',header=None)
  17.  
  18. dtest_x=pd.read_csv('test_data_full.txt', sep=',',header=None)
  19. dtest_y=pd.read_csv('test_class_full.txt', sep=',',header=None)
  20.  
  21. dvalid_x=pd.read_csv('valid_data_full.txt', sep=',',header=None)
  22. dvalid_y=pd.read_csv('valid_class_full.txt', sep=',',header=None)
  23.  
  24. train_set_x = theano.shared(np.asarray(dtrain_x,dtype=theano.config.floatX),borrow=True)
  25. train_set_y = T.cast(theano.shared(np.asarray(dtrain_y,dtype=theano.config.floatX),borrow=True),'int32')
  26.  
  27. test_set_x = theano.shared(np.asarray(dtest_x,dtype=theano.config.floatX),borrow=True)
  28. test_set_y = T.cast(theano.shared(np.asarray(dtest_y,dtype=theano.config.floatX),borrow=True),'int32')
  29.  
  30. valid_set_x = theano.shared(np.asarray(dvalid_x,dtype=theano.config.floatX),borrow=True)
  31. valid_set_y = T.cast(theano.shared(np.asarray(dvalid_y,dtype=theano.config.floatX),borrow=True),'int32')
  32.  
  33.  
  34. ''' CREATE MINIBATCHES '''
  35.  
  36. batch_size=100
  37. n_train_batches = train_set_x.get_value(borrow=True).shape[0]
  38. n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
  39. n_test_batches = test_set_x.get_value(borrow=True).shape[0]
  40. n_train_batches /= batch_size
  41. n_valid_batches /= batch_size
  42. n_test_batches /= batch_size
  43.  
  44. ''' LAYERS '''
  45.  
  46. rng1 = np.random.RandomState(1234)
  47. x=T.tensor3('x',dtype=theano.config.floatX)
  48. y=T.ivector('y')
  49.  
  50. nkerns=[10,10]
  51. learning_rate=0.01
  52. layer_0_input=x.reshape(batch_size,1,28,28,28)
  53. layer0=ConvLayer(layer_0_input, 1, nkerns[0], (5,5,5), (28,28,28), 100, T.tanh,"Conv1")
  54. layer1=PoolLayer(layer0.output, (2,2,2))
  55. layer2=ConvLayer(layer1.output, nkerns[0], nkerns[1], (5,5,5), (12,12,12), 100, T.tanh,"Conv2")
  56. layer3=PoolLayer(layer2.output, (2,2,2))
  57. layer4_input=layer3.output.flatten(2)
  58. layer4=HiddenLayer(layer4_input, nkerns[1]*4*4*4, 500, T.tanh)
  59. layer5=LogRegr(layer4.output, 500, 10, rng1)
  60.  
  61. ''' PARAMS, COST, GRAD, UPDATES, ERRORS '''
  62.  
  63. cost=layer5.negative_log_likelihood(y)
  64. params=layer5.params+layer4.params+layer3.params+layer2.params+layer1.params+layer0.params
  65. grads=T.grad[cost,params]
  66. updates=[(param_i, param_i - learning_rate*grad_i)
  67. for param_i,grad_i in zip(params,grads)]
  68.  
  69. index=T.lscalar()
  70.  
  71. ''' DEFINE TRAIN, TEST & VALIDATION MODEL '''
  72.  
  73. test_model = theano.function(
  74. [index],
  75. layer5.errors(y),
  76. givens={
  77. x: test_set_x[index * batch_size: (index + 1) * batch_size],
  78. y: test_set_y[index * batch_size: (index + 1) * batch_size]}
  79. )
  80.  
  81. validate_model = theano.function(
  82. [index],
  83. layer5.errors(y),
  84. givens={
  85. x: valid_set_x[index * batch_size: (index + 1) * batch_size],
  86. y: valid_set_y[index * batch_size: (index + 1) * batch_size]
  87. }
  88. )
  89.  
  90. train_model = theano.function(
  91. [index],
  92. cost,
  93. updates=updates,
  94. givens={
  95. x: train_set_x[index * batch_size: (index + 1) * batch_size],
  96. y: train_set_y[index * batch_size: (index + 1) * batch_size]
  97. }
  98. )
  99.  
  100. ###############
  101. # TRAIN MODEL #
  102. ###############
  103. print '... training'
  104. # early-stopping parameters
  105. patience = 10000 # look as this many examples regardless
  106. patience_increase = 2 # wait this much longer when a new best is # found
  107. improvement_threshold = 0.995 # a relative improvement of this much is considered significant
  108. validation_frequency = min(n_train_batches, patience / 2)
  109. # go through this many minibatche before checking the network on the validation set; in this case we check every epoch
  110.  
  111. best_validation_loss = np.inf
  112. best_iter = 0
  113. test_score = 0.
  114. start_time = timeit.default_timer()
  115.  
  116. epoch = 0
  117. n_epochs=10
  118. done_looping = False
  119.  
  120. while (epoch < n_epochs) and (not done_looping):
  121. epoch = epoch + 1
  122. for minibatch_index in xrange(n_train_batches):
  123. iter = (epoch - 1) * n_train_batches + minibatch_index
  124. if iter % 100 == 0:
  125. print 'training @ iter = ', iter
  126. cost_ij = train_model(minibatch_index)
  127. if (iter + 1) % validation_frequency == 0:
  128. # compute zero-one loss on validation set
  129. validation_losses = [validate_model(i) for i
  130. in xrange(n_valid_batches)]
  131. this_validation_loss = np.mean(validation_losses)
  132. print('epoch %i, minibatch %i/%i, validation error %f %%' %
  133. (epoch, minibatch_index + 1, n_train_batches,
  134. this_validation_loss * 100.))
  135. # if we got the best validation score until now
  136. if this_validation_loss < best_validation_loss:
  137. #improve patience if loss improvement is good enough
  138. if this_validation_loss < best_validation_loss * \
  139. improvement_threshold:
  140. patience = max(patience, iter * patience_increase)
  141. # save best validation score and iteration number
  142. best_validation_loss = this_validation_loss
  143. best_iter = iter
  144. # test it on the test set
  145. test_losses = [
  146. test_model(i)
  147. for i in xrange(n_test_batches)
  148. ]
  149. test_score = np.mean(test_losses)
  150. print((' epoch %i, minibatch %i/%i, test error of '
  151. 'best model %f %%') %
  152. (epoch, minibatch_index + 1, n_train_batches,
  153. test_score * 100.))
  154. if patience <= iter:
  155. done_looping = True
  156. break
  157. end_time = timeit.default_timer()
  158. print('Optimization complete.')
  159. print('Best validation score of %f %% obtained at iteration %i, '
  160. 'with test performance %f %%' %
  161. (best_validation_loss * 100., best_iter + 1, test_score * 100.))
  162. print >> sys.stderr, ('The code for file ' +
  163. os.path.split(__file__)[1] +
  164. ' ran for %.2fm' % ((end_time - start_time) / 60.))
Add Comment
Please, Sign In to add comment