国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

PyTorch深度學(xué)習(xí)快速入門教程(絕對(duì)通俗易懂?。。。?/h1>

這篇具有很好參考價(jià)值的文章主要介紹了PyTorch深度學(xué)習(xí)快速入門教程(絕對(duì)通俗易懂?。。。?。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


一、PyTorch環(huán)境的配置及安裝

1.官網(wǎng)下載最新版Anaconda,完成后打開Anaconda Prompt,顯示(base)即安裝成功
2.conda create -n pytorch python=3.6建立一個(gè)命名為pytorch的環(huán)境,且環(huán)境python版本為3.6
3.conda activate pytorch激活并進(jìn)入pytorch這個(gè)環(huán)境;linux:source activate pytorch
4.pip list來查看環(huán)境內(nèi)安裝了哪些包,可以發(fā)現(xiàn)并沒有我們需要的pytorch
5.打開PyTorch官網(wǎng),直接找到最新版pytorch指令conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch(無腦最新版就完事了。。。。老版本調(diào)了半天,最后還出問題了),打開pytorch環(huán)境,輸入指令下載安裝
6.檢驗(yàn)是否安裝成功。輸入python,import torch不報(bào)錯(cuò)即pytorch安裝成功。輸入torch.cuda.is_available(),若返回True即機(jī)器顯卡是可以被pytorch使用的(如失敗,建議去英偉達(dá)官網(wǎng)下載更新驅(qū)動(dòng)程序,并刪除環(huán)境,使用各種最新版重新安裝)。
7.linux服務(wù)器安裝時(shí)出現(xiàn)環(huán)境安裝不到conda/envs下,而在.conda下,進(jìn)行如下操作
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

other:conda info -e (查看所有的虛擬環(huán)境)

刪除環(huán)境:
第一步:首先退出環(huán)境
conda deactivate
第二步:刪除環(huán)境
conda remove -n 需要?jiǎng)h除的環(huán)境名 --all

rm -rf + 文件名 刪除文件夾
df -h查看linux系統(tǒng)各分區(qū)的情況
nohup 命令 > 文件 2>&1 & # 使模型在后臺(tái)訓(xùn)練 exit退出黑窗口
1.> 會(huì)重寫文件,如果文件里面有內(nèi)容會(huì)覆蓋,沒有則創(chuàng)建并寫入。
2.>> 將內(nèi)容追加到文件中,即如果文件里面有內(nèi)容會(huì)把新內(nèi)容追加到文件尾,如果文件不存在,就創(chuàng)建文件
kill -9 PID # 關(guān)閉特定進(jìn)程
tar -xvf #解壓tar包
查看當(dāng)前文件夾的大?。篸u -ah
查看當(dāng)前文件夾下面各個(gè)文件夾的大?。篸u -ah --max-depth=1
anaconda下的pkgs怎么清理:conda clean -a
ps u pid 查詢顯卡誰在使用
sudo chmod -R 777 myResources 修改文件的權(quán)限為所有用戶擁有最高權(quán)限
pip install *** -i https://pypi.tuna.tsinghua.edu.cn/simple 鏡像加速安裝
ps -f -p 26359 可以看到進(jìn)程26359在跑訓(xùn)練
cp -r /TEST/test1 /TEST/test2 復(fù)制文件夾
Defaulting to user installation because normal site-packages is not writeable : python3 -m pip install requests

fuser -v /dev/nvidia* nvidia-smi 無進(jìn)程占用GPU,但GPU顯存卻被占用了

二、Pycharm、jupyter的安裝

1. Pycharm

1.pycharm官網(wǎng)下載安裝
2.新建項(xiàng)目(lean_pytorch),pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
點(diǎn)擊已存在的編譯器,點(diǎn)進(jìn)去尋找剛剛我們安裝好的環(huán)境。pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
導(dǎo)入成功。

2.jupyter

  1. 安裝好anaconda后無需再次安裝。
  2. jupyter默認(rèn)安裝在base環(huán)境中,所以我們需要在pytorch環(huán)境中安裝jupyter.
  3. 進(jìn)入pytorch環(huán)境,輸入conda install nb_conda安裝juypter
  4. 安裝完成后輸入juypter notebook即可打開。
  5. pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
    新建pytorch環(huán)境下的juypter文件。
  6. 輸入import torch,torch.cuda.is_available(),返回TRUE即安裝成功。

三、Python學(xué)習(xí)中的兩大法寶函數(shù)(help、dir)

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
進(jìn)入pycharm的python console,輸入dir(torch),dir(torch.cuda),dir(torch.cuda.is_available()),help(torch.cuda.is_available)。

四、加載數(shù)據(jù)(Dataset)

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

from torch.utils.data import Dataset, DataLoader
import numpy as np
from PIL import Image
import os
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from torchvision.utils import make_grid

writer = SummaryWriter("logs")

class MyData(Dataset):

    def __init__(self, root_dir, image_dir, label_dir, transform):
        self.root_dir = root_dir
        self.image_dir = image_dir
        self.label_dir = label_dir
        self.label_path = os.path.join(self.root_dir, self.label_dir)
        self.image_path = os.path.join(self.root_dir, self.image_dir)
        self.image_list = os.listdir(self.image_path)
        self.label_list = os.listdir(self.label_path)
        self.transform = transform
        # 因?yàn)閘abel 和 Image文件名相同,進(jìn)行一樣的排序,可以保證取出的數(shù)據(jù)和label是一一對(duì)應(yīng)的
        self.image_list.sort()
        self.label_list.sort()

    def __getitem__(self, idx):
        img_name = self.image_list[idx]
        label_name = self.label_list[idx]
        img_item_path = os.path.join(self.root_dir, self.image_dir, img_name)
        label_item_path = os.path.join(self.root_dir, self.label_dir, label_name)
        img = Image.open(img_item_path)

        with open(label_item_path, 'r') as f:
            label = f.readline()

        # img = np.array(img)
        img = self.transform(img)
        sample = {'img': img, 'label': label}
        return sample

    def __len__(self):
        assert len(self.image_list) == len(self.label_list)
        return len(self.image_list)

if __name__ == '__main__':
    transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor()])
    root_dir = "dataset/train"
    image_ants = "ants_image"
    label_ants = "ants_label"
    ants_dataset = MyData(root_dir, image_ants, label_ants, transform)
    image_bees = "bees_image"
    label_bees = "bees_label"
    bees_dataset = MyData(root_dir, image_bees, label_bees, transform)
    train_dataset = ants_dataset + bees_dataset

    # transforms = transforms.Compose([transforms.Resize(256, 256)])
    dataloader = DataLoader(train_dataset, batch_size=1, num_workers=2)

    writer.add_image('error', train_dataset[119]['img'])
    writer.close()
    # for i, j in enumerate(dataloader):
    #     # imgs, labels = j
    #     print(type(j))
    #     print(i, j['img'].shape)
    #     # writer.add_image("train_data_b2", make_grid(j['img']), i)
    #
    # writer.close()

