zoukankan      html  css  js  c++  java
  • 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乘以向量








  • 相关阅读:
    centos6.8安装DB2 10.5
    linux yum配置本地iso镜像
    DB2的空间数据库管理复杂配置
    高性能计算linux集群常用配置
    centos 6.8操作系统安装arcgis server 10.4
    【转】用python实现简单的文本情感分析
    Python3制作中文词云图
    MongoDB Replica Set搭建集群
    pycharm编写spark程序,导入pyspark包
    Mysql变量声明与使用
  • 原文地址:https://www.cnblogs.com/fireae/p/3772670.html
Copyright © 2011-2022 走看看