Skip to main content
 首页 » 编程设计

python之TensorFlow 中相当于 PyTorch 中的 expand() 的函数是什么

2024年08月16日29lyhabc

假设我有一个 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,但我不确定这是否是最好的方法。

请您参考如下方法:

pytorch expand 的等效函数是 tensorflow tf.broadcast_to

文档:https://www.tensorflow.org/api_docs/python/tf/broadcast_to