#python #keras #conv-neural-network #gridsearchcv
Вопрос:
код для построения модели поиска по сетке с помощью keras (ДАННЫЕ CIFAR10), пытаясь играть с номером conv. слои и ядра:
это полный код:
def build_model(n_conv=2, n_kernels=64, n_hidden=2, n_neurons=64):
model = keras.models.Sequential()
# Input layer
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
# Conv layers
for layer in range(n_conv-1):
model.add(layers.Conv2D(n_kernels, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
# Hidden layers
for layer in range(n_hidden):
model.add(layers.Dense(n_neurons, activation="relu"))
# Output Layer
model.add(layers.Dense(10, activation ='softmax'))
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
return model
keras_clf = keras.wrappers.scikit_learn.KerasClassifier(build_model)
from sklearn.model_selection import GridSearchCV
param_distribs = { "n_conv": [2, 3],
"n_kernels": [64, 128] }
search_cv = GridSearchCV(keras_clf, param_distribs, cv=3, verbose=2)
search_cv.fit(train_images, train_labels)
Сообщение об ошибке :
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
/usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py:536: FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details:
tensorflow.python.framework.errors_impl.InvalidArgumentError: required broadcastable shapes
[[node Equal (defined at /usr/local/lib/python3.7/dist-packages/sklearn/model_selection/_validation.py:515) ]] [Op:__inference_train_function_4073]
Function call stack:
train_function
Заранее спасибо
Ответ №1:
Проблема Flatten
в том, что слой не используется после сверточных блоков, как только я обновил ваш код с его помощью, он начал нормально тренироваться.
import tensorflow as tf
import tensorflow.keras.layers as layers
import tensorflow.keras as keras
def build_model(n_conv=2, n_kernels=64, n_hidden=2, n_neurons=64):
model = keras.models.Sequential()
# Input layer
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
# Conv layers
for layer in range(n_conv-1):
model.add(layers.Conv2D(n_kernels, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
# Hidden layers
for layer in range(n_hidden):
model.add(layers.Dense(n_neurons, activation="relu"))
# Output Layer
model.add(layers.Dense(10, activation ='softmax'))
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
return model
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
keras_clf = keras.wrappers.scikit_learn.KerasClassifier(build_model)
from sklearn.model_selection import GridSearchCV
param_distribs = { "n_conv": [2, 3],
"n_kernels": [64, 128] }
search_cv = GridSearchCV(keras_clf, param_distribs, cv=3, verbose=2)
search_cv.fit(train_images, train_labels)