from matplotlib import pyplot as plt plt.figure() fig, ax = plt.subplots(2,5) ax = ax.flatten()for i inrange(10):# 获取 Y == i 的 X0 的第一个样本,并转换为28 * 28的矩阵 Im = X0[Y0 == i][0].reshape(28,28) ax[i].imshow(Im) plt.show()
第四步,转化Y变量形式。
# 转换为 one-hot 型因变量 import tensorflow as tf from keras.utils import to_categorical YY0 = to_categorical(Y0) YY1 = to_categorical(Y1)
第五步,构建模型。
from keras.layers import Activation, Dense, Flatten, Input from keras import Model input_shape =(784,) input_layer = Input(input_shape) x = input_layer x = Dense(10)(x) x = Activation('softmax')(x) output_layer = x model = Model(input_layer, output_layer) model.summary()
第六步,拟合模型,得到模型参数。
from keras.optimizers import Adam # 设置模型参数 model.compile(optimizer = Adam(0.01), loss = 'categorical_crossentropy', metrics = ['accuracy']) # 拟合模型 model.fit(X0,YY0, validation_data = (X1, YY1), batch_size = 100, epochs = 10)