【PYG】pytorch中size和shape有什么不同

  • 一般使用tensor.shape打印维度信息,因为简单直接

在 PyTorch 中,sizeshape 都用于获取张量的维度信息,但它们之间有细微的区别。下面是它们的定义和用法:

  1. size

    • size 是一个方法(size())和属性(size),用于返回张量的维度信息。
    • 使用方法 size() 可以选择获取特定维度的大小。
    • 示例:
      import torch
      
      tensor = torch.tensor([[1.0, 2.0, 3.0, 4.0],
                             [2.0, 3.0, 4.0, 5.0],
                             [3.0, 4.0, 5.0, 6.0]])
      
      # 使用 size 方法(无参数)
      size_method = tensor.size()
      print(f"使用 size 方法: {size_method}")  # 输出: 使用 size 方法: torch.Size([3, 4])
      
      # 使用 size 方法(带维度参数)
      size_dim1 = tensor.size(1)
      print(f"维度 1 的大小: {size_dim1}")  # 输出: 维度 1 的大小: 4
      
  2. shape

    • shape 是一个属性,直接返回张量的维度信息,表示为一个 torch.Size 对象。
    • shape 属性不能接受参数,因此不能直接用于获取特定维度的大小。
    • 示例:
      import torch
      
      tensor = torch.tensor([[1.0, 2.0, 3.0, 4.0],
                             [2.0, 3.0, 4.0, 5.0],
                             [3.0, 4.0, 5.0, 6.0]])
      
      # 使用 shape 属性
      shape_attr = tensor.shape
      print(f"使用 shape 属性: {shape_attr}")  # 输出: 使用 shape 属性: torch.Size([3, 4])
      

区别

  • size 方法和属性

    • size 方法可以接受参数,例如 size(dim),用于获取特定维度的大小。
    • size 属性直接返回一个 torch.Size 对象,表示张量的所有维度。
  • shape 属性

    • shape 属性只返回一个 torch.Size 对象,表示张量的所有维度。
    • shape 属性不能直接获取特定维度的大小。

总结

  • size 提供了方法和属性,方法可以接受参数来获取特定维度的大小。

  • shape 仅作为属性,返回整个张量的维度信息,不能接受参数。

  • tensor.size返回<built-in method size of Tensor object at 0x7fee569194a0>

  • tensor.shape返回<class ‘torch.Size’>,tensor.size()返回<class ‘torch.Size’>

示例对比

import torch

tensor = torch.tensor([[1.0, 2.0, 3.0, 4.0],
                       [2.0, 3.0, 4.0, 5.0],
                       [3.0, 4.0, 5.0, 6.0]])

# 使用 size 属性
size_attr = tensor.size
print(f"使用 size 属性: {size_attr}")  # 输出: 使用 size 属性: torch.Size([3, 4])

# 使用 size 方法
size_method = tensor.size()
print(f"使用 size 方法: {size_method}")  # 输出: 使用 size 方法: torch.Size([3, 4])

# 使用 size 方法获取特定维度的大小
size_dim1 = tensor.size(1)
print(f"维度 1 的大小: {size_dim1}")  # 输出: 维度 1 的大小: 4

# 使用 shape 属性
shape_attr = tensor.shape
print(f"使用 shape 属性: {shape_attr}")  # 输出: 使用 shape 属性: torch.Size([3, 4])

通过以上示例可以看出,size 方法和属性提供了更灵活的用法,而 shape 属性则是一个简单快捷的方法来获取整个张量的维度信息。


当你直接访问 tensor.size 而不带括号时,你访问的是一个方法对象,而不是调用该方法。要获取张量的尺寸,你需要调用该方法,使用 tensor.size()。让我们通过一些示例来澄清这一点。

示例解释

首先,我们创建一个张量:

import torch

tensor = torch.tensor([[1.0, 2.0, 3.0],
                       [4.0, 5.0, 6.0]])

获取张量的尺寸

  1. 使用 size() 方法