五、TensorBorad的使用

安裝tensorborad:pip install tensorboard
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
更改端口:
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

六、Transformer

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

進(jìn)入structure

1.compose

將幾個(gè)步驟合為一個(gè)

2.toTensor

將PIL和numpy類型的圖片轉(zhuǎn)為Tensor(可用于訓(xùn)練)
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

__call__的使用:pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
ctrl+p提示函數(shù)參數(shù)

3.Normalize

講一個(gè)tensor類型進(jìn)行歸一化
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

4.Resize

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
tips:pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

七、torchvision中數(shù)據(jù)集的使用

torchvision 是PyTorch中專門用來處理圖像的庫。這個(gè)包中有四個(gè)大類。

torchvision.datasets

torchvision.models

torchvision.transforms

torchvision.utils

這里主要介紹前三個(gè)。

1.torchvision.datasets

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

八、dataloader

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
drop_last=true,舍去最后的余數(shù)圖片,如上半張圖片將會(huì)舍去,下半張圖片為FALSE
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

九、nn.module

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

十、卷積操作

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python
pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

十一、卷積層

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)
dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)

    def forward(self, x):
        x = self.conv1(x)
        return x

tudui = Tudui()

writer = SummaryWriter("../logs")

step = 0
for data in dataloader:
    imgs, targets = data
    output = tudui(imgs)
    print(imgs.shape)
    print(output.shape)
    # torch.Size([64, 3, 32, 32])
    writer.add_images("input", imgs, step)
    # torch.Size([64, 6, 30, 30])  -> [xxx, 3, 30, 30]

    output = torch.reshape(output, (-1, 3, 30, 30))
    writer.add_images("output", output, step)

    step = step + 1

