AttributeError:“历史记录"对象没有属性“预测"-拟合火车和测试数据列表 [英] AttributeError: 'History' object has no attribute 'predict' - Fitting a List of train and test data

查看:85
本文介绍了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)

现在,我得到了错误:

AttributeError:历史记录"对象没有属性预测"

AttributeError: 'History' object has no attribute 'predict'

在以前的线程 ,这似乎是火车集不是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:“历史记录"对象没有属性“预测"-拟合火车和测试数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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