Keras Tuner-模型构建功能未返回有效的Keras Model实例 [英] Keras Tuner - Model-building function did not return a valid Keras Model instance

查看:35
本文介绍了Keras Tuner-模型构建功能未返回有效的Keras Model实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Keras Tuner在模型的超参数中搜索,但是在运行代码时出现此错误:"RuntimeError:模型构建函数未返回有效的Keras Model实例,发现< keras.engine.sequential.Sequential对象位于0x000001E9C2903F28>"

I'm trying to search Hyperparameters for a model using Keras Tuner, but I'm getting this error when I run the code: "RuntimeError: Model-building function did not return a valid Keras Model instance, found < keras.engine.sequential.Sequential object at 0x000001E9C2903F28 >"

我已经在Internet上进行搜索,但是没有找到任何可以帮助的东西,我也遵循了Keras Tuner gitHub页面中的教程(

I've searched on Internet but didn't found anything that could help, also I've followed the tutorial in the Keras Tuner gitHub page (https://github.com/keras-team/keras-tuner), but it dind't work either.

这是我的代码:

class MyHyperModel(HyperModel):

    def __init__(self, num_classes):
        self.num_classes = num_classes

    def build(self, hp):
        model=Sequential()
        model.add(Dense(units=hp.Int('units_0', 30, 900, step=30),
                        activation=hp.Choice('act_0', ['relu', 'tanh']),
                        input_dim=12))
        for i in range(hp.Int('layers', 3, 9)):
            model.add(Dense(units=hp.Int('units_' + str(i), 30, 900, step=30),
                            activation=hp.Choice('act_' + str(i), ['relu', 'tanh'])))
        model.add(Dense(6, activation='softmax'))
        model.compile(loss='categorical_crossentropy',
                        optimizer=hp.Choice('optimizer', ['adam', 'sgd']),
                        metrics=['categorical_accuracy'])
        return model


hypermodel = MyHyperModel(num_classes=6)

tuner = kt.tuners.bayesian.BayesianOptimization(
    hypermodel,
    objective='val_accuracy',
    max_trials=5,
    executions_per_trial=3,
    seed=(np.random.seed(1)),
    directory='Tests',
    project_name='test')

tuner.search_space_summary()

tuner.search(data[:200], labels[:200],
             verbose=2,
             epochs=3,
             validation_data=(data[200:], labels[200:]))

models = tuner.get_best_models(num_models=2).summary()
tuner.get_best_hyperparameters()[0].values
tuner.results_summary()

数据是300个向量的列表,其中包含12个值,标签上有6个类,这些类已通过tensorflow.convert_to_tensor()函数转换为张量.

data is an list of 300 vector with 12 values and on labels there are 6 classes which was converted to tensor with the function tensorflow.convert_to_tensor().

感谢您的帮助.

推荐答案

我知道这是什么错误,而不是代码,我的模型的最后一层有6个神经元,我将损失用作'categorical_crossentropy',但这仅在标签分别为0和1时有效,因此我将损失更改为"sparse_categorical_crossentropy",而将度量标准更改为准确性",并且它起作用了.感谢大家的答复,感谢您的帮助.

I know what's wrong and is not the code, my model have 6 neurons on the last layer and I've used the loss as 'categorical_crossentropy', but this only works when the labels are 0 and 1, so I've changed the loss to 'sparse_categorical_crossentropy' and metrics to 'accuracy' and it worked. Thanks everyone for the reply, I appreciate the help.

这篇关于Keras Tuner-模型构建功能未返回有效的Keras Model实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