提交 17ac74ed authored 作者: Frederic's avatar Frederic

make Prepend_scalar_to_each_row and Prepend_scalar_constant_to_each_row op with…

make Prepend_scalar_to_each_row and Prepend_scalar_constant_to_each_row op with with other dtype then floatX.
上级 4a951c2d
...@@ -1633,15 +1633,16 @@ class Prepend_scalar_constant_to_each_row(gof.Op): ...@@ -1633,15 +1633,16 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
def make_node(self, mat): def make_node(self, mat):
#check type of input #check type of input
if not isinstance(mat,gof.Variable) or not mat.type==tensor.matrix().type:
raise TypeError("Expected a matrix as input")
x = tensor.as_tensor_variable(mat) x = tensor.as_tensor_variable(mat)
if not mat.type.broadcastable == (False, False):
raise TypeError("Expected a matrix as input")
y = tensor.as_tensor_variable(self.val) y = tensor.as_tensor_variable(self.val)
assert y.ndim == 0
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError( TypeError(
"the value to prepend don't have the same type as the matrix") "the value to prepend don't have the same type as the matrix")
node = Apply(op=self, inputs=[mat], outputs=[tensor.matrix()]) node = Apply(op=self, inputs=[mat], outputs=[mat.type()])
return node return node
def perform(self, node, inp, out): def perform(self, node, inp, out):
...@@ -1685,18 +1686,18 @@ class Prepend_scalar_to_each_row(gof.Op): ...@@ -1685,18 +1686,18 @@ class Prepend_scalar_to_each_row(gof.Op):
def make_node(self, val, mat): def make_node(self, val, mat):
#check type of input #check type of input
x = tensor.as_tensor_variable(mat)
if isinstance(val, float): if isinstance(val, float):
val = scalar.constant(val) val = scalar.constant(val)
if (not isinstance(mat, gof.Variable) or if not mat.type.broadcastable == (False, False):
not mat.type == tensor.matrix().type):
raise TypeError("Expected a matrix as input") raise TypeError("Expected a matrix as input")
x = tensor.as_tensor_variable(mat)
y = tensor.as_tensor_variable(val) y = tensor.as_tensor_variable(val)
assert y.ndim == 0
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError( TypeError(
"the value to prepend don't have the same type as the matrix") "the value to prepend don't have the same type as the matrix")
node = Apply(op=self, inputs=[val, mat], outputs=[tensor.matrix()]) node = Apply(op=self, inputs=[val, mat], outputs=[mat.type()])
return node return node
def perform(self, node, inp, out): def perform(self, node, inp, out):
......
...@@ -277,7 +277,7 @@ class T_prepend(utt.InferShapeTester): ...@@ -277,7 +277,7 @@ class T_prepend(utt.InferShapeTester):
x = tensor.matrix('x') x = tensor.matrix('x')
y = Prepend_scalar_constant_to_each_row(4.)(x) y = Prepend_scalar_constant_to_each_row(4.)(x)
f = theano.function([x], y) f = theano.function([x], y)
m = numpy.random.rand(3, 5) m = numpy.random.rand(3, 5).astype(config.floatX)
my = f(m) my = f(m)
self.assertTrue(my.shape == (3, 6), my.shape) self.assertTrue(my.shape == (3, 6), my.shape)
self.assertTrue(numpy.all(my[:, 0] == 4.0)) self.assertTrue(numpy.all(my[:, 0] == 4.0))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论