从矩阵创建的降压广播(Theano) [英] Broadcasting for subtensor created from matrix (Theano)
问题描述
我想从一个矩阵中创建两个子张量,使用索引选择相应的行. 一个子张量有几行,另一张只有几行,应广播以允许逐元素相加.
I want to create two subtensors from a matrix, using indices to select the respective rows. One subtensor has several rows, the other just one, which should be broadcast to allow for element-wise addition.
我的问题是:我如何表明我想允许在给定索引(下例中为subtensorRight
)的子张量中的特定维度上进行广播?
My question is: how do I indicate that I want to allow for broadcasting on the specific dimension in the sub-tensor resulting given the indices (subtensorRight
in the example below)?
以下是显示我想做什么的示例:
Here is the example showing what I want to do:
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
时,它会停止抱怨并给出预期的结果:
It stops complaining and gives the expected result when reshaping subtensorRight
this way:
subtensorRight = rE[ri,:]
subtensorRight = subtensorRight.reshape((1, subtensorRight.shape[1]))
问题:这是正确的方法吗?
Question: Is this the right way to go?
更新2:
如果我尝试按以下方式重塑(我认为与上述重塑完全相同),它将不起作用:
It does not work if I try to reshape as below (which I thought to be eqivalent to the reshaping above):
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)]]
问题:为什么从次张量中取0维来重塑会产生不同的结果?
Question: Why does reshaping with taking dimension 0 from the subtensor give a different result?
推荐答案
问题是您的theano函数无法预先知道正确的(ri
)索引将仅包含1个元素(因此,所有已知信息都知道您) '将尝试从MxD矩阵中减去NxD矩阵,这通常不起作用.但是对于您的情况,您只希望N = 1.)
The problem is that your theano function does not know in advance that the right (ri
) indices will have only 1 element (so for all in knows you'll be trying to subtract a NxD matrix from a MxD matrix, which won't work in general. However for your case you only ever want N=1.)
解决方案是将正确的索引声明为标量.
The solution is to declare your right index as a scalar.
我相信以下代码可以满足您的要求:
The following code, I believe, does what you want:
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代码更具可读性.对于您的情况:
You can use the Plato library to make more readable theano code. In your case:
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)
这篇关于从矩阵创建的降压广播(Theano)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!