十二、池化層

import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)

    def forward(self, input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()

writer = SummaryWriter("../logs_maxpool")
step = 0

for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

十三、非線性激活

input = torch.tensor([[1, -0.5],
                      [-1, 3]])

input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape)

dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.relu1 = ReLU()
        self.sigmoid1 = Sigmoid()

    def forward(self, input):
        output = self.sigmoid1(input)
        return output

tudui = Tudui()

writer = SummaryWriter("../logs_relu")
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, global_step=step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step += 1

writer.close()

十四、線性層

import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.linear1 = Linear(196608, 10)

    def forward(self, input):
        output = self.linear1(input)
        return output

tudui = Tudui()

for data in dataloader:
    imgs, targets = data
    print(imgs.shape)
    output = torch.flatten(imgs)
    print(output.shape)
    output = tudui(output)
    print(output.shape)

十五、Sequential

import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.model1(x)
        return x

tudui = Tudui()
print(tudui)
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)

writer = SummaryWriter("../logs_seq")
writer.add_graph(tudui, input)
writer.close()

十六、損失函數(shù)和反向傳播

1.損失函數(shù)

import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=1)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.model1(x)
        return x


loss = nn.CrossEntropyLoss()
tudui = Tudui()
for data in dataloader:
    imgs, targets = data
    outputs = tudui(imgs)
    result_loss = loss(outputs, targets)
    print("ok")

2.反向傳播及優(yōu)化

import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=1)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.model1(x)
        return x


loss = nn.CrossEntropyLoss()
tudui = Tudui()
optim = torch.optim.SGD(tudui.parameters(), lr=0.01)
scheduler = StepLR(optim, step_size=5, gamma=0.1)
for epoch in range(20):
    running_loss = 0.0
    for data in dataloader:
        imgs, targets = data
        outputs = tudui(imgs)
        result_loss = loss(outputs, targets)
        optim.zero_grad()
        result_loss.backward()
        scheduler.step()
        running_loss = running_loss + result_loss
    print(running_loss)

十七、現(xiàn)有模型的使用及修改

import torchvision

# train_data = torchvision.datasets.ImageNet("../data_image_net", split='train', download=True,
#                                            transform=torchvision.transforms.ToTensor())
from torch import nn

vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)

print(vgg16_true)

train_data = torchvision.datasets.CIFAR10('../data', train=True, transform=torchvision.transforms.ToTensor(),
                                          download=True)

vgg16_true.classifier.add_module('add_linear', nn.Linear(1000, 10))
print(vgg16_true)

print(vgg16_false)
vgg16_false.classifier[6] = nn.Linear(4096, 10)
print(vgg16_false)

十八、網(wǎng)絡(luò)模型的保存和修改

1.保存

import torch
import torchvision
from torch import nn

vgg16 = torchvision.models.vgg16(pretrained=False)
# 保存方式1,模型結(jié)構(gòu)+模型參數(shù)
torch.save(vgg16, "vgg16_method1.pth")

# 保存方式2,模型參數(shù)(官方推薦)
torch.save(vgg16.state_dict(), "vgg16_method2.pth")

# 陷阱
class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=3)

    def forward(self, x):
        x = self.conv1(x)
        return x

tudui = Tudui()
torch.save(tudui, "tudui_method1.pth")

2.讀取

import torch
from model_save import *
# 方式1-》保存方式1,加載模型
import torchvision
from torch import nn

model = torch.load("vgg16_method1.pth")
# print(model)

# 方式2,加載模型
vgg16 = torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load("vgg16_method2.pth"))
# model = torch.load("vgg16_method2.pth")
# print(vgg16)

