1. 代码如下:
- #!/usr/bin/env python
- #! _*_ coding:UTF-8 _*_
- import numpy as np
- import theano.tensor as T
- import theano
- x = T.dmatrix('x')
- # 定义一个激励函数
- s = 1 / (1 + T.exp(-x))
- logistic = theano.function([x], s)
- print logistic([[0, 1], [-2, -3]])
- # 一个函数输出多个变量的结果
- 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))
- )
- # 也可以写为
- x1, x2, x3 = f(np.ones((2, 2)),
- np.arange(4).reshape((2, 2))
- )
- print x1, x2, x3
- # 变量的名称及默认值
- 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, )
结果:
- /Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/theano_day2/theano_test.py
- [[ 0.5 0.73105858]
- [ 0.11920292 0.04742587]]
- [array([[ 1., 0.],
- [-1., -2.]]), array([[ 1., 0.],
- [ 1., 2.]]), array([[ 1., 0.],
- [ 1., 4.]])]
- [[ 1. 0.]
- [-1. -2.]] [[ 1. 0.]
[ 1. 2.]] [[ 1. 0.]
- [ 1. 4.]]
- 48.0
- Process finished with exit code 0
来源: http://www.bubuko.com/infodetail-2734664.html