第一部分代码
import torch
import torch.nn as nn
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]
inputs = torch.Tensor(input).view(1, 1, 5, 5) # Batch Channel W H
conv_layer = nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False)
kernel = torch.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9]).view(1, 1, 3, 3) # 卷积核 O I W H
conv_layer.weight.data = kernel.data
output = conv_layer(inputs)
print(output)
第二部分代码
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
import torch.nn.functional as F # 激活函数relu
import torch.optim as optim
# # step1 准备数据集
batch_size = 32
transform = transforms.Compose([ # 将下列的内容构成pipline
transforms.ToTensor(), # 把原始数据转为张量 PIL图像0-255转为图像张量0-1
transforms.Normalize((0.1307, ), (0.3081, )) # 归一化 做normalization, 前为均值,后为标准差 数据标准化
])
train_dataset = datasets.MNIST(root='../dataset/mnist', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='../dataset/mnist', train=False, download=True, transform=transform)
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)
# # step2 设计模型
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.pool = nn.MaxPool2d(2)
self.fc = nn.Linear(320, 10)
def forward(self, x):
batch_size = x.size(0) # 1*28*28 样本数量
x = self.pool(F.relu(self.conv1(x))) # 10*24*24 -> 10*12*12
x = self.pool(F.relu(self.conv2(x))) # 20*8*8 -> 20*4*4=320
x = x.view(batch_size, -1) # 变成全连接网络想要的输入 batch*320
x = self.fc(x) # 全连接(用交叉熵损失,所以最后一层不用激活函数)
return x
model = Model()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # 使用GPU
model.to(device) # 转为CUDA tensor
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) # 冲量
# # step3 训练和测试
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad() # 优化器清零
outputs = model(inputs) # 前馈
loss = criterion(outputs, target)
loss.backward() # 反馈
optimizer.step() # 更新
running_loss += loss.item()
if batch_idx % 300 == 299: # 每300次输出一次loss
print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad(): # 测试时候不需要梯度
for data in test_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images) # 输出是一个矩阵 10列
_, predicted = torch.max(outputs.data, dim=1) # 取出每一行的最大值,对应分类,返回最大值和最大值的下标
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on test set: %d %%' % (100 * correct / total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
if epoch % 10 == 9:
test()
结果图
作业
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
import torch.nn.functional as F # 激活函数relu
import torch.optim as optim
# # step1 准备数据集
batch_size = 64
transform = transforms.Compose([ # 将下列的内容构成pipline
transforms.ToTensor(), # 把原始数据转为张量 PIL图像0-255转为图像张量0-1
transforms.Normalize((0.1307, ), (0.3081, )) # 归一化 做normalization, 前为均值,后为标准差 数据标准化
])
train_dataset = datasets.MNIST(root='../dataset/mnist', train=True, download=False, transform=transform)
test_dataset = datasets.MNIST(root='../dataset/mnist', train=False, download=False, transform=transform)
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)
# # step2 设计模型
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 8, kernel_size=3, stride=1)
self.conv2 = nn.Conv2d(8, 16, kernel_size=3, stride=1)
self.conv3 = nn.Conv2d(16, 32, kernel_size=3, stride=1)
self.pool = nn.MaxPool2d(2)
self.fc1 = nn.Linear(32, 20)
self.fc2 = nn.Linear(20, 16)
self.fc3 = nn.Linear(16, 10)
def forward(self, x):
batch_size = x.size(0) # 1*28*28 样本数量
x = self.pool(F.relu(self.conv1(x))) # 8*13*13
x = self.pool(F.relu(self.conv2(x))) # 16*6*6
x = self.pool(F.relu(self.conv3(x))) # 32*2*2=128
x = x.view(batch_size, -1) # 变成全连接网络想要的输入 batch*320
x = self.fc1(x) # 全连接(用交叉熵损失,所以最后一层不用激活函数)
x = self.fc2(x)
x = self.fc3(x)
return x
model = Model()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # 使用GPU
model.to(device) # 转为CUDA tensor
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) # 冲量
# # step3 训练和测试
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad() # 优化器清零
outputs = model(inputs) # 前馈
loss = criterion(outputs, target)
loss.backward() # 反馈
optimizer.step() # 更新
running_loss += loss.item()
if batch_idx % 300 == 299: # 每300次输出一次loss
print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad(): # 测试时候不需要梯度
for data in test_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images) # 输出是一个矩阵 10列
_, predicted = torch.max(outputs.data, dim=1) # 取出每一行的最大值,对应分类,返回最大值和最大值的下标
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on test set: %d %%' % (100 * correct / total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
if epoch % 10 == 9:
test()