pytorch-unsqueeze用法

本文详细解析了PyTorch中unsqueeze()函数的使用方法,通过具体示例展示了如何利用该函数为张量添加维度,这对于深度学习模型的构建和数据预处理具有重要意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensor.unsqueeze 为tenor添加维度

a = torch.rand((1, 3, 5), dtype=torch.float32)
print("a shape is ", a.size())

b = a.unsqueeze(-1)  # 负1表示 在最后一维上添加
print("b shape is ", b.size())

输出结果如下

a shape is  torch.Size([1, 3, 5])
b shape is  torch.Size([1, 3, 5, 1])

真实数值如下:

a:
tensor([[[0.6146, 0.2028, 0.4266, 0.9713, 0.1965],
         [0.3768, 0.5539, 0.4202, 0.5306, 0.3155],
         [0.1831, 0.1981, 0.6740, 0.1039, 0.2108]]])

b:
tensor([[[[0.6146],
          [0.2028],
          [0.4266],
          [0.9713],
          [0.1965]],

         [[0.3768],
          [0.5539],
          [0.4202],
          [0.5306],
          [0.3155]],

         [[0.1831],
          [0.1981],
          [0.6740],
          [0.1039],
          [0.2108]]]])

注意:维度的顺序是从外往里数的, 看a和b就能看看出来。

(ngp) PS D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main> python run_nerf.py --config configs/chair.txt --finest_res 512 --log2_hashmap_size 19 --lrate 0.01 --lrate_decay 10 A module that was compiled using NumPy 1.x cannot be run in NumPy 2.2.4 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. We expect that some modules will need time to support NumPy 2. Traceback (most recent call last): File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\run_nerf.py", line 18, in <module> from run_nerf_helpers import * File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\run_nerf_helpers.py", line 7, in <module> from hash_encoding import HashEmbedder, SHEncoder File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\hash_encoding.py", line 8, in <module> from utils import get_voxel_vertices File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\utils.py", line 6, in <module> from ray_utils import get_rays, get_ray_directions, get_ndc_rays File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\ray_utils.py", line 2, in <module> from kornia import create_meshgrid File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\ngp\Lib\site-packages\kornia\__init__.py", line 20, in <module> from . import filters File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\ngp\Lib\site-packages\kornia\filters\__init__.py", line 60, in <module> from .kernels_geometry import get_motion_kernel2d, get_motion_kernel3d File "D:\zhuomian\HashNeRF-pytorch-main\HashNeRF-pytorch-main\ngp\Lib\site-packages\kornia\filters\kernels_geometry.py", line 24, in <module> from kornia.geometry.transform import rotate, rotate3d File "D:\zhuomian\HashNeRF-pytorch-m
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值