Advertisement
4ever_bored

dense.py

Oct 14th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import numpy
  3.  
  4. import theano
  5. import theano.tensor as T
  6.  
  7.  
  8. class DenseLayer(object):
  9.     def __init__(self, rng, input, n_in, n_out, W=None, b=None,
  10.                  activation=T.tanh):
  11.  
  12.         self.input = input
  13.  
  14.         if W is None:
  15.             W_values = numpy.asarray(
  16.                 numpy.random.uniform(
  17.                     low=-numpy.sqrt(6. / (n_in + n_out)),
  18.                     high=numpy.sqrt(6. / (n_in + n_out)),
  19.                     size=(n_in, n_out)
  20.                 ),
  21.                 dtype=theano.config.floatX  # @UndefinedVariable
  22.             )
  23.  
  24.             W = theano.shared(value=W_values, name='W', borrow=True)
  25.  
  26.         if b is None:
  27.             b_values = numpy.zeros((n_out,), dtype=theano.config.floatX)  # @UndefinedVariable
  28.             b = theano.shared(value=b_values, name='b', borrow=True)
  29.  
  30.         self.W = W
  31.         self.b = b
  32.  
  33.         if activation is None:
  34.             self.y_pred = T.dot(input, self.W) + self.b
  35.         else:
  36.             self.output = activation(T.dot(input, self.W) + self.b)
  37.        
  38.         self.params = [self.W, self.b]
  39.  
  40.     def mse(self, y):
  41.         return T.mean((self.y_pred.T-y)**2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement