之前的函数,都是把网络直接写在model里,函数式API就是一层一层写出来
这篇是https://www.bilibili.com/video/BV1Z541167M3?p=24的笔记
#函数式api
input=keras.layers.Input(shape=x_train.shape[1:])
hidden1=keras.layers.Dense(30,activation='relu')(input)
hidden2=keras.layers.Dense(30,actication='relu')(hidden1)
#把input和hidden2结合起来(wipe&deep模型)
concat=keras.layers.concatenate([input,hidden2])
output=keras.layers.Dense(1)(concat)
model=keras.Model(inputs=[input],outputs=[output])
model.compile(loss='mean_squared_error',optimizer='sgd')
#回调函数
callbacks=[keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)]
history=model.fit(x_train_scaled,y_train,
validation_data=(x_valid_scaled,y_valid),
epochs=100,
callbacks=callbacks)