python 属性错误:"Adam"对象没有属性"build":我该如何解决这个问题[已关闭]

unhi4e5o  于 2023-02-21  发布在  Python
关注(0)|答案(1)|浏览(693)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
50分钟前就关门了。
Improve this question
我无法用我的代码创建Ann模型,因为这个错误消息每次都会出现。我该如何修复它?编辑:我使用的是Tensorflow Version 2.11.0错误消息:属性错误:'Adam'对象没有属性'build'我尝试过使用不同的优化器,但它们总是以这个错误消息结束。

import argparse
import logging
import math
import os
from keras.layers import Dense, Dropout
from keras.models import Sequential
from scikeras.wrappers import KerasClassifier, KerasRegressor
from sklearn.model_selection import GroupKFold, GridSearchCV, RandomizedSearchCV
import pandas as pd
from sklearn.model_selection import GroupKFold, GridSearchCV

def create_model(neurons_1, dropout_1, neurons_2, dropout_2, neurons_3, dropout_3):
     # create model
     model = Sequential()
     model.add(Dense(neurons_1, input_shape=(20,), activation='tanh'))
     model.add(Dropout(dropout_1, seed=0))
     model.add(Dense(neurons_2, activation="tanh"))
     model.add(Dropout(dropout_2, seed=0))
     model.add(Dense(neurons_3))
     model.add(Dropout(dropout_3, seed=0))
     model.add(Dense(3, activation='softmax'))
     model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
     return model

model = create_model(350, 0.025, 450, 0.075, 400, 0.4)
2izufjch

2izufjch1#

使用tf.keras代替keras对我很有效。尝试以下操作:

from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.models import Sequential

def create_model(neurons_1, dropout_1, neurons_2, dropout_2, neurons_3, dropout_3):
     # create model
     model = Sequential()
     model.add(Dense(neurons_1, input_shape=(20,), activation='tanh'))
     model.add(Dropout(dropout_1, seed=0))
     model.add(Dense(neurons_2, activation="tanh"))
     model.add(Dropout(dropout_2, seed=0))
     model.add(Dense(neurons_3))
     model.add(Dropout(dropout_3, seed=0))
     model.add(Dense(3, activation='softmax'))
     model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
     return model
create_model(10,0.5,10,0.5,10,0.5)

阅读Should I use the standalone Keras library or tf.keras?了解更多信息。

相关问题