提交 0c5dc59f authored 作者: James Bergstra's avatar James Bergstra

C code for subtensor and inc_subtensor

上级 7e7998ec
差异被折叠。
...@@ -2057,7 +2057,7 @@ class T_subtensor(unittest.TestCase): ...@@ -2057,7 +2057,7 @@ class T_subtensor(unittest.TestCase):
for stop in [None] + [-8,-5,-1,0,1,5,8]: for stop in [None] + [-8,-5,-1,0,1,5,8]:
for step in [None]+[-3,-1,2]: for step in [None]+[-3,-1,2]:
outs += [ data[start:stop:step].shape ] outs += [ data[start:stop:step].shape ]
shapes += [data.get_value()[start:stop:step].shape ] shapes += [data.get_value(borrow=True)[start:stop:step].shape ]
f = function([], outs, mode = mode_opt) f = function([], outs, mode = mode_opt)
t_shapes = f() t_shapes = f()
for t_shape, shape in zip(t_shapes,shapes): for t_shape, shape in zip(t_shapes,shapes):
...@@ -2065,7 +2065,6 @@ class T_subtensor(unittest.TestCase): ...@@ -2065,7 +2065,6 @@ class T_subtensor(unittest.TestCase):
assert theano.tensor.Subtensor not in [ x.op for x in assert theano.tensor.Subtensor not in [ x.op for x in
f.maker.env.toposort() ] f.maker.env.toposort() ]
def test_shape_i_scalar(self): def test_shape_i_scalar(self):
# Each axis is treated independently by shape_i/shape operators # Each axis is treated independently by shape_i/shape operators
......
...@@ -20,7 +20,7 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -20,7 +20,7 @@ class Test_inc_subtensor(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
def test_simple_ok(self): def test_simple_2d(self):
"""Increments or sets part of a tensor by a scalar using full slice and """Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar. a partial slice depending on a scalar.
""" """
...@@ -52,8 +52,41 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -52,8 +52,41 @@ class Test_inc_subtensor(unittest.TestCase):
expected_result[:,:val_sl2_end] += val_inc expected_result[:,:val_sl2_end] += val_inc
self.assertTrue(numpy.array_equal(result, expected_result)) self.assertTrue(numpy.array_equal(result, expected_result))
return
def test_simple_3d(self):
"""Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar.
"""
a = T.dtensor3()
increment = T.dscalar()
sl1 = slice(None)
sl2_end = T.lscalar()
sl2 = slice(sl2_end)
sl3 = 2
for do_set in [True,False]:
print "Set", do_set
if do_set:
resut = T.set_subtensor(a[sl1, sl3, sl2], increment)
else:
resut = T.inc_subtensor(a[sl1, sl3, sl2], increment)
f = theano.function([a, increment, sl2_end], resut)
val_a = numpy.ones((5,3,4))
val_inc = 2.3
val_sl2_end = 2
expected_result = numpy.copy(val_a)
result = f(val_a, val_inc, val_sl2_end)
if do_set:
expected_result[:,sl3,:val_sl2_end] = val_inc
else:
expected_result[:,sl3,:val_sl2_end] += val_inc
self.assertTrue(numpy.array_equal(result, expected_result))
def test_grad(self): def test_grad(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论