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

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

问题描述

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

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])

如何通过张量点做到这一点?

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中写为

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)

但是,Thein中不存在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-使用张量点计算两个张量的点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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