对于神经网络中的内容
self.conv1 如果将输出维度调整为16,那么不能reshape成3维度,但是可以reshape成8维度,这部分暂时不能理解,如果可以解决,那么就可以随意调整神经网络的过程。
最后还是将卷积的输出维度调整到了3,因为维度为3才能将数据集在tensorboard中展示
import torch
import torchvision
from torch.nn import Conv2d
from torch.nn import MaxPool2d
from torch import nn
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=4)
for data in dataloader:
imgs, targets = data
print(imgs.shape)
# 自己编写一个神经网络
class DSC(nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=3, kernel_size=3, stride=1, padding=2) # 卷积层
self.conv2 = Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1)
self.conv3 = Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=2, padding=0)
self.pool1 = MaxPool2d(kernel_size=5, ceil_mode=True) # 池化层 上取整
self.pool2 = MaxPool2d(kernel_size=3, ceil_mode=True)
self.relu = nn.ReLU()
def forward(self,x):
x = self.relu(self.conv1(x))
# x = self.pool1(x)
# for i in range(2):
# x = self.relu(self.conv2(x))
# x = self.pool2(x)
# for i in range(2):
# x = self.relu(self.conv3(x))
return x
dsc = DSC()
writer = SummaryWriter("test_logs")
for data in dataloader:
imgs, targets = data
print(imgs.shape)
step = 0
for data in dataloader:
imgs, targets = data
output = dsc(imgs)
print(output.shape)
writer.add_images("imgs", imgs, step)
# output = torch.reshape(output, (-1, 8, 34, 34))
# output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images("output", output, step)
step = step + 1