提交 c195a8d7 authored 作者: ChienliMa's avatar ChienliMa

move TypeError into make_node, consequently changed the test a little. Add a…

move TypeError into make_node, consequently changed the test a little. Add a note to declarethe difference between fill_diagonal and fill_diagonal_offset
上级 086a2092
...@@ -760,6 +760,11 @@ class FillDiagonalOffset(gof.Op): ...@@ -760,6 +760,11 @@ class FillDiagonalOffset(gof.Op):
if val.dtype != a.dtype: if val.dtype != a.dtype:
raise TypeError('%s: type of second parameter must be the same' raise TypeError('%s: type of second parameter must be the same'
' as the first\'s' % self.__class__.__name__) ' as the first\'s' % self.__class__.__name__)
elif offset.dtype[:3] != 'int':
raise TypeError('%s: type of third parameter must be as integer'
' use theano.tensor.cast( input, \'int32/int64\')' \
% self.__class__.__name__)
return gof.Apply(self, [a, val, offset], [a.type()]) return gof.Apply(self, [a, val, offset], [a.type()])
...@@ -770,11 +775,6 @@ class FillDiagonalOffset(gof.Op): ...@@ -770,11 +775,6 @@ class FillDiagonalOffset(gof.Op):
offset = inputs[2] offset = inputs[2]
height, width = a.shape height, width = a.shape
# offset should be an integer
if offset % 1 != 0:
raise ValueError('%s: third parameter must be an integer'\
% self.__class__.__name__)
""" """
Note: The fill_diagonal only support rectangular matrix. The output Note: The fill_diagonal only support rectangular matrix. The output
of tall matrix is "wrapped", which is an option in numpy 1.9.0 of tall matrix is "wrapped", which is an option in numpy 1.9.0
......
...@@ -422,11 +422,11 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -422,11 +422,11 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
x = tensor.matrix() x = tensor.matrix()
y = tensor.scalar() y = tensor.scalar()
z = tensor.scalar() z = tensor.scalar()
z_in = tensor.cast( z, "int32")
test_offset = numpy.array(numpy.random.randint(-5,5), test_offset = numpy.random.randint(-5,5)
dtype = config.floatX)
f = function([x, y, z], fill_diagonal_offset(x, y, z)) f = function([x, y, z_in], fill_diagonal_offset(x, y, z_in))
for shp in [(8, 8), (5, 8), (8, 5)]: for shp in [(8, 8), (5, 8), (8, 5)]:
a = numpy.random.rand(*shp).astype(config.floatX) a = numpy.random.rand(*shp).astype(config.floatX)
val = numpy.cast[config.floatX](numpy.random.rand()) val = numpy.cast[config.floatX](numpy.random.rand())
...@@ -443,8 +443,7 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -443,8 +443,7 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
a.shape[0]+test_offset ) a.shape[0]+test_offset )
def test_gradient(self): def test_gradient(self):
test_offset = numpy.array(numpy.random.randint(-5,5), test_offset = numpy.random.randint(-5,5)
dtype = config.floatX)
# input 'offset' will not be tested # input 'offset' will not be tested
def fill_diagonal_with_fix_offset( a, val): def fill_diagonal_with_fix_offset( a, val):
return fill_diagonal_offset( a, val, test_offset) return fill_diagonal_offset( a, val, test_offset)
...@@ -460,10 +459,9 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -460,10 +459,9 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
x = tensor.dmatrix() x = tensor.dmatrix()
y = tensor.dscalar() y = tensor.dscalar()
z = tensor.dscalar() z = tensor.dscalar()
test_offset = numpy.array(numpy.random.randint(-5,5), z_in = tensor.cast( z, "int32")
dtype = config.floatX) self._compile_and_check([x, y, z_in], [self.op(x, y, z_in)],
self._compile_and_check([x, y, z], [self.op(x, y, z)],
[numpy.random.rand(8, 5), [numpy.random.rand(8, 5),
numpy.random.rand(), numpy.random.rand(),
test_offset], numpy.random.randint(-5,5)],
self.op_class) self.op_class )
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论