本文主要以源码为主,代码中主要实现了卷积神经网络的搭建与训练,其中包括前向传播与反向传播两部分
本文参考:https://blog.csdn.net/qq_43198568/article/details/107210077
相关库与包的版本如下:
matplotlib的版本为:3.3.2
numpy的版本为:1.20.2
pandas的版本为:1.1.3
sklearn的版本为:0.23.2
tensorflow的版本为:2.4.1
tensorflow.keras的版本为:2.4.0
源码如下:
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 3 21:36:40 2021
@author:ZZJin
"""
# 参考:https://blog.csdn.net/qq_43198568/article/details/107210077
# 导入库
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
# matplotlib inline
import sklearn
import os
import sys
import time
from tensorflow import keras
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__ + "的版本为:" + module.__version__)
# 导入数据
minst = keras.datasets.mnist
# 数据拆分
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = minst.load_data()
if keras.backend.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# 数据类型转换、数据规范化
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train / 255
x_test = x_test / 255
x_train.shape
# 独热编码
y_train_onehot = tf.keras.utils.to_categorical(y_train)
y_test_onehot = tf.keras.utils.to_categorical(y_test)
# 搭建模型
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten()) # 数据一维化
model.add(tf.keras.layers.Dense(128, activation='relu')) # 全连接层
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
# 输出各层参数情况
model.summary()
# 设置优化器、损失函数等超参数
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型
history = model.fit(x_train, y_train_onehot, batch_size = 256, epochs = 1, verbose=1, validation_data = (x_test, y_test_onehot))
# 结果输出
score = model.evaluate(x_test, y_test_onehot, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
weights = model.get_weights()
# np.save('CNN_weight.npy',weights)