part6 PyTorch

什么是pytorch

https://pytorch.org/

接口是python,底层实现是c++

google-->TensorFlow 偏底层偏工程

facebook--->pytorch

pytorch安装

https://pytorch.org/get-started/locally

cpu版本

gpu版本

https://developer.nvidia.cn/cuda-gpus#compute

注意:cuda版本要≥pytorch支持版本

安装cuda地址:https://developer.nvidia.com/cuda-toolkit-archive

复制代码
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu126

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

  1. 执行后,pip会优先从清华镜像源下载包,而非默认的官方PyPI源(https://pypi.org/simple)。

    • 配置会写入全局配置文件(Windows:C:\Users\<用户名>\AppData\Roaming\pip\pip.ini;Linux/macOS:~/.pip/pip.conf)。
  2. 验证是否生效

    运行以下命令检查配置:

    pip config list

    输出应包含:

    复制代码
    global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
  3. 恢复默认源

    若需恢复官方源,可运行:

    pip config unset global.index-url

张量创建

基本张量创建

1.按内容创建张量torch.tensor(data)

复制代码
import numpy as np
import torch
'''基本张量创建
1.按内容创建张量'''
tensor1=torch.tensor(10)
print(tensor1.shape)
print(tensor1.size())
print(tensor1.shape)
print(tensor1.dtype)
# 传入一个一维数组
tensor2=torch.tensor([1,2,3])
print(tensor2)
print(tensor2.size())
print(tensor2.shape)
print(tensor2.dtype)
# 传入一个二维ndarray
tensor3=torch.tensor(np.array([[1,2,3],[4,5,6]]))
print(tensor3)
print(tensor3.shape)
print(tensor3.size())
print(tensor3.dtype)
'''

运行结果:

2.按形状创建张量torch.Tensor(size)

复制代码
'''
2.按形状创建张量
'''
tensor4=torch.Tensor(3,2,4)
print('---')
print(tensor4)
print(tensor4.shape)
print(tensor4.size())
print(tensor4.dtype)
tensor5=torch.Tensor(np.array([[1,2,3],[4,5,6]]))
print('---')
print(tensor5)
print(tensor5.shape)
print(tensor5.size())
print(tensor5.dtype)
tensor6=torch.Tensor(10)
print('---')
print(tensor6)
print(tensor6.shape)
print(tensor6.size())
print(tensor6.dtype)

运行结果:

注意:大写Tensor即可以传形状也可以传数据,但是数据内容的dtype固定是float,小写tensor的类型和原始数据相同,但大写Tensor传入num不是当标量而是当成一维数组

3.创建指定类型的张量

复制代码
'''
3.按类型创建张量
'''
# int32类型
tensor7=torch.IntTensor(2,3)
tensor8=torch.tensor([1,2,3],dtype=torch.int32)
print(tensor7.dtype)
print(tensor8.dtype)
# long(int64)类型
tensor9=torch.LongTensor(3,2)
tensor10=torch.tensor([1,2,3],dtype=torch.int64)
print(tensor9.dtype)
print(tensor10.dtype)
# Byte(int8)类型
tensor11=torch.ByteTensor(2,3)
tensor12=torch.tensor([1,2,3],dtype=torch.uint8)
# float类型
tensor13=torch.FloatTensor(2,3)
tensor14=torch.tensor([1,2,3],dtype=torch.float32)
# double(float64)类型
tensor15=torch.DoubleTensor(2,3)
tensor16=torch.tensor([1,2,3],dtype=torch.float64)
# half(float16)类型
tensor17=torch.HalfTensor(2,3)
tensor18=torch.tensor([1,2,3],dtype=torch.float16)
# bool类型
tensor19=torch.BoolTensor(2,3)
tensor20=torch.tensor([1,2,3],dtype=torch.bool)
# 0为Flase 1为True

指定区间的张量创建

1.torch.arange(start,end,step)在区间内按步长创建张量

复制代码
'''
指定区间的张量创建
'''
tensor21=torch.arange(10,30,2)
print('=======')
print(tensor21)
tensor22=torch.arange(6)
print('===')
print(tensor22)

运行结果:

2.torch.linspace(start,end,steps)在区间内按元素创建张量

复制代码
# 共生成5个元素,要包含边界10和30,中间均匀分3个元素生成
tensor23=torch.linspace(10,30,5)
print('=======')
print(tensor23)

运行结果:

3.torch.logspace(start,end,steps,base)在指数区间内按指定底数创建张量

复制代码
#在tensor23的基础上取log2(最后一个参数2就是以2为底)
tensor24=torch.logspace(10,30,5,2)
print(tensor24)

运行结果:

按数值填充张量

随机张量创建

复制代码
'''
随机张量创建
'''
tensor25=torch.rand(2,3) #【0,1)均匀,size=(2,3)
tensor26=torch.rand_like(tensor25)

tensor27=torch.randint(low=0,high=100,size=(2,3)) #【0,100)均匀,size=(2,3)
tensor28=torch.randint_like(tensor27,low=10,high=20)

tensor29=torch.randn(4,2) # 标准正太分布,size=(4,2)
tensor30=torch.randn_like(tensor29)

tensor31=torch.normal(5,2,size=(2,3)) # 均值5,标准差2的正态分布
复制代码
'''
随机排列
随机数种子
'''
# 将已知的排列打乱,相当于洗牌
tensor32=torch.randperm(10)
print(tensor32)
# 查看和指定随机数种子
print(torch.random.initial_seed())
torch.manual_seed(42)
print(torch.random.initial_seed())

运行结果:

张量转换

张量数值计算

相关推荐
阳光是sunny15 小时前
别再被 worktree 绕晕了!AI 编程时代你必须掌握的 Git 隔离神器
前端·人工智能·后端
冬奇Lab15 小时前
每日一个开源项目(第148篇):obsidian-skills - Obsidian CEO 亲写的 AI Agent 格式规范,让 Agent 不再破坏你的 Vault
人工智能·开源·资讯
ethantan16 小时前
AI Agent 组成:像人一样思考的智能体
人工智能·程序员·架构
冬奇Lab16 小时前
Workflow 系列(05):评测体系——三层测试结构与 Trace 追踪
人工智能·工作流引擎
ethantan16 小时前
一篇讲解AI Agent 组成:像人一样思考的智能体
人工智能·后端·程序员
apocelipes18 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
Cosolar18 小时前
vLLM 生产级部署完全指南
人工智能·后端·架构
CodePlayer竟然被占用了19 小时前
被美国政府封杀18天,Claude Fable 5 回来了——但代价是什么?
人工智能
IT_陈寒19 小时前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
smartpi20 小时前
SmartPi GPIO 脉冲与回复语执行时序指南
人工智能