资源简介
人工智能算法实现mnist手写数字识别
代码片段和文件信息
import tensorflow as tf
import os
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras.layers import Conv2D BatchNormalization Activation MaxPool2D Dropout Flatten Dense
from tensorflow.keras import Model
np.set_printoptions(threshold=np.inf)
mnist = tf.keras.datasets.mnist
#fashion = tf.keras.datasets.fashion_mnist
(x_train y_train) (x_test y_test) = mnist.load_data()
x_train x_test = x_train / 255.0 x_test / 255.0
print(“x_train.shape“ x_train.shape)
x_train = x_train.reshape(x_train.shape[0] 28 28 1) # 给数据增加一个维度,使数据和网络结构匹配
x_test = x_test.reshape(x_test.shape[0] 28 28 1)
print(“x_train.shape“ x_train.shape)
class baseline(Model):
def __init__(self):
super(baseline self).__init__()
self.c1 = Conv2D(filters=6 kernel_size=(5 5) padding=‘same‘) # 卷积层
self.b1 = BatchNormalization() # BN层
self.a1 = Activation(‘relu‘) # 激活层
self.p1 = MaxPool2D(pool_size=(2 2) strides=2 padding=‘same‘) # 池化层
self.d1 = Dropout(0.2) # dropout层
self.flatten = Flatten()
self.f1 = Dense(128 activation=‘relu‘)
self.d2 = Dropout(0.2)
self.f2 = Dense(10 activation=‘softmax‘)
def call(self x):
x = self.c1(x)
x = self.b1(x)
x = self.a1(x)
x = self.p1(x)
x = self.d1(x)
x = self.flatten(x)
x = self.f1(x)
x = self.d2(x)
y = self.f2(x)
return y
model = baseline()
model.compile(optimizer=‘adam‘
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
metrics=[‘sparse_categorical_accuracy‘])
checkpoint_save_path = “./checkpoint/baseline.ckpt“
if os.path.exists(checkpoint_save_path + ‘.index‘):
print(‘-------------load the model-----------------‘)
model.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path
save_weights_only=True
save_best_only=True)
history = model.fit(x_train y_train batch_size=32 epochs=5 validation_data=(x_test y_test) validation_freq=1
callbacks=[cp_callback])
model.summary()
# print(model.trainable_variables)
file = open(‘./weights.txt‘ ‘w‘)
for v in model.trainable_variables:
file.write(str(v.name) + ‘\n‘)
file.write(str(v.shape) + ‘\n‘)
file.write(str(v.numpy()) + ‘\n‘)
file.close()
############################################### show ###############################################
# 显示训练集和验证集的acc和loss曲线
acc = history.history[‘sparse_categorical_accuracy‘]
val_acc = history.history[‘val_sparse_categorical_accuracy‘]
loss = history.history[‘loss‘]
val_loss = history.history[‘val_loss‘]
plt.subplot(1 2 1)
plt.plot(acc label=‘Training Accuracy‘)
plt.plot(val_acc label=‘Validation Accuracy
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3330 2020-07-11 03:54 mnist实验报告\ML_MNIST\CNN.py
文件 1495 2020-07-11 04:48 mnist实验报告\ML_MNIST\FCN.py
文件 1843 2020-07-11 05:38 mnist实验报告\ML_MNIST\RNN.py
文件 256512 2020-11-13 22:11 mnist实验报告\mnist实验报告.doc
目录 0 2020-07-11 06:00 mnist实验报告\ML_MNIST
目录 0 2020-11-13 22:11 mnist实验报告
----------- --------- ---------- ----- ----
263180 6
- 上一篇:用Python学微积分.azw3
- 下一篇:小说阅读项目源码(附数据库脚本)
相关资源
- 手写数字识别.ipynb
- 利用CNN网络实现mnist图像分类,手动实
- MNIST手写体数字训练/测试数据集(图
- OCR:一个有趣的网页版手写数字识别
- Python3实现KNN的三个包含数据集,水果
- 人工智能算法合集-python实现
- usps手写数据集+使用代码.zip
- bayes分类python
- knn算法识别mnist图片-python3
- Ubuntu18.04LTS下安装 Caffe-GPU版本及 Ana
- python不使用框架实现卷积神经网络识
- mnist手写数字识别数据集npz文件.zip
- 基于Python的手写字体识别系统
- Python学习实践-sklearn分类算法实践-M
- 使用python实现人工智能算法
- pytorch版本手写体识别MNIST.zip
- 自己编写的MNIST手写字Python实现
- mnist手写字体识别之BP.zip
- tensorflow操作mnist数据集源代码
- 使用knn对MNIST分类
- tensorflow手写数字识别完整版.zip
- 手写数字图像识别
- 基于tensorflow的手写体识别python源码附
- 逻辑回归python代码
- mnist手写字体识别之随机森林.zip
- win10+anaconda3+python3 mnist训练代码
- 神经网络实现简单的手写数字识别
- 手写数字识别:Python+BP神经网络+PYQ
- 基于TensorFlow的手写数字识别程序
- 代码:Python+TensorFlow+PyQt实现手写体数
评论
共有 条评论