TensorFlow 中相当于 PyTorch 中的 expand() 的函数是什么?
What is the function in TensorFlow that is equivalent to expand() in PyTorch?
假设我有一个 2 x 3 矩阵,我想创建一个 6 x 2 x 3 矩阵,其中第一维中的每个元素都是原始 2 x 3 矩阵。
在 PyTorch 中,我可以这样做:
import torch
from torch.autograd import Variable
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
x = Variable(torch.from_numpy(x))
# y is the desired result
y = x.unsqueeze(0).expand(6, 2, 3)
在 TensorFlow 中执行此操作的等效方法是什么?我知道 unsqueeze()
等同于 tf.expand_dims()
但我不知道 TensorFlow 有什么等同于 expand()
。我正在考虑在 1 x 2 x 3 张量列表中使用 tf.concat
,但我不确定这是否是最好的方法。
Tensorflow 自动广播,所以一般情况下你不需要做任何这些。假设你有一个形状为 6x2x3 的 y'
而你的 x
是形状 2x3
,那么你已经可以做 y'*x
或 y'+x
已经表现得好像你扩大了它。但是如果出于其他原因你真的需要这样做,那么tensorflow中的命令是tile
:
y = tf.tile(tf.reshape(x, (1,2,3)), multiples=(6,1,1))
pytorch expand
的等效函数是 tensorflow tf.broadcast_to
文档:https://www.tensorflow.org/api_docs/python/tf/broadcast_to
假设我有一个 2 x 3 矩阵,我想创建一个 6 x 2 x 3 矩阵,其中第一维中的每个元素都是原始 2 x 3 矩阵。
在 PyTorch 中,我可以这样做:
import torch
from torch.autograd import Variable
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
x = Variable(torch.from_numpy(x))
# y is the desired result
y = x.unsqueeze(0).expand(6, 2, 3)
在 TensorFlow 中执行此操作的等效方法是什么?我知道 unsqueeze()
等同于 tf.expand_dims()
但我不知道 TensorFlow 有什么等同于 expand()
。我正在考虑在 1 x 2 x 3 张量列表中使用 tf.concat
,但我不确定这是否是最好的方法。
Tensorflow 自动广播,所以一般情况下你不需要做任何这些。假设你有一个形状为 6x2x3 的 y'
而你的 x
是形状 2x3
,那么你已经可以做 y'*x
或 y'+x
已经表现得好像你扩大了它。但是如果出于其他原因你真的需要这样做,那么tensorflow中的命令是tile
:
y = tf.tile(tf.reshape(x, (1,2,3)), multiples=(6,1,1))
pytorch expand
的等效函数是 tensorflow tf.broadcast_to
文档:https://www.tensorflow.org/api_docs/python/tf/broadcast_to