theano8_Layer_class.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. # 8 - define Layer class
  5. """
  6. Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
  7. """
  8. from __future__ import print_function
  9. import theano
  10. import theano.tensor as T
  11. import numpy as np
  12. class Layer(object):
  13. def __init__(self, inputs, in_size, out_size, activation_function=None):
  14. self.W = theano.shared(np.random.normal(0, 1, (in_size, out_size)))
  15. self.b = theano.shared(np.zeros((out_size, )) + 0.1)
  16. self.Wx_plus_b = T.dot(inputs, self.W) + self.b
  17. self.activation_function = activation_function
  18. if activation_function is None:
  19. self.outputs = self.Wx_plus_b
  20. else:
  21. self.outputs = self.activation_function(self.Wx_plus_b)
  22. """
  23. to define the layer like this:
  24. l1 = Layer(inputs, 1, 10, T.nnet.relu)
  25. l2 = Layer(l1.outputs, 10, 1, None)
  26. """