PyTorch常用函数(2)

本文详细介绍了PyTorch中对象的序列化与反序列化方法torch.save()和torch.load(),以及一系列数学操作,包括绝对值、三角函数、加法、乘法、指数和对数等,还涵盖了张量的线性插值、取余、舍入等。这些操作在深度学习模型的训练和计算中至关重要。
摘要由CSDN通过智能技术生成

28. 序列化

  1. torch.save(obj, f, pickle_module=<module 'pickle' from '/home/jenkins/miniconda/lib/python3.5/pickle.py'>, pickle_protocol=2)保存一个对象到一个硬盘文件上
  • obj:保存对象
  • f:类文件对象(返回文件描述符)或一个保存文件名的字符串
  • pickle_module:用于pickling元数据和对象的模块
  • pickle_protocol:指定pickle protocal可以覆盖默认参数
  1. torch.load(f, map_location=None, pickle_module=<module 'pickle' from '/home/jenkins/miniconda/lib/python3.5/pickle.py'>)从磁盘文件中读取一个通过torch.save()保存的对象。torch.load()可通过参数map_location动态的进行内存重映射,使其能从不同设备中读取文件。一般调用时候,需两个参数:storage和location tags。返回不同地址中的storage,或者返回None(此时地址可以通过默认方法进行解析)。如果这个参数是字典的话,意味着其是从文件的地址标记到当前系统的地址标记的映射。默认情况下,locations tagscpu对应host tensorscuda:device_id对应cuda tensors。用户可通过register_package进行扩展,使用自己定义的标记和反序列化方法。
  • f:类文件对象(返回文件描述符)或一个保存文件名的字符串
  • map_location:一个函数或字典规定如何remap存储位置
  • pickle_module:用于unpickling元数据和对象的模块(必须匹配序列化文件时的pickle_module)
torch.load("tensor.pt")  
torch.load("tensor.pt", map_location=lambda storage, loc:storage)  # 加载所有tensor到CPU
torch.load("tensor.pt",map_location={"cuda:1":"cuda:0"})  # 加载所有Tensor从GPU1到GPU0

29.数学操作

  • torch.abs(input, out=None) → Tensor:计算输入张量的每个元素的绝对值
import torch
print(torch.abs(torch.tensor([-1, -3, 7])))
# tensor([1, 3, 7])
  • torch.acos(input, out=None) → Tensor:返回一个新张量,包含输入张量每个元素的反余弦,其他如torch.asin(input, out=None) → Tensor和torch.atan(input, out=None) → Tensor以及torch.atan2(input1, input2, out=None) → Tensor和torch.exp(tensor, out=None) → Tensor和torch.div(input, other, out=None)和torch.cosh(input, out=None) → Tensor和torch.cos(input, out=None) → Tensor同理
import torch
print(torch.acos(torch.tensor([-1, -0.3, 0.7])))
# tensor([3.1416, 1.8755, 0.7954])
print(torch.acos(torch.tensor([-1, -3, 7])))
# tensor([3.1416,    nan,    nan])
  • torch.add(input, value, out=None):对输入张量 input 逐元素加上标量值 value,并返回结果到一个新的张量 out
import torch
a = torch.randn(4)
print(a)
print(torch.add(a, 10))
# tensor([ 0.4790,  0.3818,  0.5941, -0.8703])
# tensor([10.4790, 10.3818, 10.5941,  9.1297])
  • torch.add(input, value=1, other, out=None):other 张量的每个元素乘以一个标量值 value,并加到 iput 张量上。返回结果到输出张量 out
import torch
a = torch.randn(4)
print(a)
b = torch.randn(4)
print(b)
print(torch.add(a, 1, b))
# tensor([ 0.0173, -1.0013,  0.6701, -1.6446])
# tensor([ 0.3475, -1.5598,  1.5256,  0.2852])
# tensor([ 0.3648, -2.5611,  2.1957, -1.3594])
  • torch.addcdiv(tensor, value=1, tensor1, tensor2, out=None) → Tensor:逐元素 tensor1/tensor2,然后乘以标量值 value 并加到 tensor,其他如torch.addcmul(tensor, value=1, tensor1, tensor2, out=None) → Tensor同理
import torch
t = torch.randn(1, 3)
t1 = torch.randn(2, 1)
t2 = torch.randn(2, 1)
print(torch.addcdiv(t, 0.1, t1, t2))
# tensor([[-0.9363, -0.6710,  0.7220],
#         [-1.3724, -1.1070,  0.2860]])

t = torch.randn(1, 2)
t1 = torch.randn(1, 2)
t2 = torch.randn(2, 1)
print(torch.addcdiv(t, 0.1, t1, t2))
# tensor([[ 0.1477, -1.1794],
#         [-0.7670, -1.9615]])
  • torch.ceil(input, out=None) → Tensor:对输入 input 张量每个元素向上取整, 即取不小于每个元素的最小整数,并返回结果到输出
import torch
a = torch.randn(1, 3)
print(a)
print( torch.ceil(a))
# tensor([[-0.3937, -0.3130, -0.9265]])
# tensor([[-0., -0., -0.]])
  • torch.clamp(input, min, max, out=None) → Tensor:将输入 input 张量每个元素的值保留在区间 [min,max],并返回结果到一个新张量
import torch
a = torch.randn(1, 3)
print(a)
print(torch.clamp(a, min=-0.5, max=0.5))
# tensor([[-0.4676,  0.2514, -0.8236]])
# tensor([[-0.4676,  0.2514, -0.5000]])
  • torch.clamp(input, *, min, out=None) → Tensor:将输入 input 张量每个元素的限制到不小于 min ,并返回结果到一个新张量
import torch
a = torch.randn(1, 3)
print(a)
print(torch.clamp(a, min=-0.5))
# tensor([[ 0.7176,  1.4181, -1.0655]])
# tensor([[ 0.7176,  1.4181, -0.5000]])
  • torch.clamp(input, *, max, out=None) → Tensor:将输入 input 张量每个元素的限制到不大于 max ,并返回结果到一个新张量
import torch
a = torch.randn(1, 3)
print(a)
print(torch.clamp(a, max=0.5))
# tensor([[-1.5973,  1.8991, -1.1537]])
# tensor([[-1.5973,  0.5000, -1.1537]])
  • torch.floor(input, out=None) → Tensor:返回不小于元素的最大整数的一个新张量
import torch
a = torch.randn(1, 3)
print(a)
print(torch.floor(a))
# tensor([[-1.0200, -0.3587,  0.2697]])
# tensor([[-2., -1.,  0.]])
  • torch.fmod(input, divisor, out=None) → Tensor:计算除法余数
import torch
print(torch.fmod(torch.Tensor([-3, -2, -1, 1, 2, 3]), 2))
# tensor([-1., -0., -1.,  1.,  0.,  1.])
print( torch.fmod(torch.Tensor([1, 2, 3, 4, 5]), 1.5))
# tensor([1.0000, 0.5000, 0.0000, 1.0000, 0.5000])
  • torch.frac(tensor, out=None) → Tensor:返回每个元素的分数部分
import torch
print(torch.frac(torch.Tensor([1, 2.5, -3.2])))
# tensor([ 0.0000,  0.5000, -0.2000])
  • torch.lerp(start, end, weight, out=None):对两个张量以 start,end 做线性插值, 将结果返回到输出张量在这里插入图片描述
import torch
start = torch.arange(1, 5, dtype=torch.float)
end = torch.arange(1, 5).fill_(10).float()
print(torch.lerp(start, end, 0.5))
# tensor([5.5000, 6.0000, 6.5000, 7.0000])
  • torch.log1p(input, out=None) → Tensor:计算 input+1 的自然对数 y i = l o g ( x i + 1 ) y_i=log(x_i+1) yi=log(xi+1)
    注意:对值比较小的输入,此函数比 torch.log()更准确
import torch
a = torch.randn(5)
print(a)
print(torch.log1p(a))
print(torch.log(a))
# tensor([-0.3608,  0.0354,  1.2058, -0.4821,  0.2975])
# tensor([-0.4475,  0.0348,  0.7911, -0.6580,  0.2605])
# tensor([    nan, -3.3399,  0.1871,     nan, -1.2122])
  • torch.mul(input, value, out=None):用标量值 value 乘以输入 input 的每个元素,并返回一个新的结果张量。out=input∗value
    其他如torch.mul(input, other, out=None)也类似, o u t i = i n p u t i ∗ o t h e r i out_i=input_i ∗other_i outi=inputiotheri
import torch
a = torch.randn(3)
print(a)
print(torch.mul(a, 100))
# tensor([-1.1483,  0.7603,  0.2267])
# tensor([-114.8307,   76.0285,   22.6663])
b = torch.randn(2,8)
c = torch.randn(2,8)
print(b)
# tensor([[-1.0345,  0.8859,  0.5654,  0.4982, -0.0614,  0.6969, -0.4431, -0.2085],
#         [-1.5169, -1.2309,  0.9151,  1.0333, -1.6818, -0.0255, -0.6141,  0.0024]])
print(c)
# tensor([[ 1.4937,  0.8986, -0.4582, -0.9688, -0.4365, -1.1407, -0.6577, -0.4107],
#         [-0.1320,  1.9057,  0.4312, -0.1754, -1.2658, -1.4375,  0.4910, -0.2760]])
print(torch.mul(b, c))
# tensor([[-1.5453e+00,  7.9602e-01, -2.5904e-01, -4.8265e-01,  2.6809e-02,-7.9496e-01,  2.9144e-01,  8.5623e-02],
#         [ 2.0022e-01, -2.3457e+00,  3.9463e-01, -1.8124e-01,  2.1289e+00, 3.6688e-02, -3.0151e-01, -6.7568e-04]])
  • torch.neg(input, out=None) → Tensor:返回输入 input 张量按元素取负。 即, o u t = − 1 ∗ i n p u t out=−1∗input out=1input
