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)