Mnist手写体识别实验报告

实验(二)

代码段:

# 实验环境:MindSpore-python3.7-aarch64

import os
# os.environ['DEVICE_ID'] = '0'

import mindspore as ms
import mindspore.context as context
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV

from mindspore import nn
from mindspore.train import Model
from mindspore.train.callback import LossMonitor

context.set_context(mode=context.GRAPH_MODE, device_target='Ascend') # Ascend, CPU, GPU

data_train = os.path.join("MNIST", 'train') # train set
data_test = os.path.join("MNIST", 'test') # test set
ds = ms.dataset.MnistDataset(data_train)
print(data_train)

请添加图片描述
以上在华为云上不断报错,而且无法排除错误,于是我参考了网上视频教程,使用keras(基于tensorflow框架)第三方库在本地电脑上运行:

# 准备工作:

pip install keras
pip install tensorflow

第一次代码:

# 实验环境:pycharm, python 3.8
# 框架:tensorflow

from keras.utils import to_categorical
from keras import models, layers, regularizers
from keras.optimizers import RMSprop
from keras.datasets import mnist # 导入数据集
import matplotlib.pyplot as plt

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 下载数据集
# print(train_images.shape, test_images.shape) # 打印输出形状
# print(train_images[0])
# print(train_labels[0])
# plt.imshow(train_images[0])
# plt.show()

# 将二维数据铺开成一维
train_images = train_images.reshape((60000, 28*28)).astype('float') # 784,输入层有784个神经元
test_images = test_images.reshape((10000, 28*28)).astype('float')
# 标签值编码
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# print(train_labels[0])

# 搭建神经网络
# 输入层28*28个神经元,隐藏层15个神经元,输出层10个神经元(0~9)
network = models.Sequential() # 序列式模型
network.add(layers.Dense(units=15, activation='relu', input_shape=(28*28, ), )) # 隐藏层
# Dense:全连接层
# units:15个神经元
# 激活函数:ReLu(sigmoid和tanh会产生梯度弥散现象,自变量很大时,图像很平缓,梯度下降十分缓慢)
network.add(layers.Dense(units=10, activation='softmax')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个代码我看不懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值