#encoding:UTF-8 __author__ = 'auroua' import theano_test from theano_test import pp from theano_test import function import theano_test.tensor as T x = T.dscalar('x') y = x**2 gy = T.grad(y,x) f = function([x],y) print f(4) x2 = T.dmatrix('x2') s = T.sum(1/(1+T.exp(-x2))) gs = T.grad(s,x2) dlogistic = function([x2],gs) print dlogistic([[0,1],[-1,-2]]) x3 = T.dvector('x3') y3 = x3**2 J,updates = theano_test.scan(lambda i,y,x:T.grad(y[i],x),sequences=T.arange(y3.shape[0]),non_sequences=[y3,x3]) f = function([x3],J,updates=updates) print f([4,4]) x4 = T.dvector('x4') y4 = x4**2 cost = y4.sum() gy4 = T.grad(cost,x4) H,updates2 = theano_test.scan(lambda i,gy,x4:T.grad(gy[i],x4),sequences=T.arange(gy4.shape[0]),non_sequences=[gy4,x4])
#encoding:UTF-8 __author__ = 'auroua' from theano_test import pp import theano_test.tensor as T from theano_test import function #简单标量函数的求导 x = T.dscalar('x') y = x ** 2 gy = T.grad(y,x) print pp(gy) f = function([x],gy) print f(4) print f(94.2) print pp(f.maker.fgraph.outputs[0]) #sigmodi的求导 xs = T.dmatrix('x') y = T.sum(1/(1+T.exp(-xs))) gs = T.grad(y,xs) dlogistic = function([xs],gs) print dlogistic([[0, 1], [-1, -2]])