如何定义允许区分的自定义Theano类型

时间:2017-01-25 17:53:34

标签: python theano

我正在尝试将文档关注到Theano中的create a double type,并按照here所述对此类型实施操作。目前的状态如下:

import theano

class Double(theano.gof.Type):

    def filter(self, value, strict = False, allow_downcast = None):
        if strict:
            # we need to return a type, but if the value is incompatible raise an exception
            if isinstance(value, float):
                return value
            else:
                raise TypeError('Expected a float!')
        elif allow_downcast:
            return float(value)
        else:
            value_float = float(value)
            if value_float == value:
                return value_float
            else:
                raise TypeError('The double type cannot be accurately represent %s of type %s' % (value, type(value)))

    def values_eq_approx(self, value_a, value_b, tolerance = 1e-6):
        return abs(value_a - value_b) / (abs(value_a) + abs(value_b)) < tolerance

double = Double()

class DoubleAddOp(theano.Op):

    __props__ = ()

    def make_node(self, x, y):
        # check input types
        if isinstance(x, (int, float)):
            x = theano.gof.Constant(double, x)
        if isinstance(y, (int, float)):
            y = theano.gof.Constant(double, y)

        if x.type != double or y.type != double:
            raise TypeError('DoubleAddOp only works on doubles.')

        return theano.gof.Apply(self, [x, y], [double()])

    def perform(self, node, inputs, output_storage):
        x = inputs[0]
        y = inputs[1]
        z = output_storage[0]
        z[0] = x + y

    def infer_shape(self, node, input_shapes):
        return [input_shapes[0]]

    def grad(self, inputs, output_grads):
        return [output_grads[0]*1, output_grads[0]*1]

    def __str__(self):
        return 'DoubleAddOp'

dadd = DoubleAddOp()

为了测试代码,我写了几个单元测试:

import theano
import random
import unittest
from double import double, dadd

class TestDoubleOps(unittest.TestCase):

    # the forward pass runs fine ...
    def test_DoubleAddOpPerform(self):
        x = double('x')
        y = double('y')
        z = dadd(x, y)
        f = theano.function([x, y], z)

        for i in range(100):
            x_value = random.random()
            y_value = random.random()
            self.assertAlmostEqual(f(x_value, y_value), x_value + y_value)

    # I am trying to get the gradient computation working here,
    # this is what I have so far:
    def test_DoubleAddOpGrad(self):
        x = double('x')
        y = double('y')
        z = dadd(x, y)
        gx = theano.tensor.grad(z, x) # <---
        gy = theano.tensor.grad(z, y)
        f = theano.function([x, y], [gx, gy])

        for i in range(100):
            x_value = random.random()
            y_value = random.random()

            print(f(x_value, y_value))

if __name__ == '__main__':
    unittest.main()

但是,在测试渐变计算时,我在标记的行处出现以下错误:

Traceback (most recent call last):
  File "~/theano/double-type-python/double_test.py", line 32, in test_DoubleAddOpGrad
    gx = theano.tensor.grad(z, x)
  File "~/.local/lib/python3.5/site-packages/theano/gradient.py", line 436, in grad
    if cost is not None and cost.ndim != 0:
AttributeError: 'Variable' object has no attribute 'ndim'

这似乎是上面定义的双重类型的问题。但是,类型本身是比例,所以我应该能够使用theano.tensor.grad计算渐变。不幸的是,我找不到一个展示自定义类型的渐变计算的示例,并且无法了解有关ndim属性的更多信息...

感谢任何帮助;谢谢!

更新。尝试欺骗theano.tensor.grad时,例如通过明确设置z.ndim = 0,问题仍然存在,例如

Traceback (most recent call last):
  File "~/theano/double-type-python/double_test.py", line 33, in test_DoubleAddOpGrad
    gx = theano.tensor.grad(z, x)
  File "/usr/local/lib/python3.4/dist-packages/theano/gradient.py", line 477, in grad
    g_cost = _float_ones_like(cost)
  File "/usr/local/lib/python3.4/dist-packages/theano/gradient.py", line 1340, in _float_ones_like
    dtype = x.type.dtype
AttributeError: 'Double' object has no attribute 'dtype'

所以我似乎缺少一些基本的东西,而且定义的Double类型缺少文档中没有提到的几种不同类型的信息。

更新。在重新阅读文档并查看Theano的源代码后,正确的问题是:是否可以在Theano中定义自定义(非张量)类型允许差异?

更新。基于nouiz&#39;回答,我遇到了下一个问题 - 这些让我觉得渐变计算不适用于非TensorType类型:

Traceback (most recent call last):
  File "~/theano/double-type-python/double_test.py", line 32, in test_DoubleAddOpGrad
    gx = theano.tensor.grad(z, x)
  File "~/.local/lib/python3.5/site-packages/theano/gradient.py", line 477, in grad
    g_cost = _float_ones_like(cost)
  File "~/.local/lib/python3.5/site-packages/theano/gradient.py", line 1344, in _float_ones_like
    return tensor.ones_like(x, dtype=dtype)
  File "~/.local/lib/python3.5/site-packages/theano/tensor/basic.py", line 2377, in ones_like
    return fill(model, ret)
  File "~/.local/lib/python3.5/site-packages/theano/gof/op.py", line 604, in __call__
    node = self.make_node(*inputs, **kwargs)
  File "~/.local/lib/python3.5/site-packages/theano/tensor/elemwise.py", line 577, in make_node
    inputs = list(map(as_tensor_variable, inputs))
  File "~/.local/lib/python3.5/site-packages/theano/tensor/basic.py", line 171, in as_tensor_variable
    "Variable type field must be a TensorType.", x, x.type)
theano.tensor.var.AsTensorError: ('Variable type field must be a TensorType.', DoubleAddOp.0, <double.Double object at 0x7fb623a5b9b0>)

1 个答案:

答案 0 :(得分:1)

答案是肯定的。您可以。我们自己做稀疏变量和GPU变量。

但是你遇到角落的情况下,theano.grad()没有得到支持。大多数情况下,它期望一个ndim参数和一个dtype参数。添加dtype =“float64”参数应该可以解决这个问题。

ndim很容易用The diff修复Theano:

diff --git a/theano/gradient.py b/theano/gradient.py
index 6d6fbaf..3b4d706 100644
--- a/theano/gradient.py
+++ b/theano/gradient.py
@@ -433,7 +433,7 @@ def grad(cost, wrt, consider_constant=None,
                          "cost is NaN because " +
                          cost.type.why_null)

-    if cost is not None and cost.ndim != 0:
+    if cost is not None and getattr(cost, 'ndim', 0) != 0:
         raise TypeError("cost must be a scalar.")

     if isinstance(wrt, set):

对于dtype,它更复杂,因为我们在许多地方使用它来进行验证(例如你不能取整数的渐变),也可以初始化渐变链(或者你可以通过它传递它known_grad参数)

更新:可以使用更大的差异修复新错误:

diff --git a/theano/gradient.py b/theano/gradient.py
index 6d6fbaf..6a9ec03 100644
--- a/theano/gradient.py
+++ b/theano/gradient.py
@@ -433,7 +433,7 @@ def grad(cost, wrt, consider_constant=None,
                          "cost is NaN because " +
                          cost.type.why_null)

-    if cost is not None and cost.ndim != 0:
+    if cost is not None and getattr(cost, 'ndim', 0) != 0:
         raise TypeError("cost must be a scalar.")

     if isinstance(wrt, set):
@@ -1341,7 +1341,7 @@ def _float_ones_like(x):
     if dtype not in tensor.float_dtypes:
         dtype = theano.config.floatX

-    return tensor.ones_like(x, dtype=dtype)
+    return x.ones_like(dtype=dtype)


 class numeric_grad(object):
diff --git a/theano/tensor/var.py b/theano/tensor/var.py
index 2ecb9f0..6b08a45 100644
--- a/theano/tensor/var.py
+++ b/theano/tensor/var.py
@@ -727,6 +727,9 @@ class _tensor_py_operators(object):
     def zeros_like(model, dtype=None):
         return theano.tensor.basic.zeros_like(model, dtype=dtype)

+    def ones_like(model, dtype=None):
+        return theano.tensor.basic.ones_like(model, dtype=dtype)
+
     def cumsum(self, axis=None):
         return theano.tensor.extra_ops.cumsum(self, axis)

您需要将ones_like方法添加到您的变量中,如下所示:     def my_ones_like(model,dtype = None):         回来......     double.ones_like = my_ones_like