2017-08-06 115 views
1

我正在嘗試使用此example的NN模型。我正在擬合一個NN模型的值列表。但是,我得到一個AttributeError。這已被問及之前已被回答。不幸的是,它不適合我。如示例所示,我創建了以下,AttributeError:'歷史'對象沒有'預測'屬性 - 擬合列表和測試數據

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: 'History' object has no attribute 'predict'

在之前的threads,它似乎是火車設置不是fitpredict之前的模型。然而,在我的,我適合他們在第二個代碼片段。任何想法我正在做什麼其他可能的錯誤?

+0

我發現https://github.com/fchollet/keras/issues/2379這一點,我有我的'功能,我的猜測是做這項工作的內部model.compile' 。仍然無法包裹我的頭,我還有什麼其他可能的錯誤。 –

回答

2

model.fit()不會返回Keras模型,而是包含您的訓練的損失和度量值的History對象。因此,在此代碼中:

NNmodelList.append(nn_model.fit(i,j)) 

您正在創建History對象列表,而不是模型。一個簡單的修正是:

NNmodelList.append(nn_model) 
nn_model.fit(i,j) 
+0

謝謝,這是現在的工作,隨時引導我談論這個問題的任何資源。 –

+0

Keras [文檔](https://keras.io/)應該具備您需要的所有API及其用法。 –

相關問題