当前位置: 首页 > news >正文

PyTorch张量

一、PyTorch张量概述

PyTorch 是一个开源的机器学习库,张量(Tensor)是 PyTorch 中最基本的数据结构。张量可以看作是多维数组,它类似于 NumPy 中的 ndarray。张量可以存储数据,并且能够利用 GPU 加速计算,这是 PyTorch 在深度学习中非常重要的特性。

  • 数据类型:张量可以存储多种数据类型,如浮点数(float32、float64 等)、整数(int32、int64 等)等。例如,torch.FloatTensor 表示存储浮点数的张量。

  • 维度:张量的维度可以是任意的。一个标量(单个数值)可以看作是 0 维张量,一维张量类似于向量,二维张量类似于矩阵,而更高维度的张量可以用于表示更复杂的数据结构,比如图像数据(通常是一个 4 维张量,包括批次大小、通道数、高度和宽度)。

二、创建张量

(一)直接从数据创建

Python复制

import torch

# 创建一个一维张量
tensor_1d = torch.tensor([1, 2, 3])
print(tensor_1d)

# 创建一个二维张量
tensor_2d = torch.tensor([[1, 2], [3, 4]])
print(tensor_2d)

这种方式可以直接从 Python 列表等数据结构创建张量。PyTorch 会根据提供的数据推断出张量的数据类型和形状。

(二)使用特定函数创建

  • 全零张量

    zeros_tensor = torch.zeros(2, 3)  # 创建一个 2×3 的全零张量
    print(zeros_tensor)
  • 全一张量

    ones_tensor = torch.ones(2, 3)  # 创建一个 2×3 的全一张量
    print(ones_tensor)
  • 随机张量

    rand_tensor = torch.rand(2, 3)  # 创建一个 2×3 的随机张量,元素值在 [0, 1) 之间
    print(rand_tensor)

    还有其他随机函数,如 torch.randn 用于创建正态分布的随机张量。

三、张量的基本操作

(一)索引和切片

  • 索引

    tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
    print(tensor[0, 1])  # 输出第一行第二列的元素
  • 切片

    print(tensor[:, 1:])  # 输出所有行,从第二列到最后一列

    这和 Python 中的切片语法类似,可以方便地获取张量的子部分。

(二)形状操作

  • 查看形状

    print(tensor.shape)  # 输出张量的形状
  • 改变形状

    tensor_reshaped = tensor.view(3, 2)  # 将张量重塑为 3×2 的形状
    print(tensor_reshaped)

    需要注意的是,重塑操作要求原始张量和目标张量的元素总数相同。

(三)数学运算

  • 加法

    tensor_a = torch.tensor([1, 2, 3])
    tensor_b = torch.tensor([4, 5, 6])
    tensor_sum = tensor_a + tensor_b  # 元素对应相加
    print(tensor_sum)
  • 乘法

    • 点乘(逐元素乘法)

      tensor_product = tensor_a * tensor_b
      print(tensor_product)
    • 矩阵乘法

      tensor_matrix_a = torch.tensor([[1, 2], [3, 4]])
      tensor_matrix_b = torch.tensor([[5, 6], [7, 8]])
      tensor_matrix_product = torch.matmul(tensor_matrix_a, tensor_matrix_b)
      print(tensor_matrix_product)

    这些数学运算使得张量可以方便地用于各种数学模型的构建和计算。

四、张量的设备管理

PyTorch 支持在 CPU 和 GPU 上进行计算。可以通过 .to() 方法将张量移动到指定的设备。

if torch.cuda.is_available():
    device = torch.device("cuda")
else:
    device = torch.device("cpu")

tensor = torch.tensor([1, 2, 3]).to(device)
print(tensor)

在深度学习中,将张量和模型移动到 GPU 上可以显著加快计算速度,因为 GPU 对于大规模并行计算有很好的性能优势。

参考文章

PyTorch 张量(Tensor) | 菜鸟教程

[自然语言处理]pytorch概述--什么是张量(Tensor)和基本操作-CSDN博客

张量 — PyTorch 教程 1.7.1 文档 - PyTorch 深度学习库

【PyTorch入门】 张量的介绍及常用函数和数据基础【一】-腾讯云开发者社区-腾讯云


文章转载自:
http://bullwhack.dxwdwl.cn
http://bolton.dxwdwl.cn
http://affliction.dxwdwl.cn
http://carlovingian.dxwdwl.cn
http://childbed.dxwdwl.cn
http://census.dxwdwl.cn
http://annular.dxwdwl.cn
http://blandiloquence.dxwdwl.cn
http://anathemata.dxwdwl.cn
http://chengchow.dxwdwl.cn
http://biofeedback.dxwdwl.cn
http://bentonitic.dxwdwl.cn
http://achlorhydria.dxwdwl.cn
http://anhinga.dxwdwl.cn
http://carneous.dxwdwl.cn
http://aiche.dxwdwl.cn
http://carrollese.dxwdwl.cn
http://bricklayer.dxwdwl.cn
http://bursarial.dxwdwl.cn
http://bedroom.dxwdwl.cn
http://astragal.dxwdwl.cn
http://abhorrer.dxwdwl.cn
http://breviped.dxwdwl.cn
http://appassionata.dxwdwl.cn
http://arginaemia.dxwdwl.cn
http://atrous.dxwdwl.cn
http://adenovirus.dxwdwl.cn
http://cerebration.dxwdwl.cn
http://carlet.dxwdwl.cn
http://caesarist.dxwdwl.cn
http://www.dtcms.com/a/111429.html

相关文章:

  • Opencv计算机视觉编程攻略-第九节 检测兴趣点
  • Linux systemd 服务全面详解
  • SQL语句(三)—— DQL
  • 详解AI采集框架Crawl4AI,打造智能网络爬虫
  • poetry安装
  • Transformer+BO-SVM时间序列预测(Matlab)
  • 第十五届蓝桥杯大赛软件赛省赛Python 大学 C 组:5.回文数组
  • 系统分析师-前6章总结
  • STM32单片机入门学习——第14节: [6-2] 定时器定时中断定时器外部时钟
  • PGSQL 对象创建函数生成工具
  • RSA和ECC在密钥长度相同的情况下哪个更安全?
  • 深度学习中的 Batch 机制:从理论到实践的全方位解析
  • AcWing 6118. 蛋糕游戏
  • Ubuntu安装Podman教程
  • Spring 核心技术解析【纯干货版】- XXI:Spring 第三方工具整合模块 Spring-Context-Suppor 模块精讲
  • 《古龙群侠传》游戏秘籍
  • 【 <二> 丹方改良:Spring 时代的 JavaWeb】之 Spring Boot 中的监控:使用 Actuator 实现健康检查
  • 【spring cloud Netflix】Eureka注册中心
  • 关于uint8_t、uint16_t、uint32_t、uint64_t的区别与分析
  • Linux(2025.3.15)
  • 安装 TabbyAPI+Exllamav2 和 vLLM 的详细步骤
  • 前后端通信指南
  • C# Winform 入门(7)之简单的抽奖系统邮件
  • #管理Node.js的多个版本
  • 虚拟现实 UI 设计:打造沉浸式用户体验
  • MINIQMT学习课程Day10
  • 欧几里得算法求最大公约数、最小公倍数
  • chromium魔改——CDP(Chrome DevTools Protocol)检测01
  • CCF GESP C++编程 八级认证真题 2025年3月
  • MySQL 性能调优:数据库的极限运动训练