# 陷阱1
# class Tudui(nn.Module):
#     def __init__(self):
#         super(Tudui, self).__init__()
#         self.conv1 = nn.Conv2d(3, 64, kernel_size=3)
#
#     def forward(self, x):
#         x = self.conv1(x)
#         return x

model = torch.load('tudui_method1.pth')
print(model)

只用方式2!?。。?/p>

十九、完整的模型訓(xùn)練套路

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

import torchvision
from my_model import  *
from torch.utils.tensorboard import SummaryWriter

#準(zhǔn)備數(shù)據(jù)集
from torch import nn
from torch.utils.data import DataLoader

train_data = torchvision.datasets.CIFAR10(root="../data",train=True,transform=torchvision.transforms.ToTensor(),download=True)
test_data = torchvision.datasets.CIFAR10(root="../data",train=False,transform=torchvision.transforms.ToTensor(),download=True)

# length 長(zhǎng)度
train_data_size = len(train_data)
test_data_size = len(test_data)
# 如果train_data_size=10,訓(xùn)練數(shù)據(jù)集的長(zhǎng)度為:10
print("訓(xùn)練數(shù)據(jù)集的長(zhǎng)度為:{}".format(train_data_size))
print("測(cè)試數(shù)據(jù)集的長(zhǎng)度為:{}".format(test_data_size))


#利用 DataLoader 來加載數(shù)據(jù)集
train_dataloader = DataLoader(train_data,batch_size=64)
test_dataloader = DataLoader(test_data,batch_size=64)

#創(chuàng)建網(wǎng)絡(luò)模型
tudui = Tudui()

#損失函數(shù)
loss_fn = nn.CrossEntropyLoss()

#優(yōu)化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(),lr=learning_rate)

#訓(xùn)練網(wǎng)絡(luò)的一些參數(shù)
#記錄訓(xùn)練的次數(shù)
total_train_step = 0
#記錄測(cè)試的次數(shù)
total_test_step = 0
#訓(xùn)練的輪數(shù)
epoch = 10


#添加tensorboard
writer = SummaryWriter("../logs_train")


for i in range(epoch):
    print("-----------第{}輪訓(xùn)練開始-----------".format(i+1))


    #訓(xùn)練步驟開始
    tudui.train()
    for data in train_dataloader:
        imgs,targets = data
        outputs = tudui(imgs)
        loss = loss_fn(outputs,targets)
        #優(yōu)化器優(yōu)化模型
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        total_train_step += 1
        if total_train_step % 100 == 0:
            print("訓(xùn)練次數(shù):{},Loss:{}".format(total_train_step,loss.item()))
            writer.add_scalar("train_loss",loss.item(),total_train_step)


    # 測(cè)試步驟開始
    tudui.eval()
    total_test_loss = 0
    total_accuracy = 0
    with torch.no_grad():
        for data in test_dataloader:
            imgs,targets = data
            outputs = tudui(imgs)
            loss = loss_fn(outputs,targets)
            total_test_loss += loss
            accuracy = (outputs.argmax(1)==targets).sum()
            total_accuracy += accuracy
    print("整體集上的Loss:{}".format(total_test_loss))
    print("整體數(shù)據(jù)集上的正確率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss",total_test_loss,total_test_step)
    writer.add_scalar("test_accuracy",total_accuracy/test_data_size,total_test_step)
    total_test_step += 1

    torch.save(tudui,"tudui_{}.pth".format(i))
    #torch.save(tudui.state_dict(),"tudui_{}".format(i))
    print("模型已保存")

writer.close()

二十、利用GPU訓(xùn)練

pytorch入門教程,深度學(xué)習(xí),pytorch,深度學(xué)習(xí),python

import torchvision
from torch.utils.tensorboard import SummaryWriter
import torch
import time

#準(zhǔn)備數(shù)據(jù)集
from torch import nn
from torch.utils.data import DataLoader


device = torch.device("cuda")
train_data = torchvision.datasets.CIFAR10(root="../data",train=True,transform=torchvision.transforms.ToTensor(),download=True)
test_data = torchvision.datasets.CIFAR10(root="../data",train=False,transform=torchvision.transforms.ToTensor(),download=True)

