提交 cbc68829 authored 作者: Frederic Bastien's avatar Frederic Bastien

Fix gh-4399. Add CpuContiguous.grad

上级 9c8561fc
...@@ -35,6 +35,9 @@ class CpuContiguous(theano.Op): ...@@ -35,6 +35,9 @@ class CpuContiguous(theano.Op):
assert x.flags['C_CONTIGUOUS'] assert x.flags['C_CONTIGUOUS']
y[0] = x y[0] = x
def grad(self, inputs, dout):
return [theano.tensor.as_tensor_variable(dout[0])]
def c_code(self, node, name, inames, onames, sub): def c_code(self, node, name, inames, onames, sub):
x, = inames x, = inames
y, = onames y, = onames
......
...@@ -25,12 +25,16 @@ def test_cpu_contiguous(): ...@@ -25,12 +25,16 @@ def test_cpu_contiguous():
a = T.fmatrix('a') a = T.fmatrix('a')
i = T.iscalar('i') i = T.iscalar('i')
a_val = numpy.asarray(numpy.random.rand(4, 5), dtype='float32') a_val = numpy.asarray(numpy.random.rand(4, 5), dtype='float32')
f = theano.function([a, i], cpu_contiguous(a.reshape((5,4))[::i])) f = theano.function([a, i], cpu_contiguous(a.reshape((5, 4))[::i]))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert any([isinstance(node.op, CpuContiguous) for node in topo]) assert any([isinstance(node.op, CpuContiguous) for node in topo])
assert f(a_val, 1).flags['C_CONTIGUOUS'] assert f(a_val, 1).flags['C_CONTIGUOUS']
assert f(a_val, 2).flags['C_CONTIGUOUS'] assert f(a_val, 2).flags['C_CONTIGUOUS']
assert f(a_val, 3).flags['C_CONTIGUOUS'] assert f(a_val, 3).flags['C_CONTIGUOUS']
# Test the grad:
theano.tests.unittest_tools.verify_grad(cpu_contiguous,
[numpy.random.rand(5, 7, 2)])
class TestCumsumOp(utt.InferShapeTester): class TestCumsumOp(utt.InferShapeTester):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论