【发布时间】:2017-01-25 17:53:34
【问题描述】:
我正在尝试按照 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'
看来这是上面定义的double类型的问题。但是,类型本身是比例,所以我应该能够使用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 的回答,我遇到了下一个问题 - 这些给我的印象是梯度计算不适用于非 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>)
【问题讨论】: