keras.models.load_model
from sklearn.datasets import make_circles
from os.path import exists
from keras.models import Sequential, load_model
from keras.layers import Dense
"""创建样本"""
x_train, y_train = make_circles(n_samples=256, noise=.1, factor=.4)
"""建模"""
filepath = 'model.hdf5'
if exists(filepath):
model = load_model(filepath) # 加载模型
else:
model = Sequential()
model.add(Dense(units=25, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile('RMSprop', 'binary_crossentropy', ['acc'])
model.fit(x_train, y_train, epochs=125)
model.save(filepath) # 保存模型
"""模型评估"""
x_test, y_test = make_circles(n_samples=128, noise=.1, factor=.4)
loss, acc = model.evaluate(x_test, y_test)
print('loss %g acc %g' % (loss, acc))
keras.callbacks.ModelCheckpoint
from sklearn.datasets import make_circles
from os.path import exists
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
"""创建样本"""
x_train, y_train = make_circles(n_samples=512, noise=.1, factor=.4)
"""建模"""
filepath = 'model.hdf5'
if exists(filepath):
model = load_model(filepath) # 加载模型
else:
model = Sequential()
model.add(Dense(25, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile('RMSprop', 'binary_crossentropy', ['acc'])
model.fit(
x_train, y_train, epochs=100, verbose=2,
validation_split=.125,
callbacks=[ModelCheckpoint(filepath, save_best_only=True)]
)
"""模型评估"""
x_test, y_test = make_circles(n_samples=256, noise=.1, factor=.4)
loss, acc = model.evaluate(x_test, y_test)
print('loss %g acc %g' % (loss, acc))
keras.callbacks.EarlyStopping
from sklearn.datasets import make_circles
from os.path import exists
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint, EarlyStopping
"""创建样本"""
x_train, y_train = make_circles(n_samples=512, noise=.1, factor=.4)
"""建模"""
filepath = 'model.hdf5'
if exists(filepath):
model = load_model(filepath) # 加载模型
else:
model = Sequential()
model.add(Dense(25, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile('RMSprop', 'binary_crossentropy', ['acc'])
callbacks = [
EarlyStopping(monitor='val_acc', patience=10),
ModelCheckpoint(filepath, 'val_acc', save_best_only=True)
]
model.fit(
x_train, y_train, epochs=999, verbose=2,
validation_split=.125, callbacks=callbacks
)
"""模型评估"""
x_test, y_test = make_circles(n_samples=256, noise=.1, factor=.4)
loss, acc = model.evaluate(x_test, y_test)
print('loss %g acc %g' % (loss, acc))
from sklearn.datasets import make_circles
from os.path import exists
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint, EarlyStopping
class Conf:
"""配置"""
filepath = 'model.hdf5' # 模型保存路径
patience = 10 # 进步停止轮数
units = 25 # 神经元数量
validation_split = .125 # 验证切分率
batch_size = 512
class Model:
def __init__(self):
"""模型初始化"""
self.model = None
if exists(Conf.filepath):
self.model = load_model(Conf.filepath)
else:
x_train, y_train = make_circles(n_samples=512, noise=.1, factor=.4)
self.modeling(x_train, y_train)
def modeling(self, x, y):
"""建模"""
model = Sequential()
model.add(Dense(Conf.units, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile('RMSprop', 'binary_crossentropy', ['acc'])
callbacks = [
EarlyStopping(patience=Conf.patience),
ModelCheckpoint(Conf.filepath, save_best_only=True)
]
model.fit(
x, y, batch_size=Conf.batch_size, epochs=9999, verbose=2,
validation_split=Conf.validation_split, callbacks=callbacks
)
self.model = model
def evaluate(self, x, y):
"""模型评估"""
loss, acc = self.model.evaluate(x, y)
print('loss %g acc %g' % (loss, acc))
if __name__ == '__main__':
model = Model()
x_test, y_test = make_circles(n_samples=256, noise=.1, factor=.4)
model.evaluate(x_test, y_test)