机器学习 - PyTorch一些常用的用法

如果我们要创建2维随机数

python 复制代码
import torch 

random_tensor = torch.rand(size=(3,4))
print(random_tensor)

# 输出
tensor([[0.0137, 0.7773, 0.0150, 0.2406],
        [0.6414, 0.7830, 0.7603, 0.1866],
        [0.8157, 0.8269, 0.0438, 0.0314]])

有时候需要通过加入数字0和数字1到tensor里

python 复制代码
# Create a tensor with all zeros 
zero = torch.zeros(size=(3,4))
print(zero) 

# 输出
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
python 复制代码
# Create a tensor with all ones
one = torch.ones(size=(3,4))
print(one)

# 输出
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])

想创建一个有范围的tensor,就得需要用到 torch.arange()

python 复制代码
zero_to_ten = torch.arange(start=0, end=10, step=1)
print(zero_to_ten)
# 输出
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

要想创建一个全部都是0或者是1的tensor

python 复制代码
ten_zeros = torch.zeros_like(input = zero_to_ten)
print(ten_zeros)
ten_ones = torch.ones_like(input = zero_to_ten)
print(ten_ones)

# 输出
tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

顺带介绍一下 CUDA.

英伟达的GPUs用了computing toolkit,这个toolkit叫做 CUDA。


Precision is the amount of detail used to describe a number.

在 PyTorch 中,精度 (precision) 值通常情况下,精度值可以是8, 16, 32或64,分别对应unit8, 单精度浮点数 (float32),半精度浮点数 (float16), 双精度浮点数 (float64)。

  • float32 (32位浮点数): 通常用于深度学习中的计算,默认情况下PyTorch中的Tensor类型是float32。它具有较高的精度和范围,适用于大多数深度学习任务。
  • float16 (16位浮点数): 也称为半精度浮点数,它可以显著减少内存占用,加快计算速度,但同时会带来精度损失。在某些情况下,可以使用float16来加速计算,但需要注意潜在的精度损失。
  • float64 (64位浮点数):也称为双精度浮点数,具有更高的精度和范围,但同时也会占有更多的内存空间。在某些情况下需要更高的数值精度时,可以使用float64。

可以自定义一些tensors with specific datatypes. 是用 dtype 这个参数

python 复制代码
float_32_tensor = torch.tensor([1.0, 6.0, 9.0],
                               dtype = None,
                               device = None,
                               requires_grad = False)

print(float_32_tensor.shape, float_32_tensor.dtype, float_32_tensor.device) 

# 结果输出
torch.Size([3]) torch.float32 cpu

通过下面的例子,来介绍什么是 shape, dtype, device

python 复制代码
example_tensor = torch.rand(3,4)
print(example_tensor)
print(f"Shape of tensor: {example_tensor.shape}")
print(f"Datatype of tensor: {example_tensor.dtype}")
print(f"Device tensor is stored on: {example_tensor.device}")

# 输出
tensor([[0.9961, 0.3773, 0.5295, 0.6546],
        [0.7505, 0.5768, 0.8997, 0.7675],
        [0.3681, 0.0447, 0.4344, 0.9033]])
Shape of tensor: torch.Size([3, 4])  
Datatype of tensor: torch.float32
Device tensor is stored on: cpu

都看到这了,给个赞呗~

相关推荐
Giser探索家4 分钟前
什么是2米分辨率卫星影像数据?
大数据·人工智能·数码相机·算法·分类·云计算
芯希望14 分钟前
芯伯乐XBL6019 60V/5A DC-DC升压芯片的优质选择
大数据·人工智能·物联网·dc-dc·电子元器件·电源管理ic·xblw芯伯乐
科大饭桶22 分钟前
AI大模型专题:LLM大模型(Prompt提示词工程)
人工智能·语言模型·llm·prompt·deepseek
六毛的毛28 分钟前
LangChain入门:内存、记录聊天历史 ChatMessageHistory、模型、提示 ( Prompt )、模式 ( Schema )
人工智能·langchain·prompt
饭碗、碗碗香1 小时前
【Dify学习笔记】:Dify搭建表单信息提交系统
人工智能·笔记·学习·ai
编程研究坊1 小时前
Neo4j APOC插件安装教程
数据库·人工智能·python·neo4j
大大花猫1 小时前
为了重温儿时回忆,我用AI做了一个小游戏合集APP【附源码】
人工智能·ai编程·游戏开发
万粉变现经纪人1 小时前
如何解决pip安装报错ModuleNotFoundError: No module named ‘transformers’问题
人工智能·python·beautifulsoup·pandas·scikit-learn·pip·ipython
cver1231 小时前
塑料可回收物检测数据集-10,000 张图片 智能垃圾分类系统 环保回收自动化 智慧城市环卫管理 企业环保合规检测 教育环保宣传 供应链包装优化
人工智能·安全·计算机视觉·目标跟踪·分类·自动化·智慧城市
jz_ddk2 小时前
[科普] AI加速器架构全景图:从GPU到光计算的算力革命
人工智能·学习·算法·架构