从矩阵(Theano)创建的subtensor的广播

时间:2016-07-11 14:25:10

标签: python numpy matrix theano

我想从矩阵创建两个子扩展器,使用索引来选择相应的行。 一个subtensor有几行,另一个只有一行,应该广播以允许按元素添加。

我的问题是:我如何表明我想允许在子张量中的特定维度上进行广播,并给出索引(下例中的subtensorRight)?

以下示例显示了我想要做的事情:

import theano
import numpy as np
import theano.tensor as T

def embedding_matrix(D, N, name):
    W_values = np.random.uniform(size=(D, N))
    return theano.shared(value=W_values, name=name)

rE = embedding_matrix(4, 5, "rE")
lis = T.ivector('lis')# [1,2]
subtensorLeft = rE[lis,:]
ri = T.ivector('ri')#[1]
subtensorRight = rE[ri,:]


def fnsim(left, right):
    return - T.sqrt(T.sum(T.sqr(left - right), axis=1))

distances_test = theano.function(
    inputs=[lis, ri],
    outputs=fnsim(subtensorLeft, subtensorRight)
)

print distances_test([1,2],[1])

它抛出了这个错误:

ValueError: Input dimension mis-match. (input[0].shape[0] = 2, input[1].shape[0] = 1)
Apply node that caused the error: Elemwise{Composite{sqr((i0 - i1))}}[(0, 0)](AdvancedSubtensor1.0, AdvancedSubtensor1.0)
Toposort index: 2
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(2, 5), (1, 5)]
Inputs strides: [(40, 8), (40, 8)]
Inputs values: ['not shown', array([[ 0.39528934,  0.4414946 ,  0.36837258,  0.52523446,  0.35431748]])]
Outputs clients: [[Sum{axis=[1], acc_dtype=float64}(Elemwise{Composite{sqr((i0 - i1))}}[(0, 0)].0)]]

===

更新1:

它停止投诉并在以这种方式重塑subtensorRight时给出预期结果:

subtensorRight = rE[ri,:]
subtensorRight = subtensorRight.reshape((1, subtensorRight.shape[1]))

问题:这是正确的方法吗?

更新2:

如果我尝试重塑如下(我认为与上述重塑相同),它不起作用:

subtensorRight = rE[ri,:]
subtensorRight = subtensorRight.reshape(subtensorRight.shape)

错误是:

ValueError: Input dimension mis-match. (input[0].shape[0] = 2, input[1].shape[0] = 1)
Apply node that caused the error: Elemwise{Composite{sqr((i0 - i1))}}[(0, 0)](AdvancedSubtensor1.0, Reshape{2}.0)
Toposort index: 6
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(2, 5), (1, 5)]
Inputs strides: [(40, 8), (40, 8)]
Inputs values: ['not shown', array([[ 0.54193252,  0.36793023,  0.89009085,  0.02487759,  0.95955664]])]
Outputs clients: [[Sum{axis=[1], acc_dtype=float64}(Elemwise{Composite{sqr((i0 - i1))}}[(0, 0)].0)]]

问题:为什么从subtensor获取维度0的重塑会产生不同的结果?

1 个答案:

答案 0 :(得分:1)

问题在于你的theano函数事先并不知道正确的(ri)索引只有1个元素(因此对于所有人来说都知道你会尝试从中减去NxD矩阵一个MxD矩阵,一般不会工作。但是对于你的情况你只需要N = 1.)

解决方案是将您的正确索引声明为标量。

我相信以下代码可以满足您的需求:

import theano
import numpy as np
import theano.tensor as T

def embedding_matrix(D, N, name):
    W_values = np.random.uniform(size=(D, N))
    return theano.shared(value=W_values, name=name)

rE = embedding_matrix(4, 5, "rE")
lis = T.ivector('lis')# [1,2]
subtensorLeft = rE[lis,:]
ri = T.iscalar('ri')  # Instead of: ri = T.ivector('ri')
subtensorRight = rE[ri,:]


def fnsim(left, right):
    return - T.sqrt(T.sum(T.sqr(left - right), axis=1))

distances_test = theano.function(
    inputs=[lis, ri],
    outputs=fnsim(subtensorLeft, subtensorRight)
)

print distances_test([1,2],1)  # Instead of: distances_test([1,2],[1])

(输出[-0. -1.01565315]

无耻的自我推销:

您可以使用Plato库来制作更具可读性的theano代码。在你的情况下:

from plato.core import symbolic
import numpy as np
import theano.tensor as T

@symbolic
def distances_test(matrix, test_rows, reference_row):
    left = matrix[test_rows]
    right = matrix[reference_row]
    return - T.sqrt(T.sum(T.sqr(left - right), axis=1))

f = distances_test.compile()

print f(np.random.uniform(size=(4, 5)), np.array([1,2]), 1)