从技术上讲,torch.STACK([t1,t1,t1],dim=1)和torch.hSTACK([t1,t1,t1])方法执行相同的操作,即它们都水平堆叠向量. 但是,当我在同一个向量上执行这两个操作时,它们产生了两个不同的输出,有人能解释一下为什么吗?

采用张量T1:

# Code : 
t1 = torch.arange(1.,10.)
t1,t1.shape
# Output : 
(tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.]), torch.Size([9]))

使用torch.栈([t1,t1,t1],dim=1)

# Code :
t1_stack = torch.stack([t1,t1,t1],dim=1)
#  dim value lies between [-2,1]
# -2 and 0 stack vertically
# -1 and 1 stack Horizontally
t1_stack,t1_stack.shape
# Output :
(tensor([[5., 5., 5.],
         [2., 2., 2.],
         [3., 3., 3.],
         [4., 4., 4.],
         [5., 5., 5.],
         [6., 6., 6.],
         [7., 7., 7.],
         [8., 8., 8.],
         [9., 9., 9.]]),
 torch.Size([9, 3]))

使用torch.h栈([t1,t1,t1])

# Code : 
h_stack = torch.hstack([t1,t1,t1])
h_stack,h_stack.shape
# Output : 
(tensor([5., 2., 3., 4., 5., 6., 7., 8., 9., 5., 2., 3., 4., 5., 6., 7., 8., 9.,
         5., 2., 3., 4., 5., 6., 7., 8., 9.]),
 torch.Size([27]))

对于相同的矢量,使用不同的水平叠加方法时,可以得到两种不同的输出

推荐答案

看着 torch 文档:

  • hstack:"这相当于串联along the first axis for 1-D tensors".
  • stack:"连接一个张量序列along a new dimension."

显然,在你的情况下,这不是完全相同的运算(一维张量).

Python-3.x相关问答推荐

如果行在所有上级索引中都为0,如何删除下级索引行?

如何通过Pandas为不同的列集垂直设置列数据?

无法使用Python slack 螺栓SDK读取在 slack 通道中收到的消息

如何在matplotlib中显示次要刻度标签

使用递归将int转换为字符串

在Pandas 数据帧中为小于5位的邮政编码添加前导零

新行是pandas数据帧中旧行的组合

与 pandas 0.22 相比,pandas 2.0.3 中的 df.replace() 会抛出 ValueError 错误

以特定方式重新排列 pandas 数据框的列

如何将列表和字典逐行组合在一起

attrs 将 list[str] 转换为 list[float]

Python.在循环中填充字典的问题

导入在不同目录中定义的函数

通过点和线计算CV2 Homography

创建一个可旋转的 3D 地球

str.format_map(mapping) 和 str.format 有什么区别

Python3 mysqlclient-1.3.6(又名 PyMySQL)的用法?

python asyncio - 如何等待取消的屏蔽任务?

AttributeError:LinearRegression 对象没有属性coef_

在动态链接库 Anaconda3\Library\bin\mkl_intel_thread.dll 中找不到序数 242