import numpy as np
from keras import Sequential
from keras.callbacks import TensorBoard
from keras.layers import Dense
from keras.models import load_model
x = np.linspace(-10, 10, 300)
y = 3 * x + np.random.random(x.shape) * 0.44
model = Sequential()
model.add(Dense(1, activation='linear', input_shape=(1,)))
model.compile(optimizer='SGD', loss='mean_squared_error', metrics=['accuracy'])
model.summary()
model.fit(x, y, epochs=100, validation_split=0.3, verbose=2,
callbacks=[TensorBoard(log_dir='./logs', histogram_freq=1)])
model.save('../models/linear_model.h5')
predict_model = load_model('../models/linear_model.h5')
predict_y = predict_model.predict([8])
print(predict_y)
训练完后的模型可以保存下来,供下一次再训练或者拿来做predict,保存的时候只需要简单的save下就好
model.save('../models/linear_model.h5')
恢复的时候调用下load_model,就可以直接拿回来用了
predict_model = load_model('../models/linear_model.h5')