今天进阶到手写识别神经网络的搭建。
源代码:https://download.csdn.net/download/rance_king/11010836
- 导入包
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
import random
np.random.seed(0)
- 导入数据并确认数据格式正确,这里有个附加的知识点,使用assert语法来鉴别数据的格式正确,如果正确则放行,如果不正确则报错。
(X_train, y_train), (X_test, y_test) = mnist.load_data()
assert(X_train.shape[0] == y_train.shape[0]), "The number of images is not equal to the number of labels."
assert(X_test.shape[0] == y_test.shape[0]), "The number of images is not equal to the number of labels."
assert(X_train.shape[1:] == (28,28)), "The dimensions of the images are not 28x28"
assert(X_test.shape[1:] == (28,28)), "The dimensions of the images are not 28x28"
- 接下来将可视化拿下来的数据集,用比较好看的格式将数据集中的图片内容呈现出来。
#我想查看一下这个数据集里面每个标签下有多少个样本,用空数组接收后面被放入的每个标签下样本数量
num_of_samples = []
cols =