简体   繁体   中英

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

Note : I have seen this related post but I don't know I can use the answer for my problem.

I try to use Keras for a simple regression. For this I have created a simple policy_network() function, which returns me the model.

def policy_network():
    model = Sequential()
    model.add(MaxPooling2D(pool_size=(4, 4),input_shape=[64,64,3]))
    model.add(Flatten())
    model.add(Dense(1, kernel_initializer='normal', activation='linear'))

    model.compile(loss='mean_squared_error',
                  optimizer=Adam(lr=learning_rate),
                  metrics=['mean_squared_error'])

    return model

I also have defined a global variable policy_network . I use the following assignment

policy_network = policy_network().fit(images, actions,
                  batch_size=256,
                  epochs=10,
                  shuffle=True)

but when I call

action = policy_network.predict(image)

I get the AttributeError: 'History' object has no attribute 'predict'

Keras's fit() does not return the model but it returns a History object that contain per-epoch loss and metrics. The code pattern you are using will simply not work with Keras.

Do it like this:

model = policy_network()
model.fit(images, actions,
          batch_size=256,
          epochs=10,
          shuffle=True)
action = model.predict(image)

You changed policy_network's class from a keras.Model object to History object when you said to Python

policy_network = policy_network().fit(..)

If you want to store History in a variable, store it in another variable:

history = policy_network.fit(..)

You can now use policy_network.predict , the way you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM