PyTorch笔记(12)—Tensor持久化、向量化、torch.t_num_thr。。。

更新时间:2023-06-06 03:05:06 阅读: 评论:0

PyTorch笔记(12)—Tensor持久化、向量化、torch.t_num_thr。。。
1. 持久化
在 PyTorch中 ,以下对象可以持久化到硬盘,并能通过相应的⽅法加载到内存中:面部美白
Tensor
carrot的复数形式
Variable
nn.Module
Optimizer
本质上上述这些信息最终都是保存成 Tensor 。
Tensor 的保存和加载也⽐较简单,使⽤ t.save 个 t.load 即可完成相应的功能,在 save、load时可指定使⽤的 pickle 模块,在 load 时还可将 GPU Tensor 映射到 CPU 或其它 GPU 上。
In [3]:import torch as t
In [4]: a = t.arange(0,10)
In [5]:if t.cuda.is_available():
...:    a = a.cuda(1)
...:    t.save("a.pth")
...:    b = t.load("a.pth")
...:    c = t.load("a.pth", map_location=lambda storage, loc:storage)
...:    d = t.load("a.pth", map_location={'cuda:1':'cuda:0'})
我们可以通过 t.save(obj, file_name) 等⽅法保存任意可序列化的对象,然后通过 obj = t.load(file_name) ⽅法加载保存的数据。对于 Module 和 Optimizer 对象,这⾥建议保存对应的 state_dict ,⽽不是直接保存整个Module/Optimizer 对象。Optimizer 对象保存的主要是参数,以及动量信息,通过加载之前的动量信息,能够有效地减少模型震荡,下⾯举例说明。
a = t.Tensor(3,4)
if t.cuda.is_available():
a = a.cuda(0) # 把a转为GPU0上的tensor,av girl
t.save(a,'a.pth')
# 加载为b,存储于GPU0上(因为保存时tensor就在GPU0上)
b = t.load('a.pth')
# 加载为c,存储于CPU
c = t.load('a.pth', map_location=lambda storage, loc: storage)
# 加载为d,存储于GPU0上
d = t.load('a.pth', map_location={'cuda:1':'cuda:0'})
t.t_default_tensor_type('torch.FloatTensor')
dels import SqueezeNet
model =SqueezeNet()
# module的state_dict是⼀个字典
model.state_dict().keys()
输出:
odict_keys(['features.0.weight','features.0.bias','features.3.squeeze.weight','features.3.squeeze.bias','pand1x1.weight','pand1 x1.bias','pand3x3.weight','pand3x3.bias','features.4.squeeze.weight','features.4.squeeze.bias','pand1x1.weight', 'pand1x1.bias','pand3x3.weight','pand3x3.bias','features.5.squeeze.weight','features.5.squeeze.bias','features.5. expand1x1.weight','pand1x1.bias','pand3x3.weight','pand3x3.bias','features.7.squeeze.weight','features.7.squee ze.bias','pand1x1.weight','pand1x1.bias','pand3x3.weight','pand3x3.bias','features.8.squeeze.weigh t','features.8.squeeze.bias','pand1x1.weight','pand1x1.bias','pand3x3.weight','pand3x3.bias','featur es.9.squeeze.weight','features.9.squeeze.bias','pand1x1.weight','pand1x1.bias','pand3x3.weight','p and3x3.bias','features.10.squeeze.weight','features.10.squeeze.bias','pand1x1.weight','pand1x1.bias','pand3 x3.weight','pand3x3.bias','features.12.squeeze.weight','features.12.squeeze.bias','pand1x1.weight','pand1x1 .bias','pand3x3.weight','pand3x3.bias','classifier.1.weight','classifier.1.bias'])
# Module对象的保存与加载
redflagt.save(model.state_dict(),'squeezenet.pth')
trampling
裙子的英文怎么说model.load_state_dict(t.load('squeezenet.pth')) # <All keys matched successfully>
original是什么意思
optimizer = t.optim.Adam(model.parameters(), lr=0.1)
t.save(optimizer.state_dict(),'optimizer.pth')
optimizer.load_state_dict(t.load('optimizer.pth'))
all_data =dict(
optimizer = optimizer.state_dict(),
model = model.state_dict(),
研究生网上报名时间info = u'模型和优化器的所有参数'
)
t.save(all_data,'all.pth')
all_data = t.load('all.pth')
all_data.keys()
输出:
dict_keys(['optimizer', 'model', 'info'])
2. 向量化
向量化计算是⼀种特殊的并⾏计算⽅式,⼀般程序在同⼀时间只执⾏⼀个操作指令,⽽向量化计算可在同⼀时间执⾏多个操作,通常是对不同的数据执⾏同样的⼀个或⼀批指令,或者说把指令应⽤于⼀个数组或向量上。
向量化可极⼤地提⾼科学运算效率,在科学计算中应当极⼒避免使⽤ Python 原⽣的 for 循环,尽量使⽤向量化的数值运算。
In [1]:import torch as turname是什么意思
In [2]:def for_loop_add(x, y):
...:    result =[]
.
..:for i, j in zip(x, y):
...:        result.append(i+j)
男士衣着打扮...:return t.Tensor(result)
...:
In [3]: x = t.zeros(100)
In [4]: y = t.ones(100)
In [5]:%timeit -n 10 for_loop_add(x, y)
The slowest run took 44.68 times longer than the fastest. This could mean that an intermediate result is being cached.
5.04 ms ± 10.1 ms per loop (mean ± std. dev. of 7 runs,10 loops each)
In [6]:%timeit -n 10 x + y
The slowest run took 59.81 times longer than the fastest. This could mean that an intermediate result is being cached.
25.8 µs ± 52.9 µs per loop (mean ± std. dev. of 7 runs,10 loops each)
可以看出两者运算速度会差很⼤,因此实际使⽤中尽量使⽤内建函数,这些函数底层是由 C/C++ 实现,能通过执⾏底层优化实现⾼效运算。
3. Tensor 特点
⼤多数的 t.function 都有⼀个参数 out ,这时产⽣的结果会保存在 out 指定的 tensor 之中;
t.t_num_threads 可以设置 PyTorch 进⾏ CPU 多线程并⾏计算时所占⽤的线程数,⽤来限制 PyTorch 所占⽤的 CPU 数⽬;
t.t_printoptions可以⽤来设置打印 tensor 时的数值精度和格式;

本文发布于:2023-06-06 03:05:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/879462.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:保存   运算   加载   对象
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图