keras-team / keras-tuner

A Hyperparameter Tuning Library for Keras
https://keras.io/keras_tuner/
Apache License 2.0
2.86k stars 397 forks source link

attributeerror: 'sequential' object has no attribute "get_build_config" #949

Closed areflesh closed 1 year ago

areflesh commented 1 year ago

This issue appeared after switching from version 1.3.5 to 1.4.2. No code changes were done.


if args.auto_tune:
        print('AUTO TUNING ENABLED')
        def build_model(hp):

            model = Sequential()
            model.add(Dense(input_dims+1, input_dim=input_dims, activation='relu'))
            if args.normalize:
                model.add(tf.keras.layers.BatchNormalization())
            for i in range(hp.Int('num_layers', 1,5)):
                model.add(Dense(
                hp.Choice('units', [32, 64, 128, 256, 512, 1024]),
                activation='relu'))
                model.add(Dropout(args.dropout_rate))
            model.add(Dense(2, activation='softmax'))
            if args.gradient_clipping:
                optimizer = tf.keras.optimizers.Adam(clipnorm=args.gradient_clipping_value)
            else:
                optimizer = 'adam'
            model.compile(loss="kullback_leibler_divergence", optimizer=optimizer,
                    metrics=[
                        tf.keras.metrics.BinaryAccuracy(name='accuracy'),
                        tf.keras.metrics.Precision(name='precision'),
                        tf.keras.metrics.Recall(name='recall'),
                        tf.keras.metrics.AUC(name='auc'),
                        tf.keras.metrics.AUC(name='prc', curve='PR'),])
            return model
        if args.objective == 'prc':
            objective = keras_tuner.Objective("prc", direction="max")
        elif args.objective == 'loss':
            objective = keras_tuner.Objective("loss", direction="min")
        else:
            raise ValueError('objective must be either prc or loss')
        tuner = keras_tuner.BayesianOptimization(
            build_model,
            objective=objective,
            max_trials=args.max_trials,
            executions_per_trial=1,
            directory="s3://{}/{}/model/auto_tune".format(args.s3_bucket, args.s3_dir),
            overwrite=True)

        tuner.search_space_summary()
        tuner.search(dataset,
                epochs = args.max_epochs_tune,
                verbose = 2,
                class_weight = class_weights)
        print('TUNNING DONE. BEST HYPERPARAMETERS:')
        tuner.results_summary()

        model = tuner.get_best_models(num_models=1)[0]
        model.build()    

Issue appear after each trial and after third trial it says RuntimeError: Number of consecutive failures exceeded the limit of 3 and stops 
haifeng-jin commented 1 year ago

Hi @areflesh , it is probably because the TF version you use? We do model.get_build_config() under the hood. As I tested in colab, Sequential model class has this method.

pacs27 commented 1 year ago

I have the same error when I use tensorflow version 2.10 which does not have this method

Tensorflow API v2.10 Layer

haifeng-jin commented 1 year ago

It has been fixed in master. Will be available in the next release. (1.4.3) It is much appreciated if anyone had the bug could test it on our master branch. Thanks!