Advertisement
nordlaender

function in nnet

Jan 4th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. import theano
  2. import theano.tensor as T
  3. import numpy
  4.  
  5.  
  6. class Layer(object):
  7.     """
  8.    this is a layer in the mlp
  9.    it's not meant to predict the outcome hence it does not compute a loss.
  10.    apply the functions for negative log likelihood = cost on the output of the last layer
  11.    """
  12.  
  13.     def __init__(self, input, n_in, n_out):
  14.         self.W = theano.shared(
  15.                 value=numpy.zeros(
  16.                         (n_in, n_out),
  17.                         dtype=theano.config.floatX
  18.                 ),
  19.                 name="W",
  20.                 borrow=True
  21.         )
  22.         self.b = theano.shared(
  23.                 value=numpy.zeros(n_out,
  24.                                   dtype=theano.config.floatX),
  25.                 name="b",
  26.                 borrow=True
  27.         )
  28.  
  29.         self.output = T.nnet.softmax(T.dot(input, self.W) + self.b)
  30.         self.params = (self.W, self.b)
  31.         self.input = input
  32.  
  33.  
  34. def y_pred(output):
  35.     return T.argmax(output, axis=1)
  36.  
  37.  
  38. # this is meant to train the network for one specific function value
  39. def custom_cost(output, y):
  40.     if y==0:
  41.         return -T.mean(T.log(output))
  42.     else:
  43.         return -T.mean(1-T.log(output))
  44.  
  45.  
  46. def errors(output, y):
  47.     # check if y has same dimension of y_pred
  48.     if y.ndim != y_pred(output).ndim:
  49.         raise TypeError(
  50.                 'y should have the same shape as self.y_pred',
  51.                 ('y', y.type, 'y_pred', y_pred(output).type)
  52.         )
  53.     # check if y is of the correct datatype
  54.     if y.dtype.startswith('int'):
  55.         # the T.neq operator returns a vector of 0s and 1s, where 1
  56.         # represents a mistake in prediction
  57.         return T.mean(T.neq(y_pred(output), y))
  58.     else:
  59.         raise NotImplementedError()
  60.  
  61. data_x = numpy.matrix([[0, 0],
  62.                        [1, 0],
  63.                        [0, 1],
  64.                        [1, 1]])
  65.  
  66. data_y = numpy.array([0,
  67.                       0,
  68.                       0,
  69.                       1])
  70.  
  71. train_set_x = theano.shared(numpy.asarray(data_x,
  72.                          dtype=theano.config.floatX),
  73.                          borrow=True)
  74.  
  75. train_set_y = T.cast(theano.shared(numpy.asarray(data_y,
  76.                          dtype=theano.config.floatX),
  77.                          borrow=True),"int32")
  78.  
  79. x = T.matrix("x")  # data
  80. y = T.ivector("y")  # labels
  81.  
  82. classifier = Layer(input=x, n_in=2, n_out=1)
  83.  
  84. cost = custom_cost(classifier.output, y)
  85.  
  86. g_W = T.grad(cost=cost, wrt=classifier.W)
  87. g_b = T.grad(cost=cost, wrt=classifier.b)
  88. index = T.iscalar()
  89.  
  90. learning_rate = 0.1
  91.  
  92. updates = (
  93.     (classifier.W, classifier.W - learning_rate * g_W),
  94.     (classifier.b, classifier.b - learning_rate * g_b)
  95. )
  96.  
  97. train_model = theano.function(
  98.         inputs=[index],
  99.         outputs=cost,
  100.         updates=updates,
  101.         givens={
  102.             x: train_set_x[index:index + 1],
  103.             y: train_set_y[index:index + 1]
  104.         }
  105. )
  106. validate_model = theano.function(
  107.         inputs=[index],
  108.         outputs=errors(classifier.output, y),
  109.         givens={
  110.             x: train_set_x[index:index + 1],
  111.             y: train_set_y[index:index + 1]
  112.         }
  113. )
  114.  
  115. print("before")
  116. for i in range(4):
  117.     print(validate_model(i))
  118.  
  119. n=50
  120. last=10000000
  121. sum=0
  122. eps=0.01
  123. #while abs(last-sum)>eps:
  124. for i in range(5):
  125.     last=sum
  126.     sum=0
  127.     for i in range(4):
  128.         sum+=train_model(i)
  129.     print(sum)
  130.  
  131. print("after")
  132. for i in range(4):
  133.     print(validate_model(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement