
如有翻译问题欢迎评论指出,谢谢。
今天这篇第三个回答只有一个投票,而且老长老长,我就不翻了,有需要的话进链接看吧。
注:今天这篇第二个回答有两张图片,但我现在加载不出来,之后加载出来了我再用图床补上。2022/4/7更新:图片已补。
Pytorch的torch.stack()和torch.cat()有什么区别?
-
Gulzar asked:
-
OpenAI 用于增强学习的 REINFORCE 和 actor-critic 示例代码有下面两句:
-
policy_loss = torch.cat(policy_loss).sum()
-
loss = torch.stack(policy_losses).sum() + torch.stack(value_losses).sum()
-
一个用
torch.cat
,另一个用torch.stack
。 -
就我的理解来说, 这篇文档没有清楚给出它们的区别。
-
希望有人能解答这两个函数的区别。
-
-
Answers:
-
Gulzar – vote: 173
-
stack
在新维度连接张量。
cat
在给定维度连接张量。
-
所以当形状为 (3, 4) 的
A
和B
,进行torch.cat([A, B], dim=0)
的结果形状为 (6, 4),进行torch.stack([A, B], dim=0)
的结果形状为 (2, 3, 4)。 -
Jatentaki – vote: 20
-
t1 = torch.tensor([[1, 2], [3, 4]]) t2 = torch.tensor([[5, 6], [7, 8]])
-
torch.stack
torch.cat
‘Stacks’ 在新维度连接张量: ‘Concatenates’ 在已存在的维度连接张量: -
这些函数类似
numpy.stack
和numpy.concatenate
.
-
What\’s the difference between torch.stack() and torch.cat() functions?
-
Gulzar asked:
-
OpenAI\’s REINFORCE and actor-critic example for reinforcement learning has the following code:
OpenAI 用于增强学习的 REINFORCE 和 actor-critic 示例代码有下面两句: -
policy_loss = torch.cat(policy_loss).sum()
-
loss = torch.stack(policy_losses).sum() + torch.stack(value_losses).sum()
-
One is using
torch.cat
, the other usestorch.stack
.
一个用torch.cat
,另一个用torch.stack
。 -
As far as my understanding goes, the doc doesn\’t give any clear distinction between them.
就我的理解来说, 这篇文档没有清楚给出它们的区别。 -
I would be happy to know the differences between the functions.
希望有人能解答这两个函数的区别。
-
-
Answers:
-
Gulzar – vote: 173
-
stack
Concatenates sequence of tensors along a new dimension.
在新维度连接张量。cat
Concatenates the given sequence of seq tensors in the given dimension.
在给定维度连接张量。-
So if
A
andB
are of shape (3, 4),torch.cat([A, B], dim=0)
will be of shape (6, 4) andtorch.stack([A, B], dim=0)
will be of shape (2, 3, 4).
所以当形状为 (3, 4) 的A
和B
,进行torch.cat([A, B], dim=0)
的结果形状为 (6, 4),进行torch.stack([A, B], dim=0)
的结果形状为 (2, 3, 4)。 -
Jatentaki – vote: 20
-
t1 = torch.tensor([[1, 2], [3, 4]]) t2 = torch.tensor([[5, 6], [7, 8]])
-
torch.stack
torch.cat
‘Stacks’ a sequence of tensors along a new dimension:
‘Stacks’ 在新维度连接张量:‘Concatenates’ a sequence of tensors along an existing dimension:
‘Concatenates’ 在已存在的维度连接张量: -
These functions are analogous to
numpy.stack
andnumpy.concatenate
.
这些函数类似numpy.stack
和numpy.concatenate
.
-
近期评论