提交 b71bfbc4 authored 作者: Eric Larsen's avatar Eric Larsen

Split op and test classes, improve description of out_var in make_node.

上级 64cda1d9
import numpy import numpy
import theano
from theano.gof import Op, Apply from theano.gof import Op, Apply
from theano import tensor, function from theano import tensor
from theano.tests import unittest_tools as utt
try: try:
import scipy.linalg import scipy.linalg
...@@ -24,6 +24,7 @@ class Kron(Op): ...@@ -24,6 +24,7 @@ class Kron(Op):
A: array, shape (M*P, N*Q) A: array, shape (M*P, N*Q)
The result is the block matrix: The result is the block matrix:
(notice that a[i,j]*b is itself a matrix of the same shape as b)
a[0,0]*b a[0,1]*b ... a[0,-1]*b a[0,0]*b a[0,1]*b ... a[0,-1]*b
a[1,0]*b a[1,1]*b ... a[1,-1]*b a[1,0]*b a[1,1]*b ... a[1,-1]*b
...@@ -42,7 +43,7 @@ class Kron(Op): ...@@ -42,7 +43,7 @@ class Kron(Op):
def make_node(self, a, b): def make_node(self, a, b):
assert imported_scipy, ( assert imported_scipy, (
"Scipy not available. Scipy is needed for the Solve op") "Scipy not available. Scipy is needed for the Kron op")
a = tensor.as_tensor_variable(a) a = tensor.as_tensor_variable(a)
b = tensor.as_tensor_variable(b) b = tensor.as_tensor_variable(b)
...@@ -50,9 +51,9 @@ class Kron(Op): ...@@ -50,9 +51,9 @@ class Kron(Op):
raise TypeError('%s: inputs must have two dimensions' % raise TypeError('%s: inputs must have two dimensions' %
self.__class__.__name__) self.__class__.__name__)
out_type = tensor.TensorType(dtype=(a[0, 0] * b[0, 0]).dtype, out_var = tensor.TensorType(dtype=theano.scalar.upcast(a, b),
broadcastable=(False, False))() broadcastable=(False, False))()
return Apply(self, [a, b], [out_type]) return Apply(self, [a, b], [out_var])
def infer_shape(self, node, in_shapes): def infer_shape(self, node, in_shapes):
shape_a, shape_b = in_shapes shape_a, shape_b = in_shapes
...@@ -67,43 +68,3 @@ class Kron(Op): ...@@ -67,43 +68,3 @@ class Kron(Op):
' implemented' % self.__class__.__name__) ' implemented' % self.__class__.__name__)
kron = Kron() kron = Kron()
class TestKron(utt.InferShapeTester):
rng = numpy.random.RandomState(43)
def setUp(self):
super(TestKron, self).setUp()
self.op_class = Kron
self.op = kron
def test_perform(self):
x = tensor.dmatrix()
y = tensor.dmatrix()
f = function([x, y], kron(x, y))
for shp0 in [(8, 6), (5, 8)]:
for shp1 in [(5, 7), (3, 3)]:
a = numpy.random.rand(*shp0)
b = numpy.random.rand(*shp1)
out = f(a, b)
assert numpy.allclose(out, scipy.linalg.kron(a, b))
def test_infer_shape(self):
x = tensor.dmatrix()
y = tensor.dmatrix()
self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(8, 5),
numpy.random.rand(3, 7)],
self.op_class)
self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(2, 5),
numpy.random.rand(6, 3)],
self.op_class)
if __name__ == "__main__":
t = TestKron('setUp')
t.setUp()
t.test_perform()
t.test_infer_shape()
import numpy
from theano import tensor, function
from theano.tests import unittest_tools as utt
from theano.sandbox.linalg.kron import Kron, kron
try:
import scipy.linalg
imported_scipy = True
except ImportError:
imported_scipy = False
class TestKron(utt.InferShapeTester):
rng = numpy.random.RandomState(43)
def setUp(self):
super(TestKron, self).setUp()
self.op_class = Kron
self.op = kron
def test_perform(self):
assert imported_scipy, (
"Scipy not available. Scipy is needed for TestKron")
x = tensor.dmatrix()
y = tensor.dmatrix()
f = function([x, y], kron(x, y))
for shp0 in [(8, 6), (5, 8)]:
for shp1 in [(5, 7), (3, 3)]:
a = numpy.random.rand(*shp0)
b = numpy.random.rand(*shp1)
out = f(a, b)
assert numpy.allclose(out, scipy.linalg.kron(a, b))
def test_infer_shape(self):
x = tensor.dmatrix()
y = tensor.dmatrix()
self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(8, 5),
numpy.random.rand(3, 7)],
self.op_class)
self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(2, 5),
numpy.random.rand(6, 3)],
self.op_class)
if __name__ == "__main__":
t = TestKron('setUp')
t.setUp()
t.test_perform()
t.test_infer_shape()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论