Pytorch to(device)用法
如下所示:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
這兩行代碼放在讀取數(shù)據(jù)之前。
mytensor = my_tensor.to(device)
這行代碼的意思是將所有最開始讀取數(shù)據(jù)時(shí)的tensor變量copy一份到device所指定的GPU上去,之后的運(yùn)算都在GPU上進(jìn)行。
這句話需要寫的次數(shù)等于需要保存GPU上的tensor變量的個(gè)數(shù);一般情況下這些tensor變量都是最開始讀數(shù)據(jù)時(shí)的tensor變量,后面衍生的變量自然也都在GPU上
如果是多個(gè)GPU
在代碼中的使用方法為:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Model()
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model,device_ids=[0,1,2])
model.to(device)
Tensor總結(jié)
(1)Tensor 和 Numpy都是矩陣,區(qū)別是前者可以在GPU上運(yùn)行,后者只能在CPU上;
(2)Tensor和Numpy互相轉(zhuǎn)化很方便,類型也比較兼容
(3)Tensor可以直接通過print顯示數(shù)據(jù)類型,而Numpy不可以
把Tensor放到GPU上運(yùn)行
if torch.cuda.is_available(): h = g.cuda() print(h)
torch.nn.functional Convolution函數(shù) torch.nn.functional.vonv1d(input,weight,bias=None,stride=1,padding=0,dilation=1,groups=1) torch.nn.functional.conv2d(input,weight,bias=None,stride=1,padding=0,dilation=1,group=1) parameter: input --輸入張量(minibatch * in_channels * iH * iW)-weights-– 過濾器張量 (out_channels, in_channels/groups, kH, kW) - bias – 可選偏置張量 (out_channels) - stride – 卷積核的步長,可以是單個(gè)數(shù)字或一個(gè)元組 (sh x sw)。默認(rèn)為1 - padding – 輸入上隱含零填充。可以是單個(gè)數(shù)字或元組。 默認(rèn)值:0 - groups – 將輸入分成組,in_channels應(yīng)該被組數(shù)除盡 >>> # With square kernels and equal stride >>> filters = autograd.Variable(torch.randn(8,4,3,3)) >>> inputs = autograd.Variable(torch.randn(1,4,5,5)) >>> F.conv2d(inputs, filters, padding=1)
Pytorch中使用指定的GPU
(1)直接終端中設(shè)定
CUDA_VISIBLE_DEVICES=1
(2)python代碼中設(shè)定:
import os os.environ['CUDA_VISIBLE_DEVICE']='1'
(3)使用函數(shù)set_device
import torch torch.cuda.set_device(id) Pytoch中的in-place
in-place operation 在 pytorch中是指改變一個(gè)tensor的值的時(shí)候,不經(jīng)過復(fù)制操作,而是在運(yùn)來的內(nèi)存上改變它的值??梢园阉Q為原地操作符。
在pytorch中經(jīng)常加后綴 “_” 來代表原地in-place operation, 比如 .add_() 或者.scatter()
python 中里面的 += *= 也是in-place operation。
下面是正常的加操作,執(zhí)行結(jié)束加操作之后x的值沒有發(fā)生變化:
import torch x=torch.rand(2) #tensor([0.8284, 0.5539]) print(x) y=torch.rand(2) print(x+y) #tensor([1.0250, 0.7891]) print(x) #tensor([0.8284, 0.5539])
下面是原地操作,執(zhí)行之后改變了原來變量的值:
import torch x=torch.rand(2) #tensor([0.8284, 0.5539]) print(x) y=torch.rand(2) x.add_(y) print(x) #tensor([1.1610, 1.3789])
以上這篇Pytorch to(device)用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jupyter 使用Pillow包顯示圖像時(shí)inline顯示方式
這篇文章主要介紹了jupyter 使用Pillow包顯示圖像時(shí)inline顯示方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
pyqt5 從本地選擇圖片 并顯示在label上的實(shí)例
今天小編就為大家分享一篇pyqt5 從本地選擇圖片 并顯示在label上的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Flask框架的學(xué)習(xí)指南之制作簡單blog系統(tǒng)
本文是Flask框架的學(xué)習(xí)指南系列文章的第二篇主要給大家講述制作一個(gè)簡單的小項(xiàng)目blog系統(tǒng)的過程,有需要的小伙伴可以參考下2016-11-11
python實(shí)現(xiàn)斷點(diǎn)調(diào)試的方法
本文主要介紹了python實(shí)現(xiàn)斷點(diǎn)調(diào)試的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python在for循環(huán)中更改list值的方法【推薦】
這篇文章主要介紹了Python在for循環(huán)中更改list值的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08

