日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch 笔记(12)— Tensor 持久化、向量化、torch.set_num_threads、torch.set_printoptions 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 持久化

PyTorch中 ,以下對象可以持久化到硬盤,并能通過相應(yīng)的方法加載到內(nèi)存中:

  • Tensor
  • Variable
  • nn.Module
  • Optimizer

本質(zhì)上上述這些信息最終都是保存成 Tensor

Tensor 的保存和加載也比較簡單,使用 t.savet.load 即可完成相應(yīng)的功能,在 saveload時可指定使用的 pickle 模塊,在 load 時還可將 GPU Tensor 映射到 CPU 或其它 GPU 上。

In [3]: import torch as tIn [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) 方法加載保存的數(shù)據(jù)。對于 ModuleOptimizer 對象,這里建議保存對應(yīng)的 state_dict ,而不是直接保存整個Module/Optimizer 對象。Optimizer 對象保存的主要是參數(shù),以及動量信息,通過加載之前的動量信息,能夠有效地減少模型震蕩,下面舉例說明。

a = t.Tensor(3, 4)
if t.cuda.is_available():a = a.cuda(0) # 把a轉(zhuǎn)為GPU0上的tensor,t.save(a,'a.pth')# 加載為b, 存儲于GPU0上(因為保存時tensor就在GPU0上)b = t.load('a.pth')# 加載為c, 存儲于CPUc = t.load('a.pth', map_location=lambda storage, loc: storage)# 加載為d, 存儲于GPU0上d = t.load('a.pth', map_location={'cuda:1':'cuda:0'})t.set_default_tensor_type('torch.FloatTensor')
from torchvision.models 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', 'features.3.expand1x1.weight', 'features.3.expand1x1.bias', 'features.3.expand3x3.weight', 'features.3.expand3x3.bias', 'features.4.squeeze.weight', 'features.4.squeeze.bias', 'features.4.expand1x1.weight', 'features.4.expand1x1.bias', 'features.4.expand3x3.weight', 'features.4.expand3x3.bias', 'features.5.squeeze.weight', 'features.5.squeeze.bias', 'features.5.expand1x1.weight', 'features.5.expand1x1.bias', 'features.5.expand3x3.weight', 'features.5.expand3x3.bias', 'features.7.squeeze.weight', 'features.7.squeeze.bias', 'features.7.expand1x1.weight', 'features.7.expand1x1.bias', 'features.7.expand3x3.weight', 'features.7.expand3x3.bias', 'features.8.squeeze.weight', 'features.8.squeeze.bias', 'features.8.expand1x1.weight', 'features.8.expand1x1.bias', 'features.8.expand3x3.weight', 'features.8.expand3x3.bias', 'features.9.squeeze.weight', 'features.9.squeeze.bias', 'features.9.expand1x1.weight', 'features.9.expand1x1.bias', 'features.9.expand3x3.weight', 'features.9.expand3x3.bias', 'features.10.squeeze.weight', 'features.10.squeeze.bias', 'features.10.expand1x1.weight', 'features.10.expand1x1.bias', 'features.10.expand3x3.weight', 'features.10.expand3x3.bias', 'features.12.squeeze.weight', 'features.12.squeeze.bias', 'features.12.expand1x1.weight', 'features.12.expand1x1.bias', 'features.12.expand3x3.weight', 'features.12.expand3x3.bias', 'classifier.1.weight', 'classifier.1.bias'])
# Module對象的保存與加載
t.save(model.state_dict(), 'squeezenet.pth')
model.load_state_dict(t.load('squeezenet.pth'))	# <All keys matched successfully>
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'模型和優(yōu)化器的所有參數(shù)'
)
t.save(all_data, 'all.pth')all_data = t.load('all.pth')
all_data.keys() 

輸出:

dict_keys(['optimizer', 'model', 'info'])

2. 向量化

向量化計算是一種特殊的并行計算方式,一般程序在同一時間只執(zhí)行一個操作指令,而向量化計算可在同一時間執(zhí)行多個操作,通常是對不同的數(shù)據(jù)執(zhí)行同樣的一個或一批指令,或者說把指令應(yīng)用于一個數(shù)組或向量上。

向量化可極大地提高科學運算效率,在科學計算中應(yīng)當極力避免使用 Python 原生的 for 循環(huán),盡量使用向量化的數(shù)值運算。

In [1]: import torch as tIn [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)

可以看出兩者運算速度會差很大,因此實際使用中盡量使用內(nèi)建函數(shù),這些函數(shù)底層是由 C/C++ 實現(xiàn),能通過執(zhí)行底層優(yōu)化實現(xiàn)高效運算。

3. Tensor 特點

  • 大多數(shù)的 t.function 都有一個參數(shù) out ,這時產(chǎn)生的結(jié)果會保存在 out 指定的 tensor 之中;
  • t.set_num_threads 可以設(shè)置 PyTorch 進行 CPU 多線程并行計算時所占用的線程數(shù),用來限制 PyTorch 所占用的 CPU 數(shù)目;
  • t.set_printoptions可以用來設(shè)置打印 tensor 時的數(shù)值精度和格式;

總結(jié)

以上是生活随笔為你收集整理的PyTorch 笔记(12)— Tensor 持久化、向量化、torch.set_num_threads、torch.set_printoptions的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。