'Can't figure out which input shape to build a keras model with
I used keras_tuner to find the optimal hyperparameters for a model to analyze a specific dataset, but when I try to build the model to look at the output it requires an input shape. Every input shape I try returns an error, most often ValueError: In this 'tf.Variable' creation, the initial value's shape ((16, 320)) is not compatible with the explicitly supplied 'shape' argument ((784, 320)).
In the tutorial I followed, they said to use (None, 28, 28) as the input shape but inputting that into best_model.build(input_shape=(None,28,28)) just causes the above error for me.
I used this function to build my model:
def build_model(hp):
model = keras.Sequential()
model.add(layers.Flatten())
for i in range(hp.Int("num_layers", 1, 3)):
model.add(
layers.Dense(
# Tune number of units separately.
units=hp.Int(f"units_{i}", min_value=32, max_value=512, step=32),
activation=hp.Choice("activation", ["relu", "tanh"]),
)
)
if hp.Boolean("dropout"):
model.add(layers.Dropout(rate=0.25))
model.add(layers.Dense(1, activation="softmax"))
learning_rate = hp.Float("lr", min_value=1e-4, max_value=1e-2, sampling="log")
model.compile(
optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"],
)
return model
and used this code to return the model
tuner = kt.RandomSearch(
build_model,
objective='val_accuracy',
overwrite=True,
max_trials=10,
executions_per_trial=1)
tuner.search(x_train, y_train, epochs=100, validation_data=(x_test, y_test))
best_model = tuner.get_best_models()[0]
The shape of my input data is (562844, 16), but inputting that as the input_shape returns the error: ValueError: Input 0 of layer "dense" is incompatible with the layer: expected axis -1of input shape to have value 784, but received input with shape (562844, 16).
Any suggestions for finding the right input shape or otherwise fixing the problem would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
