pythontensorflowkerasneural-networkkeras-tuner

ValueError: Tensor's shape (26, 400) is not compatible with supplied shape [26, 200]


I am working on training a neural network using keras and keras_tuner for hyper parameter tuning and ran into an error that isn't similar to other questions asked in the website before.

Here's the code that is relevant according to my knowledge:

  1. Splitting my Dataset
x = data.drop('label', axis=1).values
X = normalize(x)
y = pd.get_dummies(data['label']).values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=40)
print("Length of train set: ", X_train.shape, "y:", y_train.shape)
print("Length of test set: ", X_test.shape, "y:", y_test.shape)

which resulted like this:

Length of train set:  (7922, 26) y: (7922, 6)
Length of test set:  (3396, 26) y: (3396, 6)
  1. Building my Model
def build_model(hp):
    model = Sequential()
    step = 100
    for i in range(hp.Int('num_layers', 3, 4)):
        if i == 1:
            model.add(
                Dense(
                    units=hp.Int('units_'+str(i), min_value=step*2, max_value=step*5, default=step*2, step=step),
                    input_dim=26,
                    activation=hp.Choice('dense_activation_'+str(i), values=['relu', 'tanh', 'sigmoid'])
                )
            )
        else:
            model.add(
                Dense(units=hp.Int('units_' + str(i), min_value=step*2, max_value=step*5, default=step*2, step=step),
                      activation=hp.Choice('dense_activation_'+str(i), values=['relu', 'tanh', 'sigmoid'])
                      )
            )
        model.add(
            Dropout(
                hp.Float('dropout_' + str(i), min_value=0.0, max_value=0.1, default=0.1, step=0.02)
            )
        )
        if step > 20:
            step = step/2
        else:
            step = 10

    model.add(Dense(6, activation='softmax'))
    # optimizer = optimizers.Adam(learning_rate=0.001)
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
  1. Using Hyperband to hypertune the Parameters
tuner = kt.Hyperband(
    build_model,
    max_epochs=100,
    objective='accuracy',
    seed=42,
    executions_per_trial=2
)
tuner.search(X_train, y_train, epochs=500, validation_data=(X_test, y_test))
best_model = tuner.get_best_models()[0]

and Finally after all that i got an error which is as shown:

Traceback (most recent call last):
  File "PycharmProjects\FYP_Project\Keras training\Keras Training.py", line 75, in <module>
    best_model.evaluate(X_test, y_test)[1] * 100)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py", line 1489, in evaluate
    tmp_logs = self.test_function(iterator)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
    result = self._call(*args, **kwds)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 763, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\function.py", line 3050, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\function.py", line 3279, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 999, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1323 test_function  *
        return step_function(self, iterator)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1314 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
        return fn(*args, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1307 run_step  **
        outputs = model.test_step(data)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\training.py:1266 test_step
        y_pred = self(x, training=False)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:1030 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\sequential.py:394 call
        outputs = layer(inputs, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:1023 __call__
        self._maybe_build(inputs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:2625 _maybe_build
        self.build(input_shapes)  # pylint:disable=not-callable
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\layers\core.py:1191 build
        self.kernel = self.add_weight(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer.py:639 add_weight
        variable = self._add_variable_with_custom_getter(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\training\tracking\base.py:810 _add_variable_with_custom_getter
        new_variable = getter(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\keras\engine\base_layer_utils.py:127 make_variable
        return tf_variables.VariableV1(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:260 __call__
        return cls._variable_v1_call(*args, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:206 _variable_v1_call
        return previous_getter(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
        return captured_getter(captured_previous, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3523 creator
        return next_creator(**kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
        return captured_getter(captured_previous, **kwargs)
    C:\Users\Axell\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3523 creator
        return next_creator(**kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
        return captured_getter(captured_previous, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\distribute\distribute_lib.py:3523 creator
        return next_creator(**kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:67 getter
        return captured_getter(captured_previous, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py:750 variable_capturing_scope
        v = UnliftedInitializerVariable(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\ops\variables.py:264 __call__
        return super(VariableMetaclass, cls).__call__(*args, **kwargs)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\def_function.py:293 __init__
        initial_value = initial_value()
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\training\tracking\base.py:86 __call__
        return CheckpointInitialValue(
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\training\tracking\base.py:122 __init__
        self.wrapped_value.set_shape(shape)
    AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\ops.py:1238 set_shape
        raise ValueError(

    ValueError: Tensor's shape (26, 400) is not compatible with supplied shape [26, 200]

I'm not even sure where the 400 came from, and i realized that i have a zero on one of my layer, but when i remove that, the error still stayed the same. the new changes i've made are:

def build_model(hp):
    model = Sequential()
    for i in range(hp.Int('num_layers', 4, 5)):
        # if i == 1:
        #     model.add(Dense(units=hp.Int('units_' + str(i),
        #                                  min_value=20,
        #                                  max_value=500,
        #                                  step=20),
        #                     input_dim=26,
        #                     activation='relu'))
        # else:
            model.add(Dense(units=hp.Int('units_' + str(i),
                                                min_value=20,
                                                max_value=500,
                                                step=20),
                                   activation='relu'))
    model.add(Dense(6, activation='softmax'))
    # optimizer = optimizers.Adam(learning_rate=0.001)
    model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])), metrics=['accuracy'])
    return model

Still had the 400 from somewhere, can someone enlighten me of where it comes from?


Solution

  • I think there is bug with Keras tuner, I found the similar issue when getting the best model as below:

    models = tuner.get_best_models(num_models=2) 
    best_model = models[0]
    best_model.build(input_shape=(INPUT_FEATURE_SIZE, ))
    

    This problem can be solved by using get_best_hyperparameters() method as below:

    best_hp = tuner.get_best_hyperparameters()[0]
    best_model = tuner.hypermodel.build(best_hp)
    print("Best Model summary is as below: ")
    best_model.summary()