Ya M IN
Ya M IN

Reputation: 23

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

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
import numpy as np

frames=[singleteamone_tweets,singleteamtwo_tweets,twoteam_tweets]
All_data = pd.concat(frames)

#creating a bag of words model
from sklearn.feature_extraction.text import CountVectorizer
cv= CountVectorizer()
X=cv.fit_transform(All_data.iloc[:,0]).toarray()  #.iloc[:,0]
Y=All_data.iloc[:,1].values


#training and test dataset

from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.25,random_state=0)

#Reshape 
#X_train = np.random.rand(3, 4)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))





classifier=Sequential()
#LSTM layers and Dropout regularization
classifier.add(LSTM(units=30,return_sequences=True,input_shape=(X_train.shape[1],1)))
classifier.add(Dropout(0.2))

classifier.add(LSTM(units=30,return_sequences=True))
classifier.add(Dropout(0.2))

classifier.add(LSTM(units=30,return_sequences=True))
classifier.add(Dropout(0.2))

classifier.add(LSTM(units=30))
classifier.add(Dropout(0.2))

#output layer
classifier.add(Dense(units=1, init='uniform', activation='sigmoid'))

I am getting an error while checking the accuracy its showing attribute error :- history object has no attribute ' evaluate'. i tried to fix this nothing it working.AttributeError: 'History' object has no attribute 'evaluate'

#compiling the RNN classifier.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

model=classifier.fit(X_train,Y_train,epochs=30,batch_size=15)



# Final evaluation of the model

test_loss, test_acc = model.evaluate(X_test)

Upvotes: 1

Views: 13677

Answers (2)

lukas ablatic
lukas ablatic

Reputation: 1

It should work

history=classifier.fit(X_train,Y_train,epochs=30,batch_size=15)

test_loss, test_acc = model.evaluate(X_test)

Upvotes: 0

Rishit Dagli
Rishit Dagli

Reputation: 1006

You should instead do something like this-

history = model.fit(...)

According to Keras documentation, the model.fit method returns a History callback, which has a history attribute containing the lists of successive losses and other metrics.

history has no attribute called 'evaluate' you should instead do this to get the validation accuracies. evaluate is an attribute of the model object.

history.history['accuracy']

This returns you a list of the epoch wise training accuracy.

To find your final test accuracy you should use the evaluate attribute on the model object-

You also need to give it y_test as you are trying to calculate the accuracy.

model.evaluate(X_test, y_test)

# This could also be a generator instead
# model.evaluate(test_generator)

The above piece of code

Quoted from evaluate() method documentation:

Returns

Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs.

Therefore, you can use metrics_names property of your model to find out what each of those values corresponds to. For example, if you write:

print(model.metrics_names)

This gives you the names of all the metrics. Since you need accuracy if you compile the model with-

metrics=["accuracy"]

You would also receive the accuracy in the model.evaluate() output.

Upvotes: 2

Related Questions