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

CarinAcosta的头像 CarinAcosta 0 2016-03-02 19:36 0

 基本信息

× 1   

浏览数: 18442

分享时间: 2 年 前

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




还没有人评论,赶快来抢沙发吧!