heano 当中的 function
就和 python 中的 function 类似, 不过因为要被用在多进程并行运算中,所以他的 function
有他自己的一套使用方式。
import numpy as np
import theano.tensor as T
import theano
#-----------使用激励函数---------------#
x = T.dmatrix('x')#定义矩阵容器x
s = 1/(1+T.exp(-x))#逻辑回归的激励函数
logistic = theano.function([x],s)#定义逻辑回归函数
print(logistic([[0,1],[-2,-3]]))
#------------多输入/输出的function------#
a,b = T.dmatrices('a','b')#因为多输入输出,所以是dmatrices
diff = a-b#差值
abs_diff = abs(diff)#绝对值
diff_squared = diff**2#平方
f = theano.function([a,b],[diff,abs_diff,diff_squared])#定义函数
x1,x2,x3 = f(np.ones((2,2)),np.arange(4).reshape(2,2))#调用自己定义的函数
print(x1,x2,x3)
#--------function参数的名字------------------#
x,y,w = T.dscalars('x','y','z')#定义三个容器
z = (x+y)*w
f = theano.function([x,
theano.In(y, value=1),
theano.In(w,value=2)],
z)
print(f(23))#使用默认
print(f(23,1,4))#不使用默认
#-------------function参数名字---------#
f = theano.function([x,
theano.In(y, value=1),
theano.In(w,value=2,name='weights')],
z)
print (f(23,1,weights=4)) ##调用方式
结果: