Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.70 KB | None | 0 0
  1. import cPickle
  2. import theano
  3. from theano import tensor as T
  4. from theano.tensor.nnet import conv
  5. from theano.tensor.signal import downsample
  6. import numpy as np
  7. import gzip
  8.  
  9. class LeNetConvPoolLayer(object):
  10.     def __init__(self, rng, filter_shape, image_shape, pool_size=(2,2)):
  11.         assert image_shape[1] == filter_shape[1] # Checking depth of image vs. depth of filter
  12.  
  13.         self.filter_shape = filter_shape
  14.         self.image_shape = image_shape
  15.         self.pool_size = pool_size
  16.  
  17.         fan_in = np.prod(filter_shape[1:]) # Counting total input neurons (number of pixels in image, depth included)
  18.         fan_out = (filter_shape[0] * np.prod(filter_shape[2:])) / np.prod(pool_size) # Counting total output neurons (number of kernels * image dimensions) / pooling reduction
  19.  
  20.         W_bound = np.sqrt(6. / (fan_in + fan_out)) # An arbitrary boundary on weights
  21.         self.W = theano.shared(value=np.asarray(rng.uniform(low = -W_bound, high = W_bound, size = filter_shape),
  22.                                                 dtype=theano.config.floatX),  name='convW',borrow=True)
  23.  
  24.         self.b = theano.shared(value=np.zeros((filter_shape[0],), dtype=theano.config.floatX),
  25.                                name='convB', borrow=True)
  26.  
  27.         self.params = [self.W, self.b]
  28.  
  29.     def output(self, input):
  30.         conv_out = conv.conv2d(input=input,
  31.                        filters=self.W,
  32.                        filter_shape=self.filter_shape,
  33.                        image_shape=self.image_shape)
  34.  
  35.         pooled_out = downsample.max_pool_2d(
  36.             input=conv_out,
  37.             ds=self.pool_size,
  38.             ignore_border=True
  39.         )
  40.         # Aligns the bias vector in such a way that each feature map is augmented individually, but also such that each
  41.         # feature map receives the same bias regardless of what image it happens to be on. All pixels within the feature
  42.         # map are also augmented in the same way.
  43.         return T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
  44.  
  45. class HiddenLayer(object):
  46.     def __init__(self, rng, n_in, n_out,activation=T.nnet.sigmoid):
  47.         self.activation = activation
  48.  
  49.         W = theano.shared(value=np.asarray(rng.uniform(
  50.             low = -np.sqrt(6. / (n_in + n_out)),
  51.             high = np.sqrt(6. / (n_in + n_out)),
  52.             size = (n_in, n_out)
  53.         ),dtype=theano.config.floatX), name='fcW', borrow=True)
  54.  
  55.         b = theano.shared(value=np.zeros((n_out,), dtype=theano.config.floatX), name='fcB', borrow=True)
  56.  
  57.         self.W = W
  58.         self.b = b
  59.         self.activation = activation
  60.         self.params = [self.W, self.b]
  61.  
  62.     def output(self, input):
  63.         input = input.flatten(2)
  64.         lin_output = T.dot(input, self.W) + self.b
  65.         return lin_output if self.activation is None else self.activation(lin_output)
  66.  
  67. class LogisticRegression(object):
  68.     def __init__(self, n_in, n_out):
  69.  
  70.         self.W = theano.shared(
  71.             value=np.zeros(
  72.             (n_in, n_out),
  73.             dtype=theano.config.floatX
  74.             ),
  75.             name='softmaxW',
  76.             borrow=True)
  77.  
  78.         self.b = theano.shared(
  79.             value=np.zeros(
  80.                 (n_out,),
  81.                 dtype=theano.config.floatX
  82.             ),
  83.             name='softmaxB',
  84.             borrow=True)
  85.  
  86.         self.params = [self.W, self.b]
  87.  
  88.     def output(self, input):
  89.         input = input.flatten(2)
  90.         self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
  91.         print(self.p_y_given_x)
  92.         return T.argmax(self.p_y_given_x, axis=1)
  93.  
  94. class MLP(object):
  95.     def __init__(self, topology):
  96.         self.layers = []
  97.         rng =  np.random.RandomState(6546)
  98.         for layer in topology:
  99.             type = layer[0]
  100.             if type == "conv":
  101.                 filter_shape = layer[1]
  102.                 image_shape = layer[2]
  103.                 self.layers.append(LeNetConvPoolLayer(rng, filter_shape, image_shape))
  104.             if type == "fc":
  105.                 input_count = layer[1]
  106.                 output_count = layer[2]
  107.                 self.layers.append(HiddenLayer(rng, input_count, output_count))
  108.             if type == "softmax":
  109.                 input_count = layer[1]
  110.                 output_count = layer[2]
  111.                 self.layers.append(LogisticRegression(input_count, output_count))
  112.         # Combine parameters from all layers
  113.         self.params = []
  114.         for layer in self.layers:
  115.             self.params += layer.params
  116.  
  117.     def output(self, x):
  118.         # Recursively compute output
  119.         new_x = x
  120.         for layer in self.layers:
  121.             new_x = layer.output(new_x)
  122.         return new_x
  123.  
  124.     def squared_error(self, x, y):
  125.         return T.sum((self.output(x) - y)**2)
  126.  
  127. def get_updates(cost, params, learning_rate):
  128.     updates = []
  129.     for param in params:
  130.         updates.append((param, param - learning_rate * T.grad(cost, param)))
  131.     return updates
  132.  
  133. def load_data(dataset):
  134.     f = gzip.open(dataset, 'rb')
  135.     train_set, valid_set, test_set = cPickle.load(f)
  136.     f.close()
  137.  
  138.     def shared_dataset(data_xy, borrow=True):
  139.         data_x, data_y = data_xy
  140.         shared_x = theano.shared(np.asarray(data_x, dtype=theano.config.floatX),
  141.                                 borrow=borrow)
  142.         shared_y = theano.shared(np.asarray(data_y, dtype=theano.config.floatX),
  143.                                  borrow=borrow)
  144.         return shared_x, T.cast(shared_y, 'int32')
  145.  
  146.     test_set_x, test_set_y = shared_dataset(test_set)
  147.     valid_set_x, valid_set_y = shared_dataset(valid_set)
  148.     train_set_x, train_set_y = shared_dataset(train_set)
  149.  
  150.     return [(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
  151.  
  152. batch_size = 500
  153. datasets = load_data('mnist.pkl.gz')
  154.  
  155. train_set_x, train_set_y = datasets[0]
  156. valid_set_x, valid_set_y = datasets[1]
  157. test_set_x, test_set_y = datasets[2]
  158.  
  159. n_train_batches = int(train_set_x.get_value(borrow=True).shape[0] / batch_size)
  160. n_valid_batches = int(valid_set_x.get_value(borrow=True).shape[0] / batch_size)
  161. n_test_batches = int(test_set_x.get_value(borrow=True).shape[0] / batch_size)
  162.  
  163. topology = [
  164.     #("conv", (32, 1, 5, 5), (batch_size, 1, 28, 28)),
  165.     #("conv", (64, 32, 5, 5), (batch_size, 32, 12, 12)),
  166.     #("fc", 64 * 4 * 4, 100),
  167.     ("softmax", 784, 10)
  168. ]
  169. mlp = MLP(topology)
  170.  
  171. # Create Theano variables for the MLP input
  172.  
  173. # ... and the desired output
  174. # Learning rate and momentum hyperparameter values
  175. # Again, for non-toy problems these values can make a big difference
  176. # as to whether the network (quickly) converges on a good local minimum.
  177. learning_rate = 0.5
  178.  
  179. # Create a function for computing the cost of the network given an input
  180. x = T.matrix('x')
  181. x_ = x.reshape((batch_size, 1, 28, 28))
  182. y = T.ivector('y')
  183. cost = mlp.squared_error(x_, y)
  184.  
  185. minibatch_index = T.lscalar('minibatch_index')
  186.  
  187. # Create a theano function for training the network
  188. train = theano.function([minibatch_index], cost,
  189.                         updates=get_updates(cost, mlp.params, learning_rate),
  190.                         givens= {
  191.                             x: train_set_x[minibatch_index * batch_size : (minibatch_index + 1) * batch_size],
  192.                             y: train_set_y[minibatch_index * batch_size : (minibatch_index + 1) * batch_size]
  193.                         })
  194. # Create a theano function for computing the MLP's output given some input
  195. mlp_output = theano.function([minibatch_index], mlp.output(x_),
  196.                              givens = {
  197.                                 x: train_set_x[minibatch_index * batch_size : (minibatch_index + 1) * batch_size]
  198.                              },
  199.                              allow_input_downcast=True)
  200. expected_output = theano.function([minibatch_index], y,
  201.                                   givens = {
  202.                                       y: train_set_y[minibatch_index * batch_size : (minibatch_index + 1) * batch_size]
  203.                                   })
  204.  
  205.  
  206. # Keep track of the number of training iterations performed
  207. iteration = 0
  208. max_iteration = 20
  209. while iteration < max_iteration:
  210.     print("Iteration %i" % iteration)
  211.     for minibatchIndex in xrange(n_train_batches):
  212.         current_cost = train(minibatchIndex)
  213.         current_output = mlp_output(minibatchIndex)
  214.         this_y = expected_output(minibatchIndex)
  215.         print("Error: %i" % current_cost)
  216.         #for a, b in zip(current_output, this_y):
  217.             # print(("Prediction: %i, Actual : %i") % (a, b))
  218.         accuracy = np.mean(np.not_equal(current_output,this_y))
  219.         #print(("Iteration %i: Minibatch %i/%i: Accuracy %f%%") % (iteration, minibatchIndex,
  220.         #                                                          n_train_batches, accuracy))
  221.     iteration += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement