我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用theano.tensor.col()。
def test_wrong_broadcast(self): a = tt.col() increment = tt.vector() # These symbolic graphs legitimate, as long as increment has exactly # one element. So it should fail at runtime, not at compile time. rng = numpy.random.RandomState(utt.fetch_seed()) def rng_randX(*shape): return rng.rand(*shape).astype(theano.config.floatX) for op in (tt.set_subtensor, tt.inc_subtensor): for base in (a[:], a[0]): out = op(base, increment) f = theano.function([a, increment], out) # This one should work f(rng_randX(3, 1), rng_randX(1)) # These ones should not self.assertRaises(ValueError, f, rng_randX(3, 1), rng_randX(2)) self.assertRaises(ValueError, f, rng_randX(3, 1), rng_randX(3)) self.assertRaises(ValueError, f, rng_randX(3, 1), rng_randX(0))
def test_ndim_mismatch(self): rng = numpy.random.RandomState(utt.fetch_seed()) data = rng.rand(5).astype(self.dtype) x = self.shared(data) y = tensor.col('y', self.dtype) cond = theano.tensor.iscalar('cond') self.assertRaises(TypeError, ifelse, cond, x, y) self.assertRaises(TypeError, ifelse, cond, y, x)
def test_gemm_canonicalize(): X, Y, Z, a, b = T.matrix('X'), T.matrix('Y'), T.matrix('Z'), T.scalar( 'a'), T.scalar('b') R, S, U, c, d = T.matrix('R'), T.matrix('S'), T.matrix('U'), T.scalar( 'c'), T.scalar('d') u = T.row('u') v = T.vector('v') w = T.col('w') can = [] _gemm_canonicalize(X + Y + Z, 1.0, can, 0) assert can == [(1.0, X), (1.0, Y), (1.0, Z)] can = [] _gemm_canonicalize(X + Y + u, 1.0, can, 0) assert can == [(1.0, X), (1.0, Y), (1.0, u)], can can = [] _gemm_canonicalize(X + Y + v, 1.0, can, 0) # [(1.0, X), (1.0, Y), (1.0, InplaceDimShuffle{x,0}(v))] assert can[:2] == [(1.0, X), (1.0, Y)] assert isinstance(can[2], tuple) assert len(can[2]) == 2 assert can[2][0] == 1.0 assert can[2][1].owner assert isinstance(can[2][1].owner.op, T.DimShuffle) assert can[2][1].owner.inputs == [v] can = [] _gemm_canonicalize(X + Y + w, 1.0, can, 0) assert can == [(1.0, X), (1.0, Y), (1.0, w)], can can = [] _gemm_canonicalize(a * X + Y - b * Z * c, 1.0, can, 0) assert can[0] == (a, X) assert can[1] == (1.0, Y) assert can[2][0].owner.op == T.mul assert can[2][0].owner.inputs[0].owner.op == T.neg assert can[2][0].owner.inputs[0].owner.inputs[0] == c assert can[2][0].owner.inputs[1] == b can = [] _gemm_canonicalize((-d) * X - (a * X + Y - b * Z * c), 1.0, can, 0) # print can assert can[0][0].owner.op == T.neg assert can[0][0].owner.inputs[0] == d assert can[0][1] == X assert can[1][0].owner.op == T.neg assert can[1][0].owner.inputs[0] == a assert can[2] == (-1.0, Y) assert can[3][0].owner.op == T.mul assert can[3][0].owner.inputs == [c, b]