forked from MorvanZhou/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeano5_function.py
36 lines (31 loc) · 1.1 KB
/
theano5_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# View more python tutorials on my Youtube and Youku channel!!!
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial
# 5 - theano.function
"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
from __future__ import print_function
import numpy as np
import theano
import theano.tensor as T
# activation function example
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x)) # logistic or soft step
logistic = theano.function([x], s)
print(logistic([[0, 1],[-1, -2]]))
# multiply outputs for a function
a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff ** 2
f = theano.function([a, b], [diff, abs_diff, diff_squared])
print( f(np.ones((2, 2)), np.arange(4).reshape((2, 2))) )
# default value and name for a function
x, y, w = T.dscalars('x', 'y', 'w')
z = (x+y)*w
f = theano.function([x,
theano.In(y, value=1),
theano.In(w, value=2, name='weights')],
z)
print(f(23, 2, weights=4))