百度架构师手把手带你零基础实践深度学习——21日学习打卡(第一周直播视频、大作业、总结)
首先声明,不详细讲解代码部分,主要是针对课程的理解及对作业的分析。(要是有代码相关问题可以私信)
课程链接:百度架构师手把手带你零基础实践深度学习
上一篇文章:飞浆PaddlePaddle-百度架构师手把手带你零基础实践深度学习——21日学习打卡(第一周第五日)
课程专栏:深度学习课程(可以收藏哦)
第一次课程直播哔哩哔哩链接(我录的屏):哔哩哔哩直播
第一次课程直播视频下载(文件有点大):提取码:1234
第一周的学习到这里就结束了,毕然老师讲解的课程总是让人 忍不住学下去,第一周也属于打基础的一周,讲了飞桨的基础用法(我觉得直接参照API文档),通过手写体识别的这个案例,带我们了解了神经网络模型的构建,模型的保存和加载,模型的数据处理、网络结构、损失函数、优化算法、资源配置和恢复训练。总的来说,第一周还比较简单,算是上手练习了,通过这一周的预习,期待下周的学习!大家一起加油!(对这一周的课程有问题的,可以看一下我写的这一周的学习笔记,上面有作业的讲解,对课程的理解,源代码什么的我就写了主要的,如果想再看一遍课程的话文章也有链接,有问题随时可以转到课程页面。链接本文开头课程专栏里面。)。
最后把第一周时间作业给大家写出来,供大家参考:
import os
import random
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear
import numpy as np
from PIL import Image
train_data=paddle.dataset.mnist.train()
train_data=paddle.reader.shuffle(train_data,100)
train_data=paddle.batch(train_data,100)
# 定义模型结构
class MNIST(fluid.dygraph.Layer):
def __init__(self):
super(MNIST, self).__init__()
# 定义一个卷积层,使用relu激活函数
self.conv1 = Conv2D(num_channels=1, num_filters=20, filter_size=5, stride=1, padding=2, act='relu')
# 定义一个池化层,池化核为2,步长为2,使用最大池化方式
self.pool1 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
# 定义一个卷积层,使用relu激活函数
self.conv2 = Conv2D(num_channels=20, num_filters=20, filter_size=5, stride=1, padding=2, act='relu')
# 定义一个池化层,池化核为2,步长为2,使用最大池化方式
self.pool2 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
# 定义一个全连接层,输出节点数为10
self.fc = Linear(input_dim=980, output_dim=10, act='softmax')
# 定义网络的前向计算过程
def forward(self, inputs, label):
x = self.conv1(inputs)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
x = fluid.layers.reshape(x, [x.shape[0], 980])
x = self.fc(x)
if label is not None:
acc = fluid.layers.accuracy(input=x, label=label)
return x, acc
else:
return x
use_gpu = True
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
from visualdl import LogWriter
with fluid.dygraph.guard(place):
model = MNIST()
model.train()
EPOCH_NUM = 5
BATCH_SIZE = 100
# 定义学习率,并加载优化器参数到模型中
total_steps = (int(60000//BATCH_SIZE) + 1) * EPOCH_NUM
lr = fluid.dygraph.PolynomialDecay(0.01, total_steps, 0.001)
# 使用Adam优化器
optimizer = fluid.optimizer.AdamOptimizer(learning_rate=lr, parameter_list=model.parameters())
for epoch_id in range(EPOCH_NUM):
for batch_id, data in enumerate(train_data()):
#准备数据,变得更加简洁
img_data = np.array([x[0] for x in data]).astype('float32').reshape(-1,1,28,28)
# 获得图像标签数据,并转为float32类型的数组
label_data = np.array([x[1] for x in data]).astype('int64').reshape(-1, 1)
image = fluid.dygraph.to_variable(img_data)
label = fluid.dygraph.to_variable(label_data)
#前向计算的过程,同时拿到模型输出值和分类准确率
predict, acc = model(image, label)
avg_acc = fluid.layers.mean(acc)
#计算损失,取一个批次样本损失的平均值
loss = fluid.layers.cross_entropy(predict, label)
avg_loss = fluid.layers.mean(loss)
#每训练了200批次的数据,打印下当前Loss的情况
if batch_id % 200 == 0:
print("epoch: {}, batch: {}, loss is: {}, acc is {}".format(epoch_id, batch_id, avg_loss.numpy(),avg_acc.numpy()))
#后向传播,更新参数的过程
avg_loss.backward()
optimizer.minimize(avg_loss)
model.clear_gradients()
# 保存模型参数和优化器的参数
fluid.save_dygraph(model.state_dict(), './checkpoint/mnist_epoch{}'.format(epoch_id))
fluid.save_dygraph(optimizer.state_dict(), './checkpoint/mnist_epoch{}'.format(epoch_id))
test_data=paddle.dataset.mnist.test()
#乱序、缓冲区
test_data=paddle.reader.shuffle(test_data,100)
#抽取100张
test_data=paddle.fluid.io.firstn(test_data, 100)
test_data=paddle.batch(test_data,100)
with fluid.dygraph.guard():
print('********************随机抽取原始mnist测试集100张图片进行测试 ********************')
#加载模型参数
model = MNIST()
model_state_dict, _ = fluid.load_dygraph('checkpoint/mnist_epoch4.pdopt')
model.load_dict(model_state_dict)
model.eval()
acc_set = []
avg_loss_set = []
for batch_id, data in enumerate(test_data()):
x_data = np.array([x[0] for x in data]).astype('float32').reshape(-1,1,28,28)
# 获得图像标签数据,并转为float32类型的数组
y_data = np.array([x[1] for x in data]).astype('int64').reshape(-1, 1)
img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data)
prediction, acc = model(img, label)
loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = fluid.layers.mean(loss)
acc_set.append(float(acc.numpy()))
avg_loss_set.append(float(avg_loss.numpy()))
#计算多个batch的平均损失和准确率
acc_val_mean = np.array(acc_set).mean()
avg_loss_val_mean = np.array(avg_loss_set).mean()
print('测试结果:loss={}, acc={}'.format(avg_loss_val_mean, acc_val_mean))
看看我这个模型预测的结果:
epoch: 0, batch: 0, loss is: [4.7954593], acc is [0.06]
epoch: 0, batch: 200, loss is: [0.25242123], acc is [0.94]
epoch: 0, batch: 400, loss is: [0.09221936], acc is [0.99]
epoch: 1, batch: 0, loss is: [0.09070466], acc is [0.97]
epoch: 1, batch: 200, loss is: [0.15324901], acc is [0.98]
epoch: 1, batch: 400, loss is: [0.09217171], acc is [0.97]
epoch: 2, batch: 0, loss is: [0.08392312], acc is [0.99]
epoch: 2, batch: 200, loss is: [0.13600057], acc is [0.98]
epoch: 2, batch: 400, loss is: [0.03001227], acc is [1.]
epoch: 3, batch: 0, loss is: [0.07310453], acc is [0.97]
epoch: 3, batch: 200, loss is: [0.10491574], acc is [0.98]
epoch: 3, batch: 400, loss is: [0.02930602], acc is [0.99]
epoch: 4, batch: 0, loss is: [0.05660088], acc is [0.99]
epoch: 4, batch: 200, loss is: [0.0648721], acc is [0.99]
epoch: 4, batch: 400, loss is: [0.0180967], acc is [0.99]
********************随机抽取原始mnist测试集100张图片进行测试 ********************
测试结果:loss=0.016441160812973976, acc=1.0