0

Python的theano库符号求导示例代码

CarinAcosta 10年前发布 | 39K 次阅读 Python

#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)</pre>



#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)




本文由用户 CarinAcosta 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!

AltStyle によって変換されたページ (->オリジナル) /