0

我已经使用该函数创建了一个模型,并将两个嵌入作为输入以一维数组的形式传递给模型。在尝试对输入进行网格搜索时,它会引发错误,指出“找到具有不一致样本数的输入变量”。我什至试图重塑输入但徒劳无功,因为输入是一维的。

那么问题是如何对具有多个输入和单个输出的 keras MLP 模型进行超参数调整?

另一个问题是如何获得模型摘要?我尝试使用 model.summary(),但它不适用于通过函数创建的模型。

def create_baseline():
    
    activation = 'relu'
    dropout_rate = 0.2
    kernel_initializer = 'uniform'
    optimizer = 'adam'
    learning_rate = 0.001
    latent_dim = 50
    
    # Create model

    user_input = Input(shape = (1, ), name = 'user-input')
    user_embedding = Embedding(num_users + 1, latent_dim, name = 'user-embedding')(user_input)
    user_vec = Flatten(name = 'user-flatten')(user_embedding)

    movie_input = Input(shape = (1, ), name = 'movie-input')
    movie_embedding = Embedding(num_movies + 1, latent_dim, name = 'movie-embedding')(movie_input)
    movie_vec = Flatten(name = 'movie-flatten')(movie_embedding)

    merged = concatenate([user_vec, movie_vec], axis = 1, name = 'user-movie-concat')
    merged_dropout = Dropout(dropout_rate)(merged)

    fc_1 = Dense(128, name = 'fc-1', kernel_initializer = kernel_initializer, activation = activation, activity_regularizer = l2(0.01))(merged_dropout)
    fc_1_dropout = Dropout(dropout_rate, name = 'fc-1-dropout')(fc_1)
    fc_2 = Dense(64, name = 'fc-2', kernel_initializer = kernel_initializer, activation = activation, activity_regularizer = l2(0.01))(fc_1_dropout)
    fc_2_dropout = Dropout(dropout_rate, name = 'fc-2-dropout')(fc_2)
    fc_3 = Dense(32, name = 'fc-3', kernel_initializer = kernel_initializer, activation = activation, activity_regularizer = l2(0.01))(fc_2_dropout)
    fc_3_dropout = Dropout(dropout_rate, name = 'fc-3-dropout')(fc_3)
    fc_4 = Dense(1, name = 'fc-4', kernel_initializer = kernel_initializer, activation = activation)(fc_3_dropout)

    model = Model([user_input, movie_input], fc_4)
    model.compile(loss = 'mean_squared_error', optimizer = optimizer, metrics = [rmse, 'mae'])
    
    return model


from keras.callbacks import EarlyStopping
from keras.wrappers.scikit_learn import KerasRegressor

early_stop = EarlyStopping(monitor = 'val_loss', patience = 2, verbose = 0)

# Create a KerasRegressor
model = KerasRegressor(build_fn = create_baseline, epochs = 10, batch_size = 8, verbose = 0, callbacks = [early_stop])

# Defining the parameters to tune.

mlp_params = {'batch_size': [2, 4, 8, 16, 32, 64], 
              'learning_rate': [0.0001, 0.001, 0.01, 0.1],
              'dropout_rate': [0.0, 0.1, 0.2, 0.3, 0.4, 0.5],
              'optimizer': ['Adam', 'RMSprop', 'SGD', 'Adagrad'],
              'kernel_initializer': ['uniform', 'normal', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']}

# Using Grid Search to tune the hyperparameters. 

from sklearn.model_selection import GridSearchCV

gs_mlp_model = GridSearchCV(estimator = model, param_grid = mlp_params, scoring = 'neg_mean_squared_error', cv = 5, n_jobs = -1, verbose = 1)

gs_mlp_model.fit([X_train_1.UserID.values, X_train_1.MovieID.values], y_train_1.values)


# The error is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-157-dcc10ddae7b6> in <module>
----> 1 gs_mlp_model.fit([X_train_1.UserID.values, X_train_1.MovieID.values], y_train_1.values)

~\anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    648             refit_metric = 'score'
    649 
--> 650         X, y, groups = indexable(X, y, groups)
    651         fit_params = _check_fit_params(X, fit_params)
    652 

~\anaconda3\lib\site-packages\sklearn\utils\validation.py in indexable(*iterables)
    246     """
    247     result = [_make_indexable(X) for X in iterables]
--> 248     check_consistent_length(*result)
    249     return result
    250 

~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
    210     if len(uniques) > 1:
    211         raise ValueError("Found input variables with inconsistent numbers of"
--> 212                          " samples: %r" % [int(l) for l in lengths])
    213 
    214 

ValueError: Found input variables with inconsistent numbers of samples: [2, 800167]

4

0 回答 0