如何广播theano dot产品

时间:2015-05-28 09:26:22

标签: numpy theano

任何人都可以举例说明我的theano dot产品广播。它似乎与numpy不同

import numpy
import theano
import theano.tensor as T

theano.config.compute_test_value = 'off'

W1val = numpy.random.rand(2, 5, 10, 4).astype(theano.config.floatX)

W1 = theano.shared(W1val, 'W1')

x  = T.tensor3('x')

func_of_W1 = W1

h1 = T.dot(x, func_of_W1)

f = theano.function([x], h1)

print f(numpy.random.rand(3, 5, 10)).shape

以下是我尝试使用theano的实验。

#   T.dot(x shape ,  W1 shape) = result shape

# (3, 5, 10) * (2, 5, 10, 4) = (3, 5, 2, 5, 4)

# (3, 10) * (2, 5, 10, 4) = (3, 2, 5, 4)

# (3, 10) * (10 ,4) = (3, 4)

# (3, 10) * (2, 10 ,4) = (3, 2, 4)

# (5,10) * (2, 10 ,10) = (5, 2, 10)

1 个答案:

答案 0 :(得分:3)

Theano确实像numpy一样广播。 为了演示,此代码直接比较了Theano和numpy:

fn main() {
    let test = "5% of foo".to_string();
    let result: i32 = test.split('%').next().unwrap_or("0").parse().unwrap_or(0);
}

输出

import numpy

import theano
import theano.tensor as T

TENSOR_TYPES = dict([(0, T.scalar), (1, T.vector), (2, T.matrix), (3, T.tensor3), (4, T.tensor4)])

rand = numpy.random.rand


def theano_dot(x, y):
    sym_x = TENSOR_TYPES[x.ndim]('x')
    sym_y = TENSOR_TYPES[y.ndim]('y')
    return theano.function([sym_x, sym_y], theano.dot(sym_x, sym_y))(x, y)


def compare_dot(x, y):
    print theano_dot(x, y).shape, numpy.dot(x, y).shape


print compare_dot(rand(3, 5, 10), rand(2, 5, 10, 4))
print compare_dot(rand(3, 10), rand(2, 5, 10, 4))
print compare_dot(rand(3, 10), rand(10, 4))
print compare_dot(rand(3, 10), rand(2, 10, 4))
print compare_dot(rand(5, 10), rand(2, 10, 10))

Theano和numpy会在您描述的每种情况下产生相同形状的结果。