Keras GridSearchCV - PullRequest
       29

Keras GridSearchCV

0 голосов
/ 24 апреля 2020

Невозможно найти синтаксис gridsearch для функциональных моделей.

Это моя модель.

def create_model():
    lstm_input = Input(shape=(window, 10), name='lstm_input')
    dense_input = Input(shape=(7,), name='tech_input')


    # the first branch operates on the first input
    x = LSTM(32, name='lstm_0')(lstm_input)
    x = Dropout(dropout_rate1, name='lstm_dropout_0')(x)
    lstm_branch = Model(inputs=lstm_input, outputs=x)

    # the second branch opreates on the second input
    y = Dense(32, name='tech_dense_0')(dense_input)
    y = Activation("relu", name='tech_relu_0')(y)
    y = Dropout(dropout_rate1, name='tech_dropout_0')(y)
    dense_branch = Model(inputs=dense_input, outputs=y)

    # combine the output of the two branches
    combined = concatenate([lstm_branch.output, dense_branch.output], name='concatenate')
    z = Dense(64, activation="sigmoid", name='dense_pooling')(combined)
    z = Dense(3, activation="softmax", name='dense_out')(z)

    model = Model(inputs=[lstm_branch.input, dense_branch.input], outputs=z)
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
    return model

Я бы назвал подходящую модель следующим образом:

model.fit(x=[lstm_train, dense_train], y=y_train, batch_size=batch_size, epochs=num_epochs) shuffle=True,

Теперь я пытаюсь выполнить поиск по сетке. Но не удалось найти синтаксис для функциональной модели. Это в основном примеры последовательных.

model1 = KerasClassifier(build_fn=create_model, verbose=0)
# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [1, 2, 3, 5, 8, 10]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model1, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(X, y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

эта строка является виновником:

grid_result = grid.fit(X, y)

Я пробовал:

grid_result = grid.fit(X=[lstm_train, dense_train], y=y_train)
ValueError: Found input variables with inconsistent numbers of samples: [2, 1568174]

формы моих данных следующим образом: lstm_train: (1568174, 12, 10) dens_train: (1568174, 7)
y_train: (1568174, 3)

есть идеи, какой должен быть синтаксис? Большое спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...