在 mnist 时尚数据集上使用 ANN 时如何克服 RunTimeError

发布于 2025-01-18 10:57:02 字数 1518 浏览 0 评论 0原文

我正在尝试使用 mnist 时尚数据集构建 ANN 模型,但我无法训练我的模型。

代码 -

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))

我尝试过以其他类似帖子中提到的方式导入模型,但似乎没有任何效果。请帮助

错误 -

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文