keras 将模型保存到 h5 文件并提前停止回调

问题描述 投票:0回答:0

我在 Google Collab 上使用这段代码,但很难看到在哪里 我可以将我的输出模型 保存为 Keras H5 格式,以便按照下面的代码使用提前停止/管道的方式进行归档。有人有尝试的建议吗?

!pip install -q scikeras 
!pip install -q scikit-learn
!pip install -q tensorflow
!pip install -q pandas

import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from scikeras.wrappers import KerasRegressor
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline


from google.colab import drive
drive.mount('/content/drive/')

FILE_PATH = '/content/drive/MyDrive/regression_datasets/models/best_model.h5'
ELECTRIC_POINT = 'total_main_kw'
CSV_FILE = '/content/drive/MyDrive/regression_datasets/all_data.csv'


# clean dataset
def clean_dataset(df):
    assert isinstance(df, pd.DataFrame), "df needs to be a pd.DataFrame"
    df.dropna(inplace=True)
    indices_to_keep = ~df.isin([np.nan, np.inf, -np.inf]).any(1)
    cleaner = (f'dataset has been cleaned')
    print(cleaner)
    return df[indices_to_keep].astype(np.float64)

def my_model():
    # create model
    model = Sequential()
    model.add(Dense(22, input_shape=input_shape, kernel_initializer='normal', activation='relu'))
    model.add(Dense(14, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model

# load data
df = pd.read_csv(CSV_FILE, index_col=[0], parse_dates=True)
df = clean_dataset(df)
print(df[ELECTRIC_POINT].agg([np.min,np.max,np.mean,np.median]))

# shuffle the DataFrame rows
df = df.sample(frac=1)

X = np.array(df.drop([ELECTRIC_POINT], 1))
Y = np.array(df[ELECTRIC_POINT])

# set the input shape
input_shape = (X.shape[1],)
print(f'Feature shape: {input_shape}')

# define the Keras model
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=my_model, epochs=100, batch_size=5, verbose=0)))
pipeline = Pipeline(estimators)

# define 10-fold cross-validation
kfold = KFold(n_splits=10)

# define early stopping and model checkpoint callbacks
callbacks = [EarlyStopping(monitor='val_loss', patience=10), 
             ModelCheckpoint(filepath=FILE_PATH, monitor='val_loss', save_best_only=True)]

# evaluate the model using cross-validation with callbacks
results = []
for train_idx, test_idx in kfold.split(X, Y):
    X_train, X_test = X[train_idx], X[test_idx]
    y_train, y_test = Y[train_idx], Y[test_idx]
    pipeline.fit(X_train, y_train, mlp__validation_data=(X_test, y_test), mlp__callbacks=callbacks)
    mse = pipeline.score(X_test, y_test)
    results.append(mse)

# report performance
print("MSE: %.2f (%.2f)" % (np.mean(results), np.std(results)))
python machine-learning keras deep-learning tensorflow2.0
© www.soinside.com 2019 - 2024. All rights reserved.