我得到以下错误,我无法找出原因:
RuntimeError: Model-building function没有返回一个有效的Keras Model实例,found (<tensorflow.python.keras.engine.functional)函数对象0x7f74d8b849d0>, <tensorflow.python.keras.engine.functional。函数对象在0x7f74d8b80810>)
我已经阅读了这里和这里的答案,这些答案似乎告诉我要从tensorflow
进口keras
,而不是独立的keras
,我正在这样做,但仍然得到错误。如果你能帮忙解决这个问题,我将非常感激。下面是我的全部代码:
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Concatenate, Lambda, GaussianNoise, Activation
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
from numba import njit
import tensorflow as tf
import numpy as np
from sklearn.model_selection import KFold
from sklearn.model_selection._split import _BaseKFold, indexable, _num_samples
from sklearn.utils.validation import _deprecate_positional_args
import pandas as pd
import kerastuner as kt
import gc
from tqdm import tqdm
from random import choices
import warnings
warnings.filterwarnings('ignore')
class MyTuner(kt.Tuner):
def run_trial(self, trial, x, y):
cv = PurgedGroupTimeSeriesSplit(n_splits=5, group_gap = 20)
val_losses = []
for train_indices, test_indices in cv.split(x, groups=x[0]):
x_train, y_train = x[train_indices, 1:], y[train_indices]
x_test, y_test = x[test_indices, 1:], y[test_indices]
x_train = apply_transformation(x_train)
x_test = apply_transformation(x_test)
model = self.hypermodel.build(trial.hyperparameters)
model.fit(x_train, y_train, batch_size = hp.Int('batch_size', 500, 5000, step=500, default=4000),
epochs = hp.Int('epochs', 100, 1000, step=200, default=500))
val_losses.append(model.evaluate(x_test, y_test))
self.oracle.update_trial(trial.trial_id, {'val_loss': np.mean(val_losses)})
self.save_model(trial.trial_id, model)
def create_autoencoder(hp, input_dim, output_dim):
i = Input(input_dim)
encoded = BatchNormalization()(i)
encoded = GaussianNoise(hp.Float('gaussian_noise', 1e-2, 1, sampling='log', default=5e-2))(encoded)
encoded = Dense(hp.Int('encoder_dense', 100, 300, step=50, default=64), activation='relu')(encoded)
decoded = Dropout(hp.Float('decoder_dropout_1', 1e-1, 1, sampling='log', default=0.2))(encoded)
decoded = Dense(input_dim,name='decoded')(decoded)
x = Dense(hp.Int('output_x', 32, 100, step=10, default=32),activation='relu')(decoded)
x = BatchNormalization()(x)
x = Dropout(hp.Float('x_dropout_1', 1e-1, 1, sampling='log', default=0.2))(x)
x = Dense(hp.Int('output_x', 32, 100, step=10, default=32),activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(hp.Float('x_dropout_2', 1e-1, 1, sampling='log', default=0.2))(x)
x = Dense(output_dim,activation='sigmoid',name='label_output')(x)
encoder = Model(inputs=i,outputs=encoded)
autoencoder = Model(inputs=i,outputs=[decoded, x])
# optimizer = hp.Choice('optimizer', ['adam', 'sgd'])
autoencoder.compile(optimizer=Adam(hp.Float('lr', 0.00001, 0.1, default=0.001)),
loss='sparse_binary_crossentropy',
metrics=['accuracy'])
return autoencoder, encoder
build_model = lambda hp: create_autoencoder(hp, X[:, 1:].shape[1], y.shape[1])
tuner = MyTuner(
oracle=kt.oracles.BayesianOptimization(
objective=kt.Objective('val_loss', 'min'),
max_trials=20),
hypermodel=build_model,
directory='./',
project_name='autoencoders')
tuner.search(X, (X,y), callbacks=[EarlyStopping('val_loss',patience=5),
ReduceLROnPlateau('val_loss',patience=3)])
encoder_hp = tuner.get_best_hyperparameters(1)[0]
print("Best Encoder Hyper-parameter:", encoder_hp)
best_autoencoder = tuner.get_best_models(1)[0]
RuntimeError: Model-building function没有返回一个有效的Keras Model实例,found (<tensorflow.python.keras.engine.functional)函数对象0x7f74d8b849d0>, <tensorflow.python.keras.engine.functional。函数对象在0x7f74d8b80810>)
(<tensorflow.python.keras.engine.functional.Functional object at 0x7f74d8b849d0>, <tensorflow.python.keras.engine.functional.Functional object at 0x7f74d8b80810>)
create_autoencoder(hp, input_dim, output_dim)
的输出
def create_autoencoder(hp, input_dim, output_dim):
# some lines of codes
return autoencoder, encoder
从我的理解,你没有使用encoder
。因此,你可以在你的函数中删除它。
函数是这样的
def create_autoencoder(hp, input_dim, output_dim):
# some lines of codes
return autoencoder
只返回aKeras模型实例。