提交 6d7dbb8f authored 作者: Dustin Webb's avatar Dustin Webb

Added check to make sure the variable is of the user specified size after…

Added check to make sure the variable is of the user specified size after removing leading broadcastable dimensions.
上级 85e09e63
...@@ -167,7 +167,12 @@ def as_tensor_variable(x, name=None, ndim=None): ...@@ -167,7 +167,12 @@ def as_tensor_variable(x, name=None, ndim=None):
# strip off leading broadcastable dimensions # strip off leading broadcastable dimensions
first_non_broadcastable = [idx for idx in range(x.ndim) first_non_broadcastable = [idx for idx in range(x.ndim)
if x.broadcastable[idx] == False][0] if x.broadcastable[idx] == False][0]
return x.dimshuffle(range(x.ndim)[first_non_broadcastable:]) x = x.dimshuffle(range(x.ndim)[first_non_broadcastable:])
if x.ndim > ndim:
raise ValueError(
'TensorType could not be cast to have %i dimensions' % ndim, x.type
)
return x
elif (x.type.ndim < ndim): elif (x.type.ndim < ndim):
return shape_padleft(x, n_ones=(ndim - x.type.ndim)) return shape_padleft(x, n_ones=(ndim - x.type.ndim))
else: else:
......
...@@ -1920,10 +1920,17 @@ Allocb4GradTester = makeBroadcastTester( ...@@ -1920,10 +1920,17 @@ Allocb4GradTester = makeBroadcastTester(
def test_as_tensor_variable(): def test_as_tensor_variable():
x = tensor.TensorType(config.floatX, (True, False))() x = tensor.TensorType(config.floatX, (True, False))('x')
x = as_tensor_variable(x, ndim=1) x = as_tensor_variable(x, ndim=1)
assert(x.ndim == 1) assert(x.ndim == 1)
x = tensor.matrix('x', dtype=config.floatX)
try:
x = as_tensor_variable(x, ndim=1)
assert(False) # The call above should have failed
except ValueError:
pass
class TestAlloc(unittest.TestCase): class TestAlloc(unittest.TestCase):
dtype = config.floatX dtype = config.floatX
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论