假设我有一个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
,但我不确定这是否是最好的方法 .
1 回答
Tensorflow自动广播,因此通常您不需要执行任何此操作 . 假设你有一个形状为6x2x3的
y'
而x
的形状是2x3
,那么你已经可以做y'*x
或y'+x
已经表现得好像你已经扩展了它 . 但是如果由于某些其他原因你真的需要这样做,那么tensorflow中的命令是tile
:文件:https://www.tensorflow.org/api_docs/python/tf/tile