问题描述
我正在尝试使用此示例.我正在为NN模型拟合值列表.但是,我得到一个AttributeError
.这已经被问过并且已经得到回答.不幸的是,它对我不起作用.如示例所示,我创建了以下内容,
I am trying a NN model using this example. I am fitting a list of values to a NN model. However, I am getting an AttributeError
. This has been asked before and has been answered. Unfortunately, it is not working for me. As shown in the example, I created the following,
from keras.models import Sequential
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from keras.layers import Dense
def neuralnetmodel():
#Crete model
model = Sequential()
model.add(Dense(13, input_dim = 13, kernel_initializer = 'normal', activation = 'relu'))
model.add(Dense(1, kernel_initializer = 'normal', activation = 'relu'))
model.add(Dense(1, kernel_initializer = 'normal', activation = 'relu'))
## Output layer
model.add(Dense(1, kernel_initializer = 'normal'))
#Compile model
model.compile(loss = 'mean_squared_error', optimizer = 'adam')
return model
fit
训练数据,
NNmodelList = []
for i,j in zip(X_train_scaled,y_train):
nn_model = KerasRegressor(build_fn= neuralnetmodel, nb_epoch = 50, batch_size = 10, verbose = 0)
NNmodelList.append(nn_model.fit(i,j))
predict
来自测试数据,
PredList = []
for val in X_test_scaled:
for mod in NNmodelList:
pred = mod.predict(val)
PredList.append(pred)
现在,我得到了错误:
在以前的线程 ,这似乎是火车集不是predict
之前的模型的fit
.但是,在我的系统中,我将它们放入第二个代码段中.有什么想法我还会犯其他错误吗?
In previous threads , it seems to be the train set was not fit
to the model before predict
. However, in mine, I fit them in the second code snippet. Any ideas what other possible mistakes I am making?
推荐答案
model.fit()不返回Keras模型,而是一个History对象,其中包含训练的损失和度量值.所以在这段代码中:
model.fit() does not return the Keras model, but a History object containing loss and metric values of your training. So in this code:
NNmodelList.append(nn_model.fit(i,j))
您正在创建历史记录对象列表,而不是模型.一个简单的解决方法是:
you're creating a list of History objects, not models. A simple fix would be:
NNmodelList.append(nn_model)
nn_model.fit(i,j)
这篇关于AttributeError:“历史记录"对象没有属性“预测"-拟合火车和测试数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!