• theano 入门教程1.7


    1.7.1计算导数
    使用theano.tensor.grad() 函数计算梯度 

    >>> import theano
    >>> import theano.tensor as T
    >>> from theano import pp
    >>> x = T.dscalar('x')
    >>> y = x ** 2
    >>> gy = T.grad(y, x)
    >>> pp(gy)
    '((fill((x ** TensorConstant{2}), TensorConstant{1.0}) * TensorConstant{2}) * (x ** (TensorConstant{2} - TensorConstant{1})))'
    >>> f = theano.function([x], gy)
    >>> f(4)
    array(8.0)
    >>> pp(f.maker.fgraph.outputs[0])
    '(TensorConstant{2.0} * x)'
    >>> 

    1.7.2计算Jacobian

    theano中提供了theano.gradient.jacobian()宏计算Jacobian。

    下面的代码阐明如何手动产生Jacobain。
    >>> x = T.dvector('x')
    >>> y = x ** 2
    >>> J, updates = theano.scan(lambda i, y,x : T.grad(y[i], x), sequences=T.arange(y.shape[0]), non_sequences=[y,x])
    >>> f = function([x], J, updates=updates)
    >>> f([4, 4])
    array([[ 8.,  0.],       [ 0.,  8.]])

    scan()函数是用来产生循环类型的变量。
    T.arange用来产生一个0到y.shape[0]的序列。

    1.7.3 计算Hessian

    theano中提供了theano.gradient.hessian()宏来计算Hessian

    下面的代码阐明如何自己手动产生Hessian
    >>> x = T.dvector('x')
    >>> y = x ** 2
    >>> cost = y.sum()
    >>> gy = T.grad(cost, x)
    >>> H, updates = theano.scan(lambda i, gy,x : T.grad(gy[i], x), sequences=T.arange(gy.shape[0]), non_sequences=[gy, x])
    >>> f = function([x], H, updates=updates)
    >>> f([4, 4])
    array([[ 2.,  0.],       [ 0.,  2.]])

    1.7.4
    Jacobian乘以向量
    1.7.4.1 右乘
    类似于这种形式, 
    >>> W = T.dmatrix('W')
    >>> V = T.dmatrix('V')
    >>> x = T.dvector('x')
    >>> y = T.dot(x, W)
    >>> JV = T.Rop(y, W, V)
    >>> f = theano.function([W, V, x], JV)
    >>> f([[1, 1], [1, 1]], [[2, 2], [2, 2]], [0,1])
    array([ 2.,  2.])

    1.7.4.2 左乘
    类似于这种形式, 

    >>> W = T.dmatrix('W')
    >>> v = T.dvector('v')
    >>> x = T.dvector('x')
    >>> y = T.dot(x, W)
    >>> VJ = T.Lop(y, W, v)
    >>> f = theano.function([v,x], VJ)
    >>> f([2, 2], [0, 1])
    array([[ 0.,  0.],       [ 2.,  2.]])

    1.7.5 Hessian乘以向量








  • 相关阅读:
    并发编程(2)-进程、并发和并行讲解
    并发编程(5)-管道、数据共享、进程池
    并发编程(4)-进程中的锁、信号量、 事件和队列
    人工智能及数学运算的基础方法
    并发编程(3)-进程模块
    判断一个数是否是水仙花数
    js中隐式类型转换测试
    webpack使用webpack-dev-middleware进行热重载
    网页打包安卓APP流程
    「postgres」查看数据库连接数
  • 原文地址:https://www.cnblogs.com/fireae/p/3772670.html
Copyright © 2020-2023  润新知