Search code examples
pythonnumpyattributeerrorconv-neural-networksequential

AttributeError: 'Sequential' object has no attribute 'score'


i was working with convolutional neural nertworks , while using sequential i got problemsto train data. using sequential is it not possible to get best score??

from numpy import array
from numpy import reshape
import numpy as np
def model_CNN(X_train,Y_train,X_test,Y_test):

    model = Sequential()

    model.add(Conv1D(filters=512, kernel_size=32, padding='same', kernel_initializer='normal', activation='relu', input_shape=(256, 1)))
    model.add(Conv1D(filters=512, kernel_size=32, padding='same', kernel_initializer='normal', activation='relu'))
    model.add(Dropout(0.2)) # This is the dropout layer. It's main function is to inactivate 20% of neurons in order to prevent overfitting
    model.add(Conv1D(filters=256, kernel_size=32, padding='same', kernel_initializer='normal', activation='relu'))
    model.add(Dropout(0.2))
    model.add(Conv1D(filters=256, kernel_size=32, padding='same', kernel_initializer='normal', activation='relu'))

    model.add(Flatten())

    optimizer = keras.optimizers.SGD(lr=0.01, momentum=0.5)
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    convolutional_model = model.fit(X_train, Y_train, epochs=5,batch_size=64,verbose=1, validation_data=(X_test, Y_test))
    print(convolutional_model.score(X_train,Y_train))
    model.summary()
    return model

Traceback recived as error:

AttributeError                            Traceback (most recent call last)
<ipython-input-50-9a2005301144> in <module>()
      1 
----> 2 convolutional_model= model_CNN(X_train,Y_train,X_test,Y_test)
      3 print(convolutional_model)

<ipython-input-49-bac0ec08f100> in model_CNN(X_train, Y_train, X_test, Y_test)
     34     model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
     35     convolutional_model = model.fit(X_train, Y_train, epochs=5,batch_size=64,verbose=1, validation_data=(X_test, Y_test))
---> 36     print(convolutional_model.score(X_train,Y_train))
     37     # Print the summary of the model
     38     model.summary()

AttributeError: 'Sequential' object has no attribute 'score'

since im new to python,i got troubled and checked various resourses,but nothing helped,please guide me... i got the error from this line

print(convolutional_model.score(X_train,Y_train))

if it's not possible please guide me for a better one...


Solution

  • You should use model not convolutional_model object. fit function returns an history object which contains some information about training phase like loss, accuracy.. it depends on your loss function and metric functions.

    Can you try this?

    print(model.evaluate(X_train, Y_train))