提交 46d97f35 authored 作者: Frederic's avatar Frederic

make tensor.outer(x,y) work when ndim != 1 as numpy.outer.

上级 f3cc9f75
......@@ -8185,7 +8185,14 @@ def tensordot(a, b, axes=2):
def outer(x, y):
"""Return vector-vector outer product."""
"""Return vector-vector outer product.
If an input isn't a vector, we flatten it first.
"""
if x.ndim != 1:
x = x.flatten()
if y.ndim != 1:
y = y.flatten()
return dot(
x.dimshuffle(0, 'x'),
y.dimshuffle('x', 0))
......
......@@ -2729,6 +2729,21 @@ class T_min_max(unittest.TestCase):
#axis=1)[0], n)),axis=1)
class T_outer(unittest.TestCase):
def test_outer(self):
for m in range(4):
for n in range(4):
x = tensor.tensor(dtype='floatX', broadcastable=(False,) * m)
y = tensor.tensor(dtype='floatX', broadcastable=(False,) * n)
s1 = numpy.random.randint(1, 10, m)
s2 = numpy.random.randint(1, 10, n)
v1 = numpy.asarray(numpy.random.rand(*s1)).astype(floatX)
v2 = numpy.asarray(numpy.random.rand(*s2)).astype(floatX)
print m, n, s1, s2
o = tensor.outer(x, y).eval({x: v1, y: v2})
assert_allclose(o, numpy.outer(v1, v2))
class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
"""
This is build in a way that allow to reuse it to test the
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论