该案例主要目的是为了熟悉Keras基本用法,以及了解DNN基本流程。
示例代码:
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.datasets import mnist
from keras.layers import Dense
from keras.utils.np_utils import to_categorical
#加载数据,训练60000条,测试10000条,X_train.shape=(60000,28,28)
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#特征扁平化,缩放,标签独热
X_train_flat = X_train.reshape(60000, 28*28)
X_test_flat = X_test.reshape(10000, 28*28)
X_train_norm = X_train_flat / 255
X_test_norm = X_test_flat / 255
y_train_onehot = to_categorical(y_train, 10) #shape为(60000,10)
y_test_onehot = to_categorical(y_test, 10) #shape为(10000,10)
#构建模型
model = Sequential()
model.add(Dense(100, activation='