PyTorch 笔记(12)— Tensor 持久化、向量化、torch.set_num_threads、torch.set_printoptions
1. 持久化
在 PyTorch中 ,以下對象可以持久化到硬盤,并能通過相應的方法加載到內存中:
TensorVariablenn.ModuleOptimizer
本質上上述這些信息最終都是保存成 Tensor 。
Tensor 的保存和加載也比較簡單,使用 t.save 個 t.load 即可完成相應的功能,在 save、load時可指定使用的 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) 方法加載保存的數據。對于 Module 和 Optimizer 對象,這里建議保存對應的 state_dict ,而不是直接保存整個Module/Optimizer 對象。Optimizer 對象保存的主要是參數,以及動量信息,通過加載之前的動量信息,能夠有效地減少模型震蕩,下面舉例說明。
a = t.Tensor(3, 4)
if t.cuda.is_available():a = a.cuda(0) # 把a轉為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'模型和優化器的所有參數'
)
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 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)
可以看出兩者運算速度會差很大,因此實際使用中盡量使用內建函數,這些函數底層是由 C/C++ 實現,能通過執行底層優化實現高效運算。
3. Tensor 特點
- 大多數的
t.function都有一個參數out,這時產生的結果會保存在out指定的tensor之中; t.set_num_threads可以設置PyTorch進行CPU多線程并行計算時所占用的線程數,用來限制PyTorch所占用的CPU數目;t.set_printoptions可以用來設置打印tensor時的數值精度和格式;
總結
以上是生活随笔為你收集整理的PyTorch 笔记(12)— Tensor 持久化、向量化、torch.set_num_threads、torch.set_printoptions的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyTorch 笔记(11)— Tens
- 下一篇: 问个电影,历史,亚特兰蒂斯相关的