pytorch .t(pytorch documentation)

發(fā)布時(shí)間:2024-02-13
本文主要介紹title: pytorch.t (pytorc檔),下面一起看看title: pytorch.t (pytorc檔)相關(guān)資訊。
本節(jié)參考小土丘的pytorch入門視頻教程。
現(xiàn)有模型使用和修改#pytorch框架提供了很多現(xiàn)有模型,其中torchvision.models包包含了很多視覺(圖像)領(lǐng)域的模型,如下圖所示:
以vgg16為例說(shuō)明如何使用和更改現(xiàn)有模型:
預(yù)訓(xùn)練為真,返回imagenet上預(yù)訓(xùn)練的模型;當(dāng)pregress為true時(shí),在下載模型時(shí),進(jìn)度條將通過(guò)標(biāo)準(zhǔn)錯(cuò)誤流輸出。
創(chuàng)建并運(yùn)行以下腳本:
從torchvision導(dǎo)入模型#創(chuàng)建預(yù)訓(xùn)練模型并輸出進(jìn)度vgg 16 _ pr: https:// download . py torch . org/models/vgg 16-397923 af . pth 致c:\users\winlsr/。cache \ torch \ hub \ check points \ vgg 16-397923 af . pth 100.0% vgg((0): conv2d(3,64,k: relu(in plac: conv2d(64,64,k: relu(in plac: maxpool 2d(kernel _ size = 2,inilation=1,c: conv 2d(64,128,k: relu(in plac: conv 2d(128,128,k: relu(in plac: maxpool 2d(kernel _ size = 2,stride=2,padding=0,exploation = 1,c: relu(in plac: conv 2d(512,512,k: relu(in plac: maxpool 2d(kernel _ size = 2,stride=2,padding=0,exploation = 1,c: conv 2d(512,512,kernel_size=(3,3)。com dropout(p=0.5,in plac: lin: relu(in plac: dropout(p = 0.5,in plac: linear(in _ features = 4096,out_features=1000,bias = true))如上圖輸出所示,vgg代表模型的類名,features是vgg中包含的順序組件(module),avgpool是adaptiveavgpool
創(chuàng)建并運(yùn)行以下腳本:
從torch vision導(dǎo)入模型從torch import nn #創(chuàng)建預(yù)訓(xùn)練模型并輸出進(jìn)度vgg 16 _ pr:序列((0): conv2d(3,64,k: relu(in plac: conv2d(64,64,k: relu(in plac: maxpool 2d(kernel _ size = 2,stride=2,padding=0,dilation=1,c: conv 2d(64,128,k: relu(in plac: conv 2d(128,128,k: relu(in plac: maxpool 2d(kernel _ size = 2om relu(in plac: conv 2d(512,512,k: relu(in plac: conv 2d(512,512,k: relu(in plac: maxpool 2d(kernel _ size = 2,stride=2,padding=0,exploation = 1,c: relu(in plac: dropout(p = 0.5,in plac: lin: relu(in plac: dropout(p = 0.5,in plac: lin: linear(in _ features = 1000,out _ features = 10,bias = true) #成功添加
從torch vision導(dǎo)入模型從torch import nn #創(chuàng)建預(yù)訓(xùn)練模型并輸出進(jìn)度vgg 16 _ pre-trained = models . vgg 16(pre-trained = true,progress = true) #創(chuàng)建未訓(xùn)練模型。不輸出進(jìn)度vgg 16 = models . vgg 16(retrained = false,progress = false)# del vgg 16 _ retrained . features # add vgg 16 _ retrained . classifi:自適應(yīng)avgpool2d (output _ size = (7,7))(classifi:順序((0):線性(in_f: soft max(dim = non:退學(xué)者(p=0.5,in plac:線性(in_f: relu(in plac:退學(xué)者(p = 0.5,in plac:線性(in_features=4096,out_features=1000,bias = true
執(zhí)行以下腳本:
from _ 07 _ cifar 10 _ model . cifar 10 _ model導(dǎo)入mymodel導(dǎo)入torch cifar 10 _ model = mymodel# mode 1:保存模型參數(shù)torch.save(cifar10_model, cifar10 _ model.pth )#模式二:只保存參數(shù)(官方推薦)火炬。保存(cifar10 _ model。state _ dict, cifa r10 _ model _ state _ dict . pth ).執(zhí)行成功后,會(huì)生成腳本文件所在的目錄:cifar10_model.pth,和。
恢復(fù)模式1保存的模型:
導(dǎo)入火炬#模式1 cifar10 _ model =火炬。加載( cifar10 _型號(hào)。pth )打印(cifar10 _ mod:序列((0): conv 2d(3,32,k: maxpool 2d(kernel _ size = 2,stride=2,padding=0,dilation=1,c: conv 2d(32,32,k: maxpool 2d(kernel _ size = 2,stride=2,padding=0,dilation=1,c: conv 2d(32
導(dǎo)入torch from _ 07 _ cifar 10 _ model . cifar 10 _ model import my model # mode 2(官方推薦)cifar 10 _ model = my modelcifar 10 _ model . load _ state _ dict(torch . load( cifar10 _ model _ dict.pth ))打印。
my mod:序列((0): conv2d(3,32,k: maxpool 2d(kernel_size= 2,stride=2,padding=0,dilation=1,c: conv 2d(32,32,k: maxpool 2d(kernel _ size = 2,stride=2,padding=0,dilation=1,c: conv 2d(32,64,kernel _ size = .下面將是我們模型的完整訓(xùn)練。培訓(xùn)代碼如下:
導(dǎo)入timefrom torch.utils導(dǎo)入tensor board from torch . utils . data導(dǎo)入dataloader from _ 07 _ cifar 10 _ model . cifar 10 _ model導(dǎo)入mymodel導(dǎo)入torch vis io import torch . nnif _ _ nam: start _ tim測(cè)試數(shù)據(jù)集transform = torch vision . transforms . pose({ torch vision . transforms . to tansor})。train _ data = torch vision . datasets . cifar 10( 。/dataset ,train=true,transform=transform,download = true)test _ data = torch vision . datasets . cifar 10( 。/datas: { } 。格式(train _ data _ l測(cè)試集的長(zhǎng)度{ : } 。format(test _ data _ len))# data loader train _ data loader = data loader(dataset = train _ data,batch_size=64,shuffle = true,num _ workers = 16)test _ data loader = data loader(dataset = test _ data,batch _ size = 64,shuffle=true,num _ workers = 16)# create network cifar 10 _ model = my model# create loss func = torch . nn . crossover# create optimizer #學(xué)習(xí)率,科學(xué)計(jì)數(shù)的形式便于改變learning _ rate = 1e-2writer =張力板。總結(jié)作者( 日志 )為我在range(epoch):打印( - #模型進(jìn)入訓(xùn)練模式。方法在當(dāng)前模型(加上是個(gè)好習(xí)慣)cifar10 _ mod:圖像中的數(shù)據(jù),targets = data outputs = cifar 10 _ model(images)loss = loss _ func(outputs,targets) #清空上一輪計(jì)算的梯度優(yōu)化器。zero _ grad #反向傳播計(jì)算梯度損失. backward #優(yōu)化器優(yōu)化參數(shù)(執(zhí)行梯度下降)optimizer . steptotal _ train _ step = 1 writer . add _ scalar( 培訓(xùn)/損失 ,loss.it:打印( 訓(xùn)練時(shí)報(bào): { },loss: { } 。格式(total _ train _ step,loss。item)total _ test _ loss = 0.0 total _ accuracy = 0.0 #每輪歷元后,在測(cè)試集# 測(cè)試上計(jì)算模型的損失性能時(shí),不需要計(jì)算梯度。計(jì)算速度可以加快#模型進(jìn)入驗(yàn)證(測(cè)試)模式,方法在當(dāng)前模型中可有可無(wú)(加上是個(gè)好習(xí)慣)cifar10 _ model。eval與torch.no_grad:一起用于t:映像中的數(shù)據(jù),targets =數(shù)據(jù)輸出= cifar 10 _ model(images)loss = loss _ func(outputs,targets) total_test_虧損=虧損。item準(zhǔn)確度=(輸出。argmax (1) = = targets)。sumtotal _ accuracy = accuracy print(;測(cè)試精度:{ 0 } 。format(total _ accuracy/test _ data _ len))writer . add _ scalar( 測(cè)試/損失 ,total_test_loss,i)writer . add _ scalar( 測(cè)試/準(zhǔn)確性和,total_accuracy/test_data_len,i) #保存模型torch . save(cifar 10 _ model . state _ dict)每輪訓(xùn)練結(jié)束后, cifr 10 _ model _ state _ dict _ { } _ epoch . pth 。format(i))writer . closeend _ time = time . timeprint( 耗時(shí):{ } 。格式(結(jié)束時(shí)間-開始時(shí)間))。在上面的代碼中調(diào)用模型的train和eval方法,主要是對(duì)模型中的dropout和batchnorm等模塊有用(如果有的話)。官方解釋如下:
張量板可視化結(jié)果如下:
與gpu #的學(xué)生誰(shuí)不 沒有g(shù)pu可以想辦法用google colab,它提供免費(fèi)的gpu使用時(shí)間,類似于jupyter notebook。
用gpu訓(xùn)練很簡(jiǎn)單:
方法1:。cuda#只需要調(diào)用。關(guān)于網(wǎng)絡(luò)模型、數(shù)據(jù)(輸入、標(biāo)記)和損失函數(shù)的cuda方法:
導(dǎo)入timefrom torch.utils導(dǎo)入tensor board from torch . utils . data導(dǎo)入dataloaderfrom _ 07 _ cifar 10 _ model . cifar 10 _ model導(dǎo)入mymodel導(dǎo)入torchvis導(dǎo)入torch。nnif _ _ nam:開始時(shí)間=時(shí)間。tim測(cè)試數(shù)據(jù)集轉(zhuǎn)換= torch vision。轉(zhuǎn)換姿勢(shì)。({ torch vision . transforms . totensor})train _ data = torch vision . datasets . cifar 10( 。/dataset ,train=true,transform=transform,download = true)test _ data = torch vision . datasets . cifar 10( 。/datas: { } 。格式(train _ data _ l測(cè)試集的長(zhǎng)度{ : } 。format(test _ data _ len))# data loader train _ data loader = data loader(dataset = train _ data,batch_size=64,shuffle = true,num _ workers = 16)test _ data loader = data loader(dataset = test _ data,batch _ size = 64,shuffle=true,num_workers=16) #創(chuàng)建網(wǎng)絡(luò)cifar10 _ model = mymodel如果torch.cuda.is_availabl: cifar 10 _ model = cifar 10 _ model . cuda#創(chuàng)建損失函數(shù)loss _ func = torch.nn.crossover如果torch.cuda.is_availabl:損失 科學(xué)計(jì)數(shù)的形式方便更改learning _ rate = 1e-2 optimizer = torch . optim . sgd(cifar 10 _ model . parameters、lr = learni。ng_rate) #訓(xùn)練次數(shù)total_train_step = 0 #訓(xùn)練輪次epoch = 20 #創(chuàng)建tensorboard摘要writer = tensorboard??偨Y(jié)作者( 日志 ).為我在range(epoch):打印( -方法在當(dāng)前模型中可有可無(wú)(加上是個(gè)好習(xí)慣)cifar10 _ mod:圖像中的數(shù)據(jù),targets = data if torch.cuda.is_availabl:圖像= images . cudatargets = targets . cudaoutputs = cifar 10 _ model(images)loss = loss _ func(outputs,targets) #清空上一輪計(jì)算的梯度優(yōu)化器。zero _ grad #反向傳播計(jì)算梯度損失. backward #優(yōu)化器優(yōu)化參數(shù)(執(zhí)行梯度下降)optimizer . steptotal _ train _ step = 1 writer . add _ scalar( 培訓(xùn)/損失 ,loss.it:打印( 訓(xùn)練時(shí)報(bào): { },loss: { } 。格式(total _ train _ step,loss。item)total _ test _ loss = 0.0 total _ accuracy = 0.0 #每輪歷元后,在測(cè)試集# 測(cè)試上計(jì)算模型的損失性能時(shí),不需要計(jì)算梯度??梢约涌煊?jì)算速度#模型進(jìn)入驗(yàn)證(測(cè)試)模式,這在當(dāng)前模型cifar10_mod:圖像中數(shù)據(jù)的th torch.no_grad:,targets = data if torch.cuda.is_availabl:圖像= images . cudatargets = targets . cudaoutputs = cifar 10 _ model(圖像)loss = loss_func(輸出,目標(biāo))total _ test _ loss = loss。item準(zhǔn)確度=(輸出。argmax (1) = = targets)。sumtotal _ accuracy = accuracy print(;測(cè)試準(zhǔn)確度:{ } 。format(total _ accuracy/test _ data _ len))writer . add _ scalar( 測(cè)試/損失 ,total_test_loss,i)writer . add _ scalar( 測(cè)試/準(zhǔn)確性和,total _ accuracy/test _ data _ len,i) #保存模型torch . save(cifar 10 _ model . state _ dict每輪訓(xùn)練結(jié)束后, cifr 10 _ model _ state _ dict _ { } _ epoch . pth 。format(i))writer . closeend _ time = time . timeprint( 耗時(shí):{ } 。格式(end_time-start_time))方法二:。敬。這種方法的好處是,你不僅可以使用gpu,還可以在有多個(gè)gpu的時(shí)候指定一個(gè)gpu。
如下所示:
# cpu cpu _ device = torch . device( cpu )# gpu只有一個(gè)顯卡,不需要指定哪個(gè)gpu _ device = torch . device( cuda )# 0 gpu gpu _ 0 _ device = torch . device( cuda : 0 )完整的代碼如下:
進(jìn)口port time from torch . utils import tensorboard from torch . utils . data import dataloader from _ 07 _ cifar 10 _ model . cifar 10 _ model import mymodel import torch visio nimport torch . nnif _ _ nam:設(shè)備= torch . device( cuda 如果torch . cuda . is _ availableelse cpu )start_tim測(cè)試數(shù)據(jù)集transform = torch vision . transforms . pose({ torch vision . transforms . to tansor})。train _ data = torch vision . datasets . cifar 10( 。/dataset ,train=true,transform=transform,download = true)test _ data = torch vision . datasets . cifar 10( 。/datas: { } 。格式(train _ data _ l測(cè)試集的長(zhǎng)度{ : } 。format(test _ data _ len))# data loader train _ data loader = data loader(dataset = train _ data,batch _ size = 64,shuffle = true,num _ workers = 16)test _ data loader = data loader(dataset = test _ data,batch _ size = 64,shuff。le=true,num_workers=16) #創(chuàng)建網(wǎng)絡(luò)cifar 10 _ model = mymodelcifar 10 _ model = cifar 10 _ model . to(device)# if torch.cuda.is_availabl: # cifar 10 _ model = cifar 10 _ model . cuda#創(chuàng)建損失函數(shù)loss _ func = torch . nn . crossentropylossloss _ func = loss _ func . to(device)# if torch.cuda.is_availabl: # loss _ func = loss _ func。cuda #創(chuàng)建優(yōu)化器#學(xué)習(xí)率,科學(xué)計(jì)數(shù)的形式方便更改learning _ rate = 1e-2 optimizer = torch . optim . sgd(cifar 10 _ model . parameters,lr=learning_rate) #訓(xùn)練次數(shù)total_train_step = 0 #訓(xùn)練輪次epoch = 20 #創(chuàng)建tensorboard摘要writer = tensorboard。總結(jié)作者( 日志 )為我在range(epoch):打印( -方法在當(dāng)前模型中可有可無(wú)(加上是個(gè)好習(xí)慣)cifar10 _ mod: images,targets = data images = images . to(device)targets = targets . to(device)# if torch.cuda.is_availabl: # images = images . cuda# targets = targets . cudaoutputs = cifar 10 _ model(images)loss = loss _ func(outputs,targets) #清空上一輪計(jì)算的梯度優(yōu)化器。zero _ grad #反向傳播計(jì)算梯度損失. backward #優(yōu)化器優(yōu)化參數(shù)(執(zhí)行梯度下降)optimizer . steptotal _ train _ step = 1 writer . add _ scalar( 培訓(xùn)/損失 ,loss.it:打印( 訓(xùn)練時(shí)報(bào): { },loss: { } 。格式(total _ train _ step,loss。item)total _ test _ loss = 0.0 total _ accuracy = 0.0 #每輪歷元后,在測(cè)試集# 測(cè)試上計(jì)算模型的損失性能時(shí),不需要計(jì)算梯度。計(jì)算速度可以加快#模型進(jìn)入驗(yàn)證(測(cè)試)模式,方法在當(dāng)前模型中可有可無(wú)(加上是個(gè)好習(xí)慣)cifar10 _ model。eval與torch.no_grad:一起用于t:映像中的數(shù)據(jù),targets = data images = images . to(device)targets = targets . to(device)# if torch.cuda.is_availabl: # images = images . cuda# targets = targets . cudaoutputs = cifar 10 _ model(images)loss = loss _ func(outputs,targets) total _ test _ loss = loss。item準(zhǔn)確度=(輸出。argmax (1) = = targets)。sumtotal _ accuracy = accuracy print(;測(cè)試準(zhǔn)確度:{ } 。格式(總精確度/測(cè)試數(shù)據(jù)長(zhǎng)度)。en))writer . add _ scalar( 測(cè)試/損失 ,total_test_loss,i)writer . add _ scalar( 測(cè)試/準(zhǔn)確性和,total_accuracy/test_data_len,i) #保存模型torch . save(cifar 10 _ model . state _ dict)每輪訓(xùn)練結(jié)束后, cifr10 _模型_狀態(tài)_字典_ {} _紀(jì)元。pth 。格式(i))編寫器。clos測(cè)試收藏中性能最好的模型來(lái)恢復(fù),然后在網(wǎng)上找一些圖片,看看我們的模型是否能正確分類。據(jù)tensorboard介紹,性能最好的模型是經(jīng)過(guò)18輪訓(xùn)練后的模型,可以達(dá)到65%左右的準(zhǔn)確率。預(yù)測(cè)情況如下:
根據(jù)cifar10數(shù)據(jù)集中的定義,狗的目標(biāo)是5,飛機(jī)的目標(biāo)是0:
預(yù)測(cè)代碼如下:
從pil進(jìn)口火炬進(jìn)口圖片進(jìn)口火炬視覺from _ 07 _ cifar 10 _ model . cifar 10 _ model進(jìn)口mymodeldog _ img _ path = 狗. png 飛機(jī)圖像路徑= 機(jī)場(chǎng)路dog _img_pil = image.open(dog _ img _ path)飛機(jī)_ img _ pil = image . open(飛機(jī)_img_path)#將4通道rgba轉(zhuǎn)換為3通道rgb dog _ img _ pil = dog _ img _ pil . convert( rgb )airplane _ img _ pil = airplane _ img _ pil . convert( rgb ).transform = torch vision . transforms . compose([torch vision . transforms . resize((32,32)),torch vision . transforms . totensor])dog _ img _ tensor = transform(dog _ img _ pil)planet _ img _ tensor = transform(planet _ img _ pil)# print(dog _ img _ tensor . shape)dog _ img _ tensor = torch . shape(dog _ img _ tensor,(-1,3,32,32))planet _ img _ tensor = torch . shape(planet _ img _ tensor,(1,3,32,32))cifar 10 _ model = my modelcifar 10 _ model . load _ state _ dict(中../_ 10 _ train _ model/cifar 10 _ model _ state _ dict _ 18 _ epoch . pth ))cifar10 _ model.eval帶torch.no_grad: output = cifar 10 _ model(dog _ img _ tensor)print(output . arg max(1))output = cifar 10 _ model(airplane _ img _ tensor)print(output . arg max(1))輸出如下:
張量([7]) #預(yù)測(cè)誤差張量([0]) #預(yù)測(cè)正確標(biāo)簽:
模型梯度
了解更多title: pytorch.t (pytorc檔)相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。
上一個(gè):高壓配電柜斷路器控制回路的基本要求
下一個(gè):內(nèi)存品牌及型號(hào)(內(nèi)存品牌大全)

阿里云租用第二個(gè)服務(wù)器
有線電視系統(tǒng)前端設(shè)備與控制臺(tái)的安裝要求是?
今天,茶陪你了嗎?
退耕還林補(bǔ)貼給多少年
win7揚(yáng)聲器音量圖標(biāo)不見了怎么恢復(fù)出廠設(shè)置(win7揚(yáng)聲器音量圖標(biāo)不見了怎么恢復(fù))
了解全自動(dòng)紅外測(cè)油儀的原理
看動(dòng)漫的電腦軟件(電腦上畫動(dòng)漫人物的軟件叫什么)
模擬信號(hào)隔離變送器
瑞典nilfisk出品的工業(yè)吸塵器(讓您的生產(chǎn)線更干凈高效)
數(shù)據(jù)恢復(fù)真的有用嗎,360數(shù)據(jù)恢復(fù)有用嗎
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos