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())

运行结果:

张量转换

张量数值计算

相关推荐
爱问的艾文几秒前
八周带你手搓AI应用-Day4-赋予你的AI“记忆力”
人工智能
ACP广源盛1392462567313 分钟前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑
向量引擎22 分钟前
向量引擎接入 GPT Image 2 和 deepseek v4:一个 api key 把热门模型串起来,开发者终于不用深夜修接口了
人工智能·gpt·计算机视觉·aigc·api·ai编程·key
努力努力再努力FFF33 分钟前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
AI医影跨模态组学1 小时前
如何将纵向MRI深度学习特征与局部晚期直肠癌新辅助放化疗后的免疫微环境建立关联,并解释其对pCR及预后的机制
人工智能·深度学习·论文·医学·医学影像·影像组学
Empty-Filled1 小时前
AI生成测试用例功能怎么测:一个完整实战案例
网络·人工智能·测试用例
eastyuxiao1 小时前
设计一个基于 OpenClaw 的 AI 智能体来辅助交易
人工智能
风之所往_1 小时前
Python 3.0 新特性全面总结
python
2401_882273721 小时前
如何在 CSS 中正确加载本地 JPG 背景图片
jvm·数据库·python
波动几何1 小时前
因果动力学架构技能cda
人工智能