Anubhav
Anubhav

Reputation: 585

'NoneType' object has no attribute 'evaluate' error with hyperas in keras

I keep getting a 'NoneType' object has no attribute 'evaluate' error with the following code using hyperas and keras. Any help would be appreciated!

The error is AttributeError: 'NoneType' object has no attribute 'evaluate'

This is my first keras and hyperas project.

#First keras program
from keras.datasets import mnist
from keras import models
from keras import layers
from keras import optimizers
from keras.utils import to_categorical
from hyperas import optim
from hyperas.distributions import choice
from hyperopt import Trials, STATUS_OK, tpe
#Loading Data
#Train_images has shape (60000,28,28)
#Train_labels has shape (60000)
def data():
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
    train_images = train_images.reshape((60000, 28 * 28))
    train_images = train_images.astype('float32') / 255
    test_images = test_images.reshape((10000, 28 * 28))
    test_images = test_images.astype('float32') / 255
    train_labels = to_categorical(train_labels)
    test_labels = to_categorical(test_labels)
    return train_images,train_labels,test_images,test_labels

#Defining Network and adding Dense Layers
#Compiling Network
def create_model(train_images,train_labels,test_images,test_labels):

    network = models.Sequential()
    network.add(layers.Dense({{choice([256,512,1024])}}, activation='relu', input_shape=(28 * 28,)))
    network.add(layers.Dense(10, activation='softmax'))
    network.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
    network.fit(train_images,
                train_labels,
                validation_split=0.33,
                epochs=5,
                batch_size=128)

    score,acc = network.evaluate(train_images,train_labels,verbose=0)
    print('Test accuracy:',acc)
    out={'loss':-acc,'score':score,'status':STATUS_OK}
    return out

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=create_model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=1,
                                          trials=Trials())
    x_train, y_train, x_test, y_test = data()
    # mnist_model=create_model(x_train,y_train,x_test,y_test)
    print("Evaluation of best performing model:")
    print(best_model.evaluate(x_test, y_test,verbose=0))
    print("Best performing model chosen hyper-parameters:")
    print(best_run)

Here is the full traceback

Traceback (most recent call last):
  File "C:/Users/anubhav/Desktop/Projects/chollet/keras_mnist_dense_hyperas.py", line 51, in <module>
    print(best_model.evaluate(x_test, y_test,verbose=0))
AttributeError: 'NoneType' object has no attribute 'evaluate'

Upvotes: 0

Views: 1275

Answers (2)

shallow_water
shallow_water

Reputation: 131

It looks like you're running experiments in create_model() and then throwing the model away. If you return the models this may solve your problem. Try:

out={'loss':-acc,'score':score,'status':STATUS_OK, 'model': network}

Upvotes: 0

Yunkai Xiao
Yunkai Xiao

Reputation: 301

Okay, seems like youroptim.minimize function is not returning a model.

Looking into the library, I found that best_model by default = None, if you haven't put in valid trials then it would remain that way to the end. I don't have much knowledge above Keras so what hyperas does in Trials() is beyond my knowledge.

Check that function and see what it would output or if it needs any input to generate output etc.

Good luck.

Upvotes: 1

Related Questions