'How can I overcome the RunTimeError while using ANN on mnist fashion dataset

I am trying to build an ANN model using the mnist fashion dataset but I am not able to train my model.

CODE -

from tensorflow.keras.models import Sequential
import keras_tuner as kt
from tensorflow import keras
from keras_tuner import RandomSearch
from keras_tuner import HyperParameters

def ann_model(hp):
    ann= keras.Sequential()
    for i in range(hp.Int('num_layers',1,20)):
        ann.add(layers.Dense(units = hp.Int('layer_'+str(i),
                                            min_value=32,
                                            max_value=512,
                                            step=32),
                            activation='relu'))
    ann.add(layers.Dense(10,activation='softmax'))
    ann.compile(
        optimizer=keras.optimizers.Adam
        (hp.Choice('learning_rate',[1e-2,1e-3,1e-4])),
                    loss='sparse_categorical_crossentropy',
                    metrics=['accuracy'])
    return ann_model

ann_tuner = RandomSearch(ann_model,
                            objective='val_accuracy',
                            max_trials=5,
                            executions_per_trial=3,
                            directory='output2_ann',
                            project_name="Mnist Fashion")
        
ann_tuner.search(train_img,y_train,epochs=5,validation_data=(test_img,y_test))

I have tried importing models differently as mentioned in other similar posts but nothing seems to be working out. Please HELP

ERROR -

RuntimeError: Model-building function did not return a valid Keras Model instance, found <function ann_model at 0x169da61f0>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source