定义自定义 Op theano 的等级 [英] Defining grad of a custom Op theano

查看:37
本文介绍了定义自定义 Op theano 的等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一个带有渐变的自定义 theano Op 以将其与 pymc3 一起使用,但我不明白如何定义 grad 方法.

I am trying to define a custom theano Op with a gradient to use it with pymc3 but I don't understand how to define the grad method.

下面的代码是我被卡住的地方.函数 phi() 是一个模拟函数(实际上是一个外部程序);对于标量输入 x,它返回一个向量 (phi_0(x), phi_1(x), ...).函数 phi_diff()(也是一个模拟函数)返回向量 (dphi_0/dx, dphi_1/dx, ...).

The code below is where I'm stuck. The function phi() is a mock function (in practice, it is an external program); for a scalar input x it returns a vector (phi_0(x), phi_1(x), ...). The function phi_diff() (also a mock function) returns the vector (dphi_0/dx, dphi_1/dx, ...).

我将 phi()phi_diff() 包裹在一个 theano.Op 对象中,但是我的 grad 实现代码> 功能不起作用.theano 的文档包含更简单的示例,我不明白在这种情况下如何调整它们.任何帮助将不胜感激.

I wrapped phi() and phi_diff() in a theano.Op object but my implementation of the grad function does not work. The documentation of theano contains simpler examples, I don't understand how to adapt them in this case. Any help would be greatly appreciated.

import numpy as np
import theano.tensor as T
import theano

theano.config.optimizer = "None"
theano.config.exception_verbosity = "high"


def phi(x):
    return np.arange(n) * x


def phi_diff(x):
    return np.arange(n)


class PhiOp(theano.Op):
    itypes = [theano.tensor.dscalar]
    otypes = [theano.tensor.dvector]

    def perform(self, node, inputs, output_storage):
        x = inputs[0]
        output_storage[0][0] = phi(x)

    def grad(self, inputs, output_grads):
        x = inputs[0]
        # ???
        return [PhiDiffOp()(x) * output_grads[0]]


class PhiDiffOp(theano.Op):
    itypes = [theano.tensor.dscalar]
    otypes = [theano.tensor.dvector]

    def perform(self, node, inputs, output_storage):
        x = inputs[0]
        output_storage[0][0] = phi_diff(x)


n = 5
x = 777.

phi_op = PhiOp()
x_tensor = T.dscalar("x_tensor")
phi_func = theano.function([x_tensor], phi_op(x_tensor))
np.testing.assert_allclose(phi_func(x), phi(x))

T.jacobian(phi_op(x_tensor), x_tensor)

推荐答案

找到解决方案,修改如下:

Found the solution, changes below:

def phi_diff(x):
    return np.arange(n, dtype=np.float_)

class PhiOp(theano.Op):
    def grad(self, inputs, output_grads):
        x = inputs[0]
        gg = (PhiDiffOp()(x) * output_grads[0]).sum()
        return [gg]

这篇关于定义自定义 Op theano 的等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