提交 fbc5a3a0 authored 作者: Matthew Rocklin's avatar Matthew Rocklin

matrix_of_scalars -> tensor_of_scalars

上级 5e02f08e
...@@ -8225,18 +8225,20 @@ def diag(v, k=0): ...@@ -8225,18 +8225,20 @@ def diag(v, k=0):
else: else:
raise ValueError("Input must be 1- or 2-d.") raise ValueError("Input must be 1- or 2-d.")
def matrix_of_scalars(list_of_lists): def tensor_of_scalars(arg):
""" Returns a matrix of scalars """ Returns a tensor of scalars
>>> from theano.tensor import matrix_of_scalars, scalar >>> from theano.tensor import tensor_of_scalars, scalar
>>> from theano import function >>> from theano import function
>>> a,b,c,d = map(scalar, 'abcd') >>> a,b,c,d = map(scalar, 'abcd')
>>> X = matrix_of_scalars([[a, b], >>> X = tensor_of_scalars([[a, b],
... [c, d]]) ... [c, d]])
>>> f = function([a, b, c, d], X) >>> f = function([a, b, c, d], X)
>>> f(1, 2, 3, 4) >>> f(1, 2, 3, 4)
array([[ 1., 2.], array([[ 1., 2.],
[ 3., 4.]], dtype=float32) [ 3., 4.]], dtype=float32)
""" """
from functools import partial if isinstance(arg, (tuple, list)):
return stack(*map(partial(apply, stack), list_of_lists)) return stack(*map(tensor_of_scalars, arg))
else:
return arg
...@@ -44,7 +44,7 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as, ...@@ -44,7 +44,7 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as,
dtensor3, SpecifyShape, Mean, IncSubtensor, AdvancedIncSubtensor1, dtensor3, SpecifyShape, Mean, IncSubtensor, AdvancedIncSubtensor1,
itensor3, Tile, AdvancedIncSubtensor, switch, Diagonal, Diag, itensor3, Tile, AdvancedIncSubtensor, switch, Diagonal, Diag,
nonzero, flatnonzero, nonzero_values, inplace_increment, nonzero, flatnonzero, nonzero_values, inplace_increment,
matrix_of_scalars) tensor_of_scalars)
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -6779,15 +6779,25 @@ def test_transpose(): ...@@ -6779,15 +6779,25 @@ def test_transpose():
assert tensor.transpose(x3).name == 'x3.T' assert tensor.transpose(x3).name == 'x3.T'
assert tensor.transpose(tensor.dmatrix()).name is None assert tensor.transpose(tensor.dmatrix()).name is None
def test_matrix_of_scalars(): def test_tensor_of_scalars():
a,b,c,d = map(scalar, 'abcd') a,b,c,d = map(scalar, 'abcd')
X = matrix_of_scalars([[a, b], X = tensor_of_scalars([[a, b],
[c, d]]) [c, d]])
f = function([a, b, c, d], X) f = function([a, b, c, d], X)
result = f(1,2,3,4) result = f(1,2,3,4)
assert result.shape == (2, 2) assert result.shape == (2, 2)
assert numpy.allclose(f(1, 2, 3, 4), numpy.asarray([[1,2],[3,4]])) assert numpy.allclose(f(1, 2, 3, 4), numpy.asarray([[1,2],[3,4]]))
X = tensor_of_scalars([a,b,c,d])
f = function([a, b, c, d], X)
result = f(1,2,3,4)
assert result.shape == (4,)
assert numpy.allclose(f(1, 2, 3, 4), numpy.asarray([[1,2,3,4]]))
X = tensor_of_scalars([[[a],[b]],[[c],[d]]])
f = function([a, b, c, d], X)
result = f(1,2,3,4)
assert result.shape == (2, 2, 1)
class TestSpecifyShape(unittest.TestCase): class TestSpecifyShape(unittest.TestCase):
def shortDescription(self): def shortDescription(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论