gpt4 book ai didi

python - 广播从矩阵创建的子张量(Theano)

转载 作者:太空宇宙 更新时间:2023-11-04 05:27:17 24 4
gpt4 key购买 nike

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

我的问题是:如何指示我希望允许在给定索引的子张量中的特定维度上进行广播(下例中的 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:

当以这种方式 reshape subtensorRight 时,它会停止提示并给出预期的结果:

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

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

更新 2:

如果我尝试如下 reshape 它不起作用(我认为这与上面的 reshape 等价):

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 维进行 reshape 会得到不同的结果?

最佳答案

问题是您的 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])

无耻的 self 推销:

您可以使用 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)

关于python - 广播从矩阵创建的子张量(Theano),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com