什么是“解压"?在火炬? [英] What does "unsqueeze" do in Pytorch?

查看:114
本文介绍了什么是“解压"?在火炬?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看文档,这是他们的示例.我不明白这个例子如何与他们的解释相对应:返回在指定位置插入尺寸为1的新张量."

I'm looking at the documentation, and here is their example. I cannot understand how this example corresponds to their explanation: "Returns a new tensor with a dimension of size one inserted at the specified position."

>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1,  2,  3,  4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])

推荐答案

如果查看数组前后的形状,您会发现数组的前面是(4,),后面是(1, 4)(第二个参数时)是0)和(4, 1)(当第二个参数是1时).因此,根据第二个参数的值,以数组的形状在轴01处插入了1.

If you look at the shape of the array before and after, you see that before it was (4,) and after it is (1, 4) (when second parameter is 0) and (4, 1) (when second parameter is 1). So a 1 was inserted in the shape of the array at axis 0 or 1, depending on the value of the second parameter.

np.squeeze() 相反(从MATLAB借用的术语)删除尺寸为1(单个)的轴.

这篇关于什么是“解压"?在火炬?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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