目录
关于compile和load_model()的使用顺序
我们以MNIST手写数字识别为例
importnumpy as npfrom keras.datasets importmnistfrom keras.utils importnp_utilsfrom keras.models importSequentialfrom keras.layers importDensefrom keras.optimizers importSGD#载入数据
(x_train,y_train),(x_test,y_test) =mnist.load_data()#(60000,28,28)
print('x_shape:',x_train.shape)#(60000)
print('y_shape:',y_train.shape)#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0],-1)/255.0x_test= x_test.reshape(x_test.shape[0],-1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test= np_utils.to_categorical(y_test,num_classes=10)#创建模型,输入784个神经元,输出10个神经元
model =Sequential([
Dense(units=10,input_dim=784,bias_initializer='one',activation&