# length 長(zhǎng)度
train_data_size = len(train_data)
test_data_size = len(test_data)
# 如果train_data_size=10,訓(xùn)練數(shù)據(jù)集的長(zhǎng)度為:10
print("訓(xùn)練數(shù)據(jù)集的長(zhǎng)度為:{}".format(train_data_size))
print("測(cè)試數(shù)據(jù)集的長(zhǎng)度為:{}".format(test_data_size))


#利用 DataLoader 來加載數(shù)據(jù)集
train_dataloader = DataLoader(train_data,batch_size=64)
test_dataloader = DataLoader(test_data,batch_size=64)

#創(chuàng)建網(wǎng)絡(luò)模型
class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3,32,5,1,2),
            nn.MaxPool2d(2),
            nn.Conv2d(32,32,5,1,2),
            nn.MaxPool2d(2),
            nn.Conv2d(32,64,5,1,2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4,64),
            nn.Linear(64,10)
        )
    def forward(self,x):
        x=self.model(x)
        return x
tudui = Tudui()
tudui=tudui.to(device)

#損失函數(shù)
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)

#優(yōu)化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(),lr=learning_rate)

#訓(xùn)練網(wǎng)絡(luò)的一些參數(shù)
#記錄訓(xùn)練的次數(shù)
total_train_step = 0
#記錄測(cè)試的次數(shù)
total_test_step = 0
#訓(xùn)練的輪數(shù)
epoch = 10


#添加tensorboard
writer = SummaryWriter("../logs_train")

start_time=time.time()
for i in range(epoch):
    print("-----------第{}輪訓(xùn)練開始-----------".format(i+1))


    #訓(xùn)練步驟開始
    tudui.train()
    for data in train_dataloader:
        imgs,targets = data
        imgs = imgs.to(device)
        targets = targets.to(device)
        outputs = tudui(imgs)
        loss = loss_fn(outputs,targets)
        #優(yōu)化器優(yōu)化模型
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        total_train_step += 1
        if total_train_step % 100 == 0:
            end_time = time.time()
            print(end_time-start_time)
            print("訓(xùn)練次數(shù):{},Loss:{}".format(total_train_step,loss.item()))
            writer.add_scalar("train_loss",loss.item(),total_train_step)


    # 測(cè)試步驟開始
    tudui.eval()
    total_test_loss = 0
    total_accuracy = 0
    with torch.no_grad():
        for data in test_dataloader:
            imgs,targets = data
            imgs = imgs.to(device)
            targets = targets.to(device)
            outputs = tudui(imgs)
            loss = loss_fn(outputs,targets)
            total_test_loss += loss
            accuracy = (outputs.argmax(1)==targets).sum()
            total_accuracy += accuracy
    print("整體集上的Loss:{}".format(total_test_loss))
    print("整體數(shù)據(jù)集上的正確率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss",total_test_loss,total_test_step)
    writer.add_scalar("test_accuracy",total_accuracy/test_data_size,total_test_step)
    total_test_step += 1

    torch.save(tudui,"tudui_{}.pth".format(i))
    #torch.save(tudui.state_dict(),"tudui_{}".format(i))
    print("模型已保存")

writer.close()




二十一、完整的模型驗(yàn)證套路

# -*- coding: utf-8 -*-
# 作者:小土堆
# 公眾號(hào):土堆碎念
import torch
import torchvision
from PIL import Image
from torch import nn

image_path = "../imgs/airplane.png"
image = Image.open(image_path)
print(image)
image = image.convert('RGB')    # 因?yàn)閜ng格式是四通道,除了RGB三通道外,還有一個(gè)透明度通道,
# 調(diào)用convert保留其顏色通道。當(dāng)然,如果圖片本來就是三個(gè)顏色通道,經(jīng)此操作,不變。加上這一步可以適應(yīng)png jpg各種格式的圖片
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),
                                            torchvision.transforms.ToTensor()])

image = transform(image)
print(image.shape)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.model(x)
        return x

model = torch.load("tudui_29_gpu.pth", map_location=torch.device('cpu'))
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():
    output = model(image)
print(output)

print(output.argmax(1))