size = tensor.size()
print(f"使用 size() 方法: {size}")  # 输出: 使用 size() 方法: torch.Size([2, 3])
  1. 直接访问 size 属性
size_method = tensor.size
print(f"直接访问 size 属性: {size_method}")  # 输出: 直接访问 size 属性: <built-in method size of Tensor object at 0x7fee569194a0>

在第二个示例中,我们得到的是一个方法对象的引用,而不是实际的尺寸信息。

获取特定维度的大小

要获取特定维度的大小,你需要调用 size(dim),其中 dim 是你感兴趣的维度索引:

size_dim1 = tensor.size(1)
print(f"维度 1 的大小: {size_dim1}")  # 输出: 维度 1 的大小: 3

使用 shape 属性

shape 属性是更直接获取张量尺寸的一种方式:

shape = tensor.shape
print(f"使用 shape 属性: {shape}")  # 输出: 使用 shape 属性: torch.Size([2, 3])

总结

  • tensor.size 返回一个方法对象引用。
  • tensor.size() 返回一个 torch.Size 对象,表示张量的形状。
  • tensor.size(dim) 返回特定维度的大小。
  • tensor.shape 直接返回一个 torch.Size 对象,表示张量的形状。

完整示例

import torch

tensor = torch.tensor([[1.0, 2.0, 3.0],
                       [4.0, 5.0, 6.0]])

# 使用 size() 方法
size = tensor.size()
print(f"使用 size() 方法: {size}")  # 输出: 使用 size() 方法: torch.Size([2, 3])

# 直接访问 size 属性
size_method = tensor.size
print(f"直接访问 size 属性: {size_method}")  # 输出: 直接访问 size 属性: <built-in method size of Tensor object at 0x7fee569194a0>

# 获取特定维度的大小
size_dim1 = tensor.size(1)
print(f"维度 1 的大小: {size_dim1}")  # 输出: 维度 1 的大小: 3

# 使用 shape 属性
shape = tensor.shape
print(f"使用 shape 属性: {shape}")  # 输出: 使用 shape 属性: torch.Size([2, 3])

相关推荐

  1. 【PYG】pytorchsizeshape什么不同

    2024-07-10 01:36:01       22 阅读
  2. ZooKeeperDiamond什么不同

    2024-07-10 01:36:01       42 阅读
  3. vivim什么不同

    2024-07-10 01:36:01       29 阅读
  4. tsjs什么不同

    2024-07-10 01:36:01       26 阅读
  5. 杂谈-CC++什么不同

    2024-07-10 01:36:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 01:36:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 01:36:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 01:36:01       58 阅读
  4. Python语言-面向对象

    2024-07-10 01:36:01       69 阅读

热门阅读

  1. linux指令学习

    2024-07-10 01:36:01       23 阅读
  2. 钉钉消息异常通知

    2024-07-10 01:36:01       19 阅读
  3. python 学习

    2024-07-10 01:36:01       20 阅读
  4. 【Unix/Linux】Unix/Linux如何查看系统版本

    2024-07-10 01:36:01       19 阅读
  5. 【Unix/Linux】$bash-3.2是什么

    2024-07-10 01:36:01       21 阅读
  6. Win11系统vscode配置C语言环境

    2024-07-10 01:36:01       24 阅读
  7. Mojo有哪些优势和劣势

    2024-07-10 01:36:01       19 阅读
  8. 论文阅读:Large Language Models for Education: A Survey

    2024-07-10 01:36:01       25 阅读
  9. ARM汇编的基础语法

    2024-07-10 01:36:01       24 阅读
  10. postman

    postman

    2024-07-10 01:36:01      20 阅读
  11. Redis

    Redis

    2024-07-10 01:36:01      20 阅读
  12. [Linux安全运维] Linux命令相关

    2024-07-10 01:36:01       26 阅读
  13. PCL 点云最小外接球形包围盒

    2024-07-10 01:36:01       20 阅读
  14. Pytest单元测试系列[v1.0.0][高级技巧]

    2024-07-10 01:36:01       19 阅读