Python的theano库符号求导示例代码详解编程语言

#coding=utf-8 
""" Symbolic computation of python theano 
 
@Author: zhang zewang 
@Date: 2016-3-2 
""" 
import sys 
sys.path.append('../utils/') 
import theano 
import theano.tensor as T 
import numpy as np 
from functionUtils import shared_normal,shared_zeros 
w = 1.5 
def step(input): 
    return w*input 
input = T.dscalar('input') 
output = step(input) 
f = theano.function([input],[output]) 
gf = T.grad(output,input) 
gf = theano.function([input],[gf]) 
i = 4 
print f(4) 
print gf(4)

#coding=utf-8 
""" Symbolic computation of python theano 
 
@Author: zhang zewang 
@Date: 2016-3-2 
""" 
import sys 
sys.path.append('../utils/') 
import theano 
import theano.tensor as T 
import numpy as np 
from functionUtils import shared_normal,shared_zeros 
w = [[1.5,2],[3,4]] 
w = np.array(w) 
input = T.dmatrix('input') 
output = w*(input) 
f = theano.function([input],[output]) 
gf = T.grad(output.sum(),input) 
gf = theano.function([input],[gf]) 
i = [[1,2],[5,6]] 
print f(i) 
print gf(i)

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8072.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论