Closed Preet-Sojitra closed 10 months ago
Like the error says you need to include the parameter in the call to the KerasRegressor constructor. You can also get SciKeras to dynamically give you the input shape
import numpy as np
from sklearn.model_selection import RandomizedSearchCV
from scikeras.wrappers import KerasRegressor
from scipy.stats import reciprocal
from tensorflow import keras
def build_model(n_hidden, n_neurons, learning_rate, meta):
model = keras.models.Sequential()
model.add(keras.layers.InputLayer(input_shape=(meta['n_features_in_'],)))
for layer in range(n_hidden):
model.add(keras.layers.Dense(n_neurons, activation="relu"))
model.add(keras.layers.Dense(1))
optimizer = keras.optimizers.SGD(learning_rate=learning_rate)
model.compile(loss="mse", optimizer=optimizer)
return model
keras_reg = KerasRegressor(build_model, n_hidden=1, n_neurons=30, learning_rate=3e-3)
param_distribs = {
"n_hidden": [0, 1, 2, 3],
"n_neurons": np.arange(1, 100).tolist(),
"learning_rate": reciprocal(3e-4, 3e-2).rvs(1000).tolist(),
}
x = np.random.uniform(size=(10, 8))
y = np.random.randint(0, 1, size=(10,))
rnd_search_cv = RandomizedSearchCV(keras_reg, param_distribs, n_iter=1, cv=1, verbose=2)
rnd_search_cv.fit(x, y, epochs=100, callbacks=[keras.callbacks.EarlyStopping(patience=10)])
It's probably worth taking a look at these two tutorials:
I have this function which build and compiles the model:
And here I am passing that model to
KerasRegressor
Here's the code for
RandomizedSearchCV
:This is throwing following error:
Any way to solve to make it working??