theano - 使用 tensordot 计算两个张量的点积 [英] theano - use tensordot compute dot product of two tensor

查看:35
本文介绍了theano - 使用 tensordot 计算两个张量的点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 tensordot 来计算两个张量的特定暗度的点积.喜欢:

I want to use tensordot to compute the dot product of a specific dim of two tensors. Like:

A 是一个张量,其形状为 (3, 4, 5)B 是一个张量,其形状为 (3, 5)

A is a tensor, whose shape is (3, 4, 5) B is a tensor, whose shape is (3, 5)

我想做一个点,使用 A 的第三个暗淡和 B 的第二个暗淡,并得到一个暗淡为 (3, 4) 的输出

I want to do a dot use A's third dim and B's second dim, and get a output whose dims is (3, 4)

如下图:

for i in range(3):
    C[i] = dot(A[i], B[i])

tensordot 怎么做?

How to do it by tensordot?

推荐答案

那么,你想要在 numpy 中还是在 Theano 中?在这种情况下,如您所述,您希望将 A 的第 3 轴与 B 的第 2 轴收缩,两者都很简单:

Well, do you want this in numpy or in Theano? In the case, where, as you state, you would like to contract axis 3 of A against axis 2 of B, both are straightforward:

import numpy as np

a = np.arange(3 * 4 * 5).reshape(3, 4, 5).astype('float32')
b = np.arange(3 * 5).reshape(3, 5).astype('float32')

result = a.dot(b.T)

在 Theano 中这样写

in Theano this writes as

import theano.tensor as T

A = T.ftensor3()
B = T.fmatrix()

out = A.dot(B.T)

out.eval({A: a, B: b})

然而,输出的形状为 (3, 4, 3).由于您似乎想要形状 (3, 4) 的输出,因此 numpy 替代方案使用 einsum,就像这样

however, the output then is of shape (3, 4, 3). Since you seem to want an output of shape (3, 4), the numpy alternative uses einsum, like so

einsum_out = np.einsum('ijk, ik -> ij', a, b)

然而,Theano 中不存在 einsum.所以这里的具体情况可以模拟如下

However, einsum does not exist in Theano. So the specific case here can be emulated as follows

out = (a * b[:, np.newaxis]).sum(2)

也可以用Theano编写

which can also be written in Theano

out = (A * B.dimshuffle(0, 'x', 1)).sum(2)
out.eval({A: a, B: b})

这篇关于theano - 使用 tensordot 计算两个张量的点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