如何在张量流中用 3d 张量对 2d 张量进行 matmul? [英] How to matmul a 2d tensor with a 3d tensor in tensorflow?

查看:45
本文介绍了如何在张量流中用 3d 张量对 2d 张量进行 matmul?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy 中,您可以将 2d 数组与 3d 数组相乘,如下例所示:

<预><代码>>>>X = np.random.randn(3,5,4) # [3,5,4]... W = np.random.randn(5,5) # [5,5]... out = np.matmul(W, X) # [3,5,4]

根据我的理解,np.matmul()W 并沿X 的第一个维度广播它.但在 tensorflow 中是不允许的:

<预><代码>>>>_X = tf.constant(X)... _W = tf.constant(W)... _out = tf.matmul(_W, _X)ValueError: Shape must be rank 2 but is rank 3 for 'MatMul_1' (op: 'MatMul') with input shape: [5,5], [3,5,4].

那么 np.matmul() 上面的 tensorflow 是否有等价物?tensorflow 中将 2d 张量与 3d 张量相乘的最佳实践是什么?

解决方案

尝试使用 tf.tile 在乘法之前匹配矩阵的维度.numpy 的自动广播功能似乎没有在 tensorflow 中实现.您必须手动完成.

W_T = tf.tile(tf.expand_dims(W,0),[3,1,1])

这应该可以解决问题

将 numpy 导入为 np将张量流导入为 tfX = np.random.randn(3,4,5)W = np.random.randn(5,5)_X = tf.constant(X)_W = tf.constant(W)_W_t = tf.tile(tf.expand_dims(_W,0),[3,1,1])使用 tf.Session() 作为 sess:打印(sess.run(tf.matmul(_X,_W_t)))

In numpy you can multiply a 2d array with a 3d array as below example:

>>> X = np.random.randn(3,5,4) # [3,5,4]
... W = np.random.randn(5,5) # [5,5]
... out = np.matmul(W, X) # [3,5,4]

from my understanding, np.matmul() takes W and broadcast it along the first dimension of X. But in tensorflow it is not allowed:

>>> _X = tf.constant(X)
... _W = tf.constant(W)
... _out = tf.matmul(_W, _X)

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul_1' (op: 'MatMul') with input shapes: [5,5], [3,5,4].

So is there a equivalent for what np.matmul() does above in tensorflow? And what's the best practice in tensorflow for multiplying 2d tensor with 3d tensor?

解决方案

Try using tf.tile to match the dimension of the matrix before multiplication. The automatic broadcast feature of numpy doesnt seem to be implemented in tensorflow. You have to do it manually.

W_T = tf.tile(tf.expand_dims(W,0),[3,1,1])

This should do the trick

import numpy as np
import tensorflow as tf

X = np.random.randn(3,4,5)
W = np.random.randn(5,5)

_X = tf.constant(X)
_W = tf.constant(W)
_W_t = tf.tile(tf.expand_dims(_W,0),[3,1,1])

with tf.Session() as sess:
    print(sess.run(tf.matmul(_X,_W_t)))

这篇关于如何在张量流中用 3d 张量对 2d 张量进行 matmul?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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