文章目录
16. 神经网络的基本骨架
forward:
import torch
from torch import nn
class Tudui(nn.Module):
def __init__(self):
super().__init__()
def forward(self,input):
output=input+1
return output
#创建Tudui的实例对象
tudui=Tudui()
#创建一个输入张量x
x=torch.tensor(1.0)
#将输入张量传递给tudui模型的foward()
output=tudui(x)
print(output)
输入: x
卷积
非线性
卷积
非线性
输出
17.卷积操作
import torch
import torch.nn.functional as F
input=torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]])
kernel=torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
input=torch.reshape(input,(1,1,5,5))
kernel=torch.reshape(kernel,(1,1,3,3))
print(input.shape)
print(kernel.shape)
#卷积操作
#stride:移动步长
output=F.conv2d(input,kernel,stride=1)
print(output)
output2=F.conv2d(input,kernel,stride=2)
print(output2)
#padding:对输入张量四周进行填充
output3=F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
18.卷积层
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("data1",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("logs1")
step=0
for data in dataloader:
imgs,targets=data
output=tudui(imgs)
print(imgs.shape)
print(output.shape)
writer.add_images("input",imgs,step)
output=torch.reshape(output,(-1,3,30,30))
writer.add_images("output",output,step)
step=step+1