theano4_basic_usage.py 948 B

1234567891011121314151617181920212223242526272829303132
  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. # 4 - basic usage
  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 numpy as np
  10. import theano.tensor as T
  11. from theano import function
  12. # basic
  13. x = T.dscalar('x')
  14. y = T.dscalar('y')
  15. z = x+y # define the actual function in here
  16. f = function([x, y], z) # the inputs are in [], and the output in the "z"
  17. print(f(2,3)) # only give the inputs "x and y" for this function, then it will calculate the output "z"
  18. # to pretty-print the function
  19. from theano import pp
  20. print(pp(z))
  21. # how about matrix
  22. x = T.dmatrix('x')
  23. y = T.dmatrix('y')
  24. z = x + y
  25. f = function([x, y], z)
  26. print(f(np.arange(12).reshape((3,4)), 10*np.ones((3,4))))