python求导代码_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)

你可能感兴趣的:(python求导代码)