import torch
a = torch.randn(5)
print(a)
print(torch.neg(a))
# tensor([-0.0509, -0.0584, -1.5789, -0.7119, -1.0600])
# tensor([0.0509, 0.0584, 1.5789, 0.7119, 1.0600])
  • torch.pow(input, exponent, out=None):对输入 input 的按元素求 exponent 次幂值,并返回结果张量。
import torch
a = torch.arange(4)
print(a)
print(a.pow(2))
print(torch.pow(a,2))
# tensor([0, 1, 2, 3])
# tensor([0, 1, 4, 9])
# tensor([0, 1, 4, 9])
  • torch.reciprocal(input, out=None) → Tensor:返回输入 input 张量每个元素的倒数,即 1.0/x。
import torch
a =  torch.randn(4)
print(a)
print(torch.reciprocal(a))
# tensor([ 0.8168, -0.2910,  0.2003, -1.1824])
# tensor([ 1.2243, -3.4360,  4.9919, -0.8458])
  • torch.remainder(input, divisor, out=None) → Tensor:返回输入 input 张量每个元素的除法余数。 除数与被除数可能同时包含整数或浮
    点数。余数与除数有相同的符号。
import torch
print(torch.remainder(torch.Tensor([-3, -2, -1, 1, 2, 3]), 2))
print(torch.remainder(torch.Tensor([1, 2, 3, 4, 5]), 1.5))
# tensor([1., -0., 1., 1., 0., 1.])
# tensor([1.0000, 0.5000, 0.0000, 1.0000, 0.5000])
  • torch.round(input, out=None) → Tensor:返回输入 input 张量每个元素舍入到最近的整数
import torch
a = torch.randn(4)
print(a)
print(torch.round(a))
# tensor([-0.2787, -0.8219, -0.5392,  0.2893])
# tensor([-0., -1., -1.,  0.])
  • torch.rsqrt(input, out=None) → Tensor:返回输入 input 张量每个元素的平方根倒数
import torch
a = torch.randn(4)
print(a)
print(torch.rsqrt(a))
# tensor([ 1.1742,  0.8009,  0.5737, -1.8820])
# tensor([0.9228, 1.1174, 1.3203,    nan])
  • torch.sigmoid(input, out=None) → Tensor:返回输入 input 张量每个元素的 sigmoid 值
import torch
a = torch.randn(4)
print(a)
print(torch.sigmoid(a))
# tensor([-0.4793,  0.1937, -0.4210, -1.5299])
# tensor([0.3824, 0.5483, 0.3963, 0.1780])
  • torch.sign(input, out=None) → Tensor:返回输入 input 张量每个元素的正负
import torch
a = torch.randn(4)
print(a)
print(torch.sign(a))
# tensor([-1.1192,  1.1175,  1.1149, -0.8106])
# tensor([-1.,  1.,  1., -1.])
  • torch.trunc(input, out=None) → Tensor:有符号数的小数部分被舍弃
import torch
a = torch.randn(4)
print(a)
print(torch.trunc(a))
# tensor([ 3.1237, -0.4230, -0.9364,  0.4126])
# tensor([3., -0., -0., 0.])
  • torch.sqrt(input, out=None) → Tensor:返回输入 input 张量每个元素的平方根
import torch
a = torch.randn(4)
print(a)
print(torch.sqrt(a))
# tensor([-0.5017, -0.4790,  0.6769,  0.8054])
# tensor([   nan,    nan, 0.8228, 0.8974])

参考目录

https://blog.csdn.net/weixin_40920183/article/details/119814472

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要下载PyTorch常用函数手册的PDF,可以按照以下步骤进行: 1. 打开浏览器,进入搜索引擎网站。 2. 在搜索框中输入关键词“PyTorch常用函数手册PDF下载”。 3. 根据搜索结果,选择一个可信的网站,例如官方文档或知名的技术博客。 4. 进入所选网站后,使用网站提供的搜索功能,搜索“PyTorch常用函数手册PDF”。 5. 在搜索结果中找到符合需求的链接或按钮,一般会有一个下载文件的选项。 6. 点击链接或按钮,开始下载PyTorch常用函数手册的PDF文件。 7. 下载完成后,可以在浏览器的下载文件夹中找到该PDF文件。 8. 双击该文件,使用所选的PDF阅读器打开手册。 如果无法找到可信的网站或下载链接,可以尝试以下方法: 1. 在搜索引擎中搜索“PyTorch官方文档”。 2. 进入PyTorch官方网站,找到文档页面或搜索框。 3. 在文档页面或搜索框中输入“常用函数手册”等相关关键词。 4. 在搜索结果中找到所需的PyTorch常用函数手册链接。 5. 点击链接,开始在线浏览该手册。 6. 在浏览器上方或手册界面中,应该有下载或导出为PDF的选项。 7. 点击下载或导出为PDF,将手册保存为PDF文件。 8. 打开下载的PDF文件,使用所选的PDF阅读器阅读PyTorch常用函数手册。 需要注意的是,PyTorch官方网站和其它可信来源的手册都是最可靠的资料,因此最好选择官方文档或来自官方推荐的网站。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值