赞
踩
- import paddle
- from paddle.nn import Linear
- import paddle.nn.functional as F
- import os
- import numpy as np
- import matplotlib.pyplot as plt
-
- train_dataset = paddle.vision.datasets.MNIST(mode='train')
-
- train_data0 = np.array(train_dataset[0][0])
- train_label_0 = np.array(train_dataset[0][1])
-
- # 显示第一batch的第一个图像
- import matplotlib.pyplot as plt
- plt.figure("Image") # 图像窗口名称
- plt.figure(figsize=(2,2))
- plt.imshow(train_data0, cmap=plt.cm.binary)
- plt.axis('on') # 关掉坐标轴为 off
- plt.title('image') # 图像题目
- plt.show()
-
- print("图像数据形状和对应数据为:", train_data0.shape)
- print("图像标签形状和对应数据为:", train_label_0.shape, train_label_0)
- print("\n打印第一个batch的第一个图像,对应标签数字为{}".format(train_label_0))
-
- class MNIST(paddle.nn.Layer):
- def __init__(self):
- super(MNIST, self).__init__()
-
- # 定义一层全连接层,输出维度是1
- self.fc = paddle.nn.Linear(in_features=784, out_features=1)
-
- # 定义网络结构的前向计算过程
- def forward(self, inputs):
- outputs = self.fc(inputs)
- return outputs
-
- model = MNIST()
-
- def train(model):
- # 启动训练模式
- model.train()
- # 加载训练集 batch_size 设为 16
- train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'),
- batch_size=16,
- shuffle=True)
- # 定义优化器,使用随机梯度下降SGD优化器,学习率设置为0.001
- opt &

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。