1. Fully Connected Neural Network
2. Convolutional Neural Network
- 特征提取
- 卷积
- 池化
- 分类
- 展开
- 全连接
2.1Convolution
卷积:拿N*N大小的kernel去与像素矩阵做内积
- N Input Channels and M Output Channels:
M个kernel:kernel size:n*𝒌𝒆𝒓𝒏𝒆𝒍_𝒔𝒊𝒛𝒆𝒘𝒊𝒅𝒕𝒉 × 𝒌𝒆𝒓𝒏𝒆𝒍_𝒔𝒊𝒛𝒆𝒉𝒆𝒊𝒈𝒉
𝑚 × 𝑛 × 𝑘𝑒𝑟𝑛𝑒𝑙_𝑠𝑖𝑧𝑒𝑤𝑖𝑑𝑡ℎ × 𝑘𝑒𝑟𝑛𝑒𝑙_𝑠𝑖𝑧𝑒ℎ𝑒𝑖𝑔ℎ
2.2 Convolutional Layer
input = torch.randn(batch_size,in_channels,width,height)
conv_layer = torch.nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size)
padding
为了卷积之后得到想到的m*m的矩阵,在input矩阵扩充,但是不影响结果。
padding=1:扩充一圈
stride
每次移动的格数
stride=2:每次移动两格
2.3 Max Pooling Layer
maxpooling_layer=torch.nn.MaxPool2d(kernel_size=2)#默认kernei_size=2
output1=maxpooling_layer(input1)
import torch
in_channels,out_channels=5,10
width,height=100,100
kernel_size=3
batch_size=1
#input = torch.randn(batch_size,in_channels,width,height)#生成0-1正态分布
input=[3,4,6,5,7,
2,4,6,8,2,
1,6,7,8,4,
9,7,4,6,2,
3,7,5,4,1]
input1 = [3,4,6,5,
2,4,6,8,
1,6,7,8,
9,7,4,6,]
input=torch.Tensor(input).view(1,1,5,5)
input1 = torch.Tensor(input1).view(1, 1, 4, 4)
#conv_layer=torch.nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size)
#conv_layer=torch.nn.Conv2d(1,1,kernel_size=3,padding=1,bias=False)#paddings=1(扩充一圈)相当于扩充原来矩阵维数,比如4*4,变成5*5
conv_layer=torch.nn.Conv2d(1,1,kernel_size=3,stride=2,bias=False)#每次移动两格
kernel=torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3)
conv_layer.weight.data=kernel.data
output=conv_layer(input)
#print(input.shape)
#print(output.shape)
#print(conv_layer.weight.shape)
print(output)
maxpooling_layer=torch.nn.MaxPool2d(kernel_size=2)#默认kernei_size=2
output1=maxpooling_layer(input1)
print(output1)
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1=torch.nn.Conv2d(1,10,kernel_size=5)
self.conv2=torch.nn.Conv2d(10,20,kernel_size=5)
self.pooling=torch.nn.MaxPool2d(2)
self.fc=torch.nn.Linear(320,10)
def forward(self,x):
# Flatten data from (n, 1, 28, 28) to (n, 784)
batch_size=x.size(0)
x=F.relu(self.pooling(self.conv1(x)))
x=F.relu(self.pooling(self.conv2(x)))
x=x.view(batch_size,-1)#flatten
x=self.fc(x)
return x
model=Net()
3.How to use GPU
1. Move Model to GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#Define device as the first visible cuda device if we have CUDA available.
model.to(device)
# Convert parameters and buffers of all modules to CUDA Tensor.
2. Move Tensors to GPU
inputs,target=inputs.to(device),target.to(device)
#Send the inputs and targets at every step to the GPU
4. 应用:手写数字
import numpy as np
import torch
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader #For constructing DataLoader
from torchvision import transforms #For constructing DataLoader
from torchvision import datasets #For constructing DataLoader
import torch.nn.functional as F #For using function relu()
batch_size=64
transform=transforms.Compose([transforms.ToTensor(),#Convert the PIL Image to Tensor.
transforms.Normalize((0.1307,),(0.3081,))])#The parameters are mean and std respectively.
train_dataset = datasets.MNIST(root='C:/Users/yuhongxia/PycharmProjects/dataset/mnist',train=True,transform=transform,download=True)
test_dataset = datasets.MNIST(root='C:/Users/yuhongxia/PycharmProjects/dataset/mnist',train=False,transform=transform,download=True)
train_loader = DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)
test_loader = DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=False)
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1=torch.nn.Conv2d(1,10,kernel_size=5)
self.conv2=torch.nn.Conv2d(10,20,kernel_size=5)
self.pooling=torch.nn.MaxPool2d(2)
self.fc=torch.nn.Linear(320,10)
def forward(self,x):
# Flatten data from (n, 1, 28, 28) to (n, 784)
batch_size=x.size(0)
x=F.relu(self.pooling(self.conv1(x)))
x=F.relu(self.pooling(self.conv2(x)))
x=x.view(batch_size,-1)#flatten
x=self.fc(x)
return x
model=Net()
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#Define device as the first visible cuda device if we have CUDA available.
#model.to(device)
# Convert parameters and buffers of all modules to CUDA Tensor.
criterion=torch.nn.CrossEntropyLoss()
optimizer=torch.optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
def train(epoch):
running_loss=0.0
for batch_id,data in enumerate(train_loader,0):
inputs,target=data
#inputs,target=inputs.to(device),target.to(device)
#Send the inputs and targets at every step to the GPU
optimizer.zero_grad()
# forward + backward + update
outputs=model(inputs)
loss=criterion(outputs,target)
loss.backward()
optimizer.step()
running_loss +=loss.item()
if batch_id% 300==299:
print('[%d,%5d] loss: %.3f' % (epoch+1,batch_id,running_loss/300))
running_loss=0.0
accracy = []
def test():
correct=0
total=0
with torch.no_grad():
for data in test_loader:
inputs,target=data
#inputs,target=inputs.to(device),target.to(device)
#Send the inputs and targets at every step to the GPU
outputs=model(inputs)
predicted=torch.argmax(outputs.data,dim=1)
total+=target.size(0)
correct+=(predicted==target).sum().item()
print('Accuracy on test set : %d %% [%d/%d]'%(100*correct/total,correct,total))
accracy.append([100*correct/total])
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
x=np.arange(10)
plt.plot(x, accracy)
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.grid()
plt.show()
np.arange()!!!不是arrange
np.apend([])