torch.stack() 和 torch.cat() 函数之间有什么区别? [英] What's the difference between torch.stack() and torch.cat() functions?

查看:47
本文介绍了torch.stack() 和 torch.cat() 函数之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenAI 用于强化学习的 REINFORCE 和 actor-critic 示例具有以下代码:

OpenAI's REINFORCE and actor-critic example for reinforcement learning has the following code:

强化:

policy_loss = torch.cat(policy_loss).sum()

演员评论家:

loss = torch.stack(policy_losses).sum() + torch.stack(value_losses).sum()

一个使用torch.cat,另一个使用torch.stack.

One is using torch.cat, the other uses torch.stack.

据我所知,文档 没有给出任何明确的区别他们之间.

As far as my understanding goes, the doc doesn't give any clear distinction between them.

我很高兴知道这些功能之间的区别.

推荐答案

stack

沿新维度连接张量序列.

连接给定的seq张量序列在给定维度.

Concatenates the given sequence of seq tensors in the given dimension.

所以如果 AB 的形状为 (3, 4),torch.cat([A, B], dim=0) 的形状为 (6, 4) 而 torch.stack([A, B], dim=0) 的形状为 (2, 3, 4).

So if A and B are of shape (3, 4), torch.cat([A, B], dim=0) will be of shape (6, 4) and torch.stack([A, B], dim=0) will be of shape (2, 3, 4).

这篇关于torch.stack() 和 torch.cat() 函数之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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