總結(jié)

。文章來源地址http://www.zghlxwxcb.cn/news/detail-570732.html

到了這里,關(guān)于PyTorch深度學(xué)習(xí)快速入門教程(絕對(duì)通俗易懂?。。。┑奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • pytorch中分布式訓(xùn)練DDP教程(新手快速入門!)

    PyTorch是深度學(xué)習(xí)領(lǐng)域廣泛使用的開源深度學(xué)習(xí)框架之一。隨著深度學(xué)習(xí)模型的不斷增大和數(shù)據(jù)集的不斷增長(zhǎng),單機(jī)訓(xùn)練往往不能滿足我們的需求。為了加速訓(xùn)練過程,我們可以使用分布式訓(xùn)練技術(shù)。在PyTorch中,分布式數(shù)據(jù)并行(Distributed Data Parallel,簡(jiǎn)稱DDP)是一種常見的分

    2024年02月16日
    瀏覽(23)
  • Pytorch DistributedDataParallel(DDP)教程二:快速入門實(shí)踐篇

    一、簡(jiǎn)要回顧DDP 在上一篇文章中,簡(jiǎn)單介紹了Pytorch分布式訓(xùn)練的一些基礎(chǔ)原理和基本概念。簡(jiǎn)要回顧如下: 1,DDP采用Ring-All-Reduce架構(gòu),其核心思想為:所有的GPU設(shè)備安排在一個(gè)邏輯環(huán)中,每個(gè)GPU應(yīng)該有一個(gè)左鄰和一個(gè)右鄰,設(shè)備從它的左鄰居接收數(shù)據(jù),并將數(shù)據(jù)匯總后發(fā)送

    2024年04月15日
    瀏覽(20)
  • Docker 快速上手學(xué)習(xí)入門教程

    Docker 快速上手學(xué)習(xí)入門教程

    目錄 1、docker 的基礎(chǔ)概念 2、怎樣打包和運(yùn)行一個(gè)應(yīng)用程序? 3、如何對(duì) docker 中的應(yīng)用程序進(jìn)行修改? 4、如何對(duì)創(chuàng)建的鏡像進(jìn)行共享? 5、如何使用 volumes 名稱對(duì)容器中的數(shù)據(jù)進(jìn)行存儲(chǔ)?// 數(shù)據(jù)掛載 6、另一種掛載方式:目錄掛載 7、實(shí)現(xiàn)容器之間的相互通信 8、使用?Docker

    2024年02月09日
    瀏覽(22)
  • 圖像分類保姆級(jí)教程-深度學(xué)習(xí)入門教程(附代碼)

    圖像分類保姆級(jí)教程-深度學(xué)習(xí)入門教程(附代碼)

    圖像分類是計(jì)算機(jī)視覺領(lǐng)域中的一個(gè)重要任務(wù)。它的目的是將輸入的圖像歸類到預(yù)定義的類別中。這個(gè)任務(wù)在過去被認(rèn)為是非常具有挑戰(zhàn)性的,因?yàn)閳D像的特征非常復(fù)雜,而且存在很多種不同的變化方式,例如光照、角度、遮擋等等。 然而,隨著深度學(xué)習(xí)的發(fā)展,圖像分類問

    2024年01月22日
    瀏覽(27)
  • 【深度學(xué)習(xí)】pytorch——快速入門

    【深度學(xué)習(xí)】pytorch——快速入門

    筆記為自我總結(jié)整理的學(xué)習(xí)筆記,若有錯(cuò)誤歡迎指出喲~ PyTorch是一個(gè)開源的機(jī)器學(xué)習(xí)框架,它提供了豐富的工具和庫,用于構(gòu)建和訓(xùn)練深度學(xué)習(xí)模型。下面是一些關(guān)于PyTorch的基本信息: 張量(Tensor)操作 :PyTorch中的核心對(duì)象是張量,它是一個(gè)多維數(shù)組。PyTorch提供了廣泛的

    2024年02月06日
    瀏覽(53)
  • 【Redis學(xué)習(xí)筆記01】快速入門(含安裝教程)

    【Redis學(xué)習(xí)筆記01】快速入門(含安裝教程)

    先來看門見山的給出 Redis 的概念: Redis:是一種基于內(nèi)存的高性能K-V鍵值型NoSQL數(shù)據(jù)庫 Redis官網(wǎng):https://redis.io/ 1.1 初識(shí)NoSQL 想必大家都對(duì)關(guān)系型數(shù)據(jù)庫更為熟悉!如MySQL、Oracle、SQL Server都是比較常見的關(guān)系型數(shù)據(jù)庫,所謂關(guān)系型數(shù)據(jù)庫主要以二維表作為數(shù)據(jù)結(jié)構(gòu)進(jìn)行存儲(chǔ),但

    2024年01月22日
    瀏覽(56)
  • 全網(wǎng)網(wǎng)絡(luò)安全入門教程(非常詳細(xì))從零基礎(chǔ)入門到精通,看完這一篇絕對(duì)夠了

    全網(wǎng)網(wǎng)絡(luò)安全入門教程(非常詳細(xì))從零基礎(chǔ)入門到精通,看完這一篇絕對(duì)夠了

    由于我之前寫了不少網(wǎng)絡(luò)安全技術(shù)相關(guān)的故事文章,不少讀者朋友知道我是從事網(wǎng)絡(luò)安全相關(guān)的工作,于是經(jīng)常有人在微信里問我: 我剛?cè)腴T網(wǎng)絡(luò)安全,該怎么學(xué)?要學(xué)哪些東西?有哪些方向?怎么選? 不同于Java、C/C++等后端開發(fā)崗位有非常明晰的學(xué)習(xí)路線,網(wǎng)路安全更多

    2024年02月08日
    瀏覽(23)
  • Google Earth Engine(GEE)深度學(xué)習(xí)入門教程- GEE導(dǎo)出篇

    Google Earth Engine(GEE)深度學(xué)習(xí)入門教程- GEE導(dǎo)出篇

    官方教程:TFRecord 和地球引擎 在GEE的JS Code Editor中,我們按照我們的需要去處理對(duì)應(yīng)的遙感影像,得到處理后Image影像。為了導(dǎo)出后讀取數(shù)據(jù),在導(dǎo)出前 一定清楚每個(gè)波段的名稱(不然沒法讀?。?。深度學(xué)習(xí)數(shù)據(jù)集所需數(shù)據(jù)以為patch為單位,所以需要對(duì)整個(gè)遙感影像進(jìn)行裁剪

    2024年02月03日
    瀏覽(59)
  • 【PyTorch教程】pytorch入門系列 ——土堆教程的目錄及索引

    一、幾句題外話 深度學(xué)習(xí)上手已經(jīng)很長(zhǎng)時(shí)間了,還記得最初的入門是跟著 B站up小土堆 的一步步學(xué)起來的,從起初的環(huán)境配置,到現(xiàn)在調(diào)整整個(gè)模型的進(jìn)階,非常感謝土堆的貢獻(xiàn)。 寫這個(gè)博客的初衷是為了自己 看著方便 ,由于多臺(tái)電腦多個(gè)環(huán)境下查看這些內(nèi)容很麻煩,所以

    2024年03月17日
    瀏覽(33)
  • 學(xué)習(xí)SpringBoot入門知識(shí),附帶教程源碼分享,快速掌握開發(fā)技巧-【imooc-java2021】體系課-Java工程師 2022版

    學(xué)習(xí)SpringBoot入門知識(shí),附帶教程源碼分享,快速掌握開發(fā)技巧-【imooc-java2021】體系課-Java工程師 2022版

    以下截圖自 Spring Boot 官方文檔: 翻譯整理一下,內(nèi)容如下: Spring Boot 是基于 Spring 框架基礎(chǔ)上推出的一個(gè)全新的框架, 旨在讓開發(fā)者可以輕松地創(chuàng)建一個(gè)可獨(dú)立運(yùn)行的,生產(chǎn)級(jí)別的應(yīng)用程序。 基于 Spring Boot 內(nèi)部的自動(dòng)化配置功能,開發(fā)者可以在\\\"零\\\"配置, 或者只需要添加很少

    2024年02月06日
    瀏覽(116)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包