paddlepaddle第六章手写数字训练测试结合,配有损失和正确率曲线图

#获取训练数据
train_set = paddle.dataset.mnist.train()
train_reader = paddle.batch(train_set,batch_size=16)
#获取测试数据
test_set = paddle.dataset.mnist.test()
test_reader = paddle.batch(test_set,batch_size=32)

# 定义飞桨动态图工作环境
with fluid.dygraph.guard():
    # 实例化模型
    # 以下三个模型任选其一
    # Softmax分类器
    # model = SoftmaxRegression('mnist')
    # 定义多层感知器分类器
    # model = MultilayerPerceptron('mnist')
    # 卷积神经网络分类器
    model = ConvolutionalNeuralNetwork('mnist')
    
    # 开启模型训练模式
    model.train()
    # 使用Adam优化器
    # 学习率为0.001
    opt = fluid.optimizer.Adam(learning_rate=0.001, parameter_list=model.parameters())
    # 迭代次数设为5
    EPOCH_NUM = 10

test_loss = []
test_acc = []
idx = []
with fluid.dygraph.guard():
    # 定义外层循环
    for pass_num in range(EPOCH_NUM):
        # 定义内层循环
        for batch_id,data in enumerate(train_reader()):
            # 调整数据shape使之适合模型
            images = np.array([x[0].reshape(1, 28, 28) for x in data],np.float32)
            labels = np.array([x[1] for x in data]).astype('int64').reshape(-1,1)
            
            # 将numpy数据转为飞桨动态图variable形式
            image = fluid.dygraph.to_variable(images)
            label = fluid.dygraph.to_variable(labels)
            
            # 前向计算
            predict = model(image)

            # 计算损失
            loss = fluid.layers.cross_entropy(predict,label)
            avg_loss = fluid.layers.mean(loss)
            # 计算精度
            acc = fluid.layers.accuracy(predict,label)
            
            if batch_id % 500 == 0:
                print("pass:{},batch_id:{},train_loss:{},train_acc:{}".
                      format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
            
            # 反向传播
            avg_loss.backward()
            # 最小化loss,更新参数
            opt.minimize(avg_loss)
            # 清除梯度
            model.clear_gradients()
            
        # 保存模型文件到指定路径
        fluid.save_dygraph(model.state_dict(), 'mnist')
        with fluid.dygraph.guard():
                    # 读取模型
                    # 参数为保存模型参数的文件地址
                    model_dict, _ = fluid.load_dygraph('mnist')
                    # 加载模型参数
                    model.load_dict(model_dict)
                    #评估模式
                    model.eval()
                    
                    avg_cost = 0.0
                    avg_acc= 0.0
                    num = 0
                    for batch_id,data in enumerate(test_reader()):
                        num = num + 1
                        # 调整数据shape使之适合模型
                        images = np.array([x[0].reshape(1, 28, 28) for x in data],np.float32)
                        labels = np.array([x[1] for x in data]).astype('int64').reshape(-1,1)
                        
                        # 将numpy数据转为飞桨动态图variable形式
                        image = fluid.dygraph.to_variable(images)
                        label = fluid.dygraph.to_variable(labels)
                        
                        # 前向计算
                        predict = model(image)

                        # 计算损失
                        loss = fluid.layers.cross_entropy(predict,label)
                        avg_loss = fluid.layers.mean(loss)
                        avg_cost = avg_cost + avg_loss 
                        # 计算精度
                        acc = fluid.layers.accuracy(predict,label)
                        avg_acc = avg_acc + acc
                        if batch_id % 50 == 0:
                            print("pass:{},batch_id:{},test_loss:{},test_acc:{}".
                                format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
                    
                    idx.append(pass_num)
                    test_loss.append(float(avg_cost.numpy())/num)
                    test_acc.append(float(avg_acc.numpy())/num)
                
draw_line(test_loss,test_acc,idx)
print(test_loss)
print(test_acc)
from IPython import display
import matplotlib.pyplot as plt
def draw_line(costs,acc, idx):
    """
    动态绘制训练中costs的曲线
    :param costs: 记录了训练过程的cost变化的list
    """
    fig, axis_loss = plt.subplots()
    axis_acc = axis_loss.twinx()

    line_loss = axis_loss.plot(idx, costs, c ="g", label="loss")
 
    line_acc = axis_acc.plot(idx, acc, c="r", label="acc")

    plt.title("Learning rate = 0.001" )

    axis_loss.set_xlabel('iterations')
    axis_loss.set_ylabel('loss',color='g')
    axis_loss.tick_params(axis='y', colors='g') 
    axis_acc.set_ylabel('acc', color='r') 
    axis_acc.tick_params(axis='y', colors='r')
    #plt.legend(loc="best")

    plt.xlabel('iterations')
    #plt.pause(0.05)
    #display.clear_output(wait=True)

aa=[0.2,0.1,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.9,0.8,0.10,0.12]
bb=[1,2,3,4,5,6,7,8,9,10,11,12,13]
idx = bb

draw_line(aa,bb,idx)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值