Conv2d, MaxPool2d, Linear, Flatten, Sequential的使用

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Linear, Flatten, Sequential
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
test_data = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=dataset_transform,download = True)
# test_loader = DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=False)
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        # self.conv1 = Conv2d(3, 32, 5, padding=2)
        # self.maxpool1 = MaxPool2d(2)
        # self.conv2 = Conv2d(32, 32, 5, padding=2)
        # self.conv3 = Conv2d(32, 64, 5, padding=2)
        # self.flatten = Flatten()
        # self.linear1 = Linear(1024,64)
        # self.linear2 = Linear(64, 10)
        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
        # x1 = self.conv1(input)   # 3@32×32  ——  32@32×32
        # x2 = self.maxpool1(x1)   #32@32×32  ——  32@16×16
        # x3 = self.conv2(x2)      #32@16×16  ——  32@16×16
        # x4 = self.maxpool1(x3)   #32@16×16  ——  32@8×8
        # x5 = self.conv3(x4)      #  32@8×8  ——  64@8×8
        # x6 = self.maxpool1(x5)   # 64@8×8  ——  64@4×4
        # x7 = self.flatten(x6)       # 64@4×4  ——  64×4×4
        # x8 = self.linear1(x7)     #64×4×4   ——  64
        # output = self.linear2(x8)  # 64   ——  10
image,targe = test_data[1]
image = torch.reshape(image,(-1,3,32,32))
print(image.shape)
model = Model()  
print(model)
output = model(image)
print(output)

网络结构如下所示

Conv2d, MaxPool2d, Linear, Flatten, Sequential的使用

 

上一篇:第六周学习任务


下一篇:百度地图 坑爹之路