question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'Model' object has no attribute 'predict_classes'

See original GitHub issue
  • I’m up-to-date with the latest release:

    pip install -U talos
    
  • I’ve confirmed that my Keras model works outside of Talos.

I have an error when I run scan. I am suspecting that it’s because my model is not Sequential but talos expects it to be Sequential. And it’s because my input is a list of numpy arrays, not a numpy array.

Here is the model code (it works and run until the last line because I saw it printed “Best val_r2_keras score”):

# build and train then return the history and the model
def build_deep_model(X_train, y_train, X_val, y_val, params):
    X_train_keras = wave_df_to_keras_inputs(X_train) # this function converts array of shape (?, 604) to a list of 4 numpy arrays of shape (?,200,1), (?,200,1), (?,200,1), and (?,4)
    X_val_keras = wave_df_to_keras_inputs(X_val)
    input_x = keras.Input(shape=(200,1), name='x')
    input_y = keras.Input(shape=(200,1), name='y')
    input_z = keras.Input(shape=(200,1), name='z')
    input_q = keras.Input(shape=(4,), name='q')
    def compute_wave_features(x, name=None):
        x = keras.layers.Conv1D(32, 5, strides=2, padding='same', activation='relu')(x)
        x = keras.layers.Conv1D(32, 5, strides=2, padding='same', activation='relu')(x)
        x = keras.layers.Conv1D(32, 5, strides=2, padding='same', activation='relu')(x)
        x = keras.layers.Conv1D(32, 5, strides=2, padding='valid', activation='relu')(x)
        x = keras.layers.Flatten()(x)
        x = keras.layers.Dense(64, activation='relu', name=name)(x)
        return x
    feature_x = compute_wave_features(input_x, 'feature_x')
    feature_y = compute_wave_features(input_y, 'feature_y')
    feature_z = compute_wave_features(input_z, 'feature_z')
    features = keras.layers.Concatenate()([feature_x, feature_y, feature_z, input_q])
    features = keras.layers.Dense(32, activation='relu')(features)
    readout = keras.layers.Dense(y_train.shape[1], activation='linear', name='readout')(features)
    deep_model = keras.models.Model(inputs=[input_x, input_y, input_z, input_q], outputs=readout)
    deep_model.compile('adam', loss='mse', metrics=['mae', r2_keras])
    deep_model.summary()

    callbacks = [
        keras.callbacks.EarlyStopping(patience=3)
    ]
    history = deep_model.fit(X_train_keras, y_train, epochs=500, validation_data=[X_val_keras, y_val], callbacks=callbacks, verbose=2)
    best_r2 = np.max(history.history['val_r2_keras'])
    epochs = history.epoch[-1] + 1
    print("Best val_r2_keras score:", best_r2, 'Epochs:', epochs)
    return history, deep_model

Here is the code that produces the error (no params yet):

params = {
    
}
h = ta.Scan(X_train.values, y_train.values, params, build_deep_model, val_split=0.2, dataset_name='em-wave-pos', experiment_no='1')

Here is the output:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
x (InputLayer)                  (None, 200, 1)       0                                            
__________________________________________________________________________________________________
y (InputLayer)                  (None, 200, 1)       0                                            
__________________________________________________________________________________________________
z (InputLayer)                  (None, 200, 1)       0                                            
__________________________________________________________________________________________________
conv1d_13 (Conv1D)              (None, 100, 32)      192         x[0][0]                          
__________________________________________________________________________________________________
conv1d_17 (Conv1D)              (None, 100, 32)      192         y[0][0]                          
__________________________________________________________________________________________________
conv1d_21 (Conv1D)              (None, 100, 32)      192         z[0][0]                          
__________________________________________________________________________________________________
conv1d_14 (Conv1D)              (None, 50, 32)       5152        conv1d_13[0][0]                  
__________________________________________________________________________________________________
conv1d_18 (Conv1D)              (None, 50, 32)       5152        conv1d_17[0][0]                  
__________________________________________________________________________________________________
conv1d_22 (Conv1D)              (None, 50, 32)       5152        conv1d_21[0][0]                  
__________________________________________________________________________________________________
conv1d_15 (Conv1D)              (None, 25, 32)       5152        conv1d_14[0][0]                  
__________________________________________________________________________________________________
conv1d_19 (Conv1D)              (None, 25, 32)       5152        conv1d_18[0][0]                  
__________________________________________________________________________________________________
conv1d_23 (Conv1D)              (None, 25, 32)       5152        conv1d_22[0][0]                  
__________________________________________________________________________________________________
conv1d_16 (Conv1D)              (None, 11, 32)       5152        conv1d_15[0][0]                  
__________________________________________________________________________________________________
conv1d_20 (Conv1D)              (None, 11, 32)       5152        conv1d_19[0][0]                  
__________________________________________________________________________________________________
conv1d_24 (Conv1D)              (None, 11, 32)       5152        conv1d_23[0][0]                  
__________________________________________________________________________________________________
flatten_4 (Flatten)             (None, 352)          0           conv1d_16[0][0]                  
__________________________________________________________________________________________________
flatten_5 (Flatten)             (None, 352)          0           conv1d_20[0][0]                  
__________________________________________________________________________________________________
flatten_6 (Flatten)             (None, 352)          0           conv1d_24[0][0]                  
__________________________________________________________________________________________________
feature_x (Dense)               (None, 64)           22592       flatten_4[0][0]                  
__________________________________________________________________________________________________
feature_y (Dense)               (None, 64)           22592       flatten_5[0][0]                  
__________________________________________________________________________________________________
feature_z (Dense)               (None, 64)           22592       flatten_6[0][0]                  
__________________________________________________________________________________________________
q (InputLayer)                  (None, 4)            0                                            
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 196)          0           feature_x[0][0]                  
                                                                 feature_y[0][0]                  
                                                                 feature_z[0][0]                  
                                                                 q[0][0]                          
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 32)           6304        concatenate_2[0][0]              
__________________________________________________________________________________________________
readout (Dense)                 (None, 3)            99          dense_2[0][0]                    
==================================================================================================
Total params: 121,123
Trainable params: 121,123
Non-trainable params: 0
__________________________________________________________________________________________________
Train on 9379 samples, validate on 2345 samples
Epoch 1/500
 - 3s - loss: 0.0905 - mean_absolute_error: 0.2260 - r2_keras: 0.2083 - val_loss: 0.0542 - val_mean_absolute_error: 0.1868 - val_r2_keras: 0.5264
Epoch 2/500
 - 3s - loss: 0.0552 - mean_absolute_error: 0.1869 - r2_keras: 0.5240 - val_loss: 0.0509 - val_mean_absolute_error: 0.1773 - val_r2_keras: 0.5562
Epoch 3/500
 - 3s - loss: 0.0517 - mean_absolute_error: 0.1783 - r2_keras: 0.5550 - val_loss: 0.0499 - val_mean_absolute_error: 0.1721 - val_r2_keras: 0.5638
Epoch 4/500
 - 3s - loss: 0.0503 - mean_absolute_error: 0.1748 - r2_keras: 0.5664 - val_loss: 0.0509 - val_mean_absolute_error: 0.1757 - val_r2_keras: 0.5543
Epoch 5/500
 - 3s - loss: 0.0495 - mean_absolute_error: 0.1728 - r2_keras: 0.5736 - val_loss: 0.0510 - val_mean_absolute_error: 0.1713 - val_r2_keras: 0.5549
Epoch 6/500
 - 3s - loss: 0.0481 - mean_absolute_error: 0.1692 - r2_keras: 0.5868 - val_loss: 0.0490 - val_mean_absolute_error: 0.1681 - val_r2_keras: 0.5725
Epoch 7/500
 - 3s - loss: 0.0465 - mean_absolute_error: 0.1654 - r2_keras: 0.5999 - val_loss: 0.0528 - val_mean_absolute_error: 0.1785 - val_r2_keras: 0.5370
Epoch 8/500
 - 3s - loss: 0.0450 - mean_absolute_error: 0.1626 - r2_keras: 0.6125 - val_loss: 0.0514 - val_mean_absolute_error: 0.1673 - val_r2_keras: 0.5548
Epoch 9/500
 - 3s - loss: 0.0432 - mean_absolute_error: 0.1578 - r2_keras: 0.6281 - val_loss: 0.0483 - val_mean_absolute_error: 0.1633 - val_r2_keras: 0.5810
Epoch 10/500
 - 3s - loss: 0.0414 - mean_absolute_error: 0.1531 - r2_keras: 0.6436 - val_loss: 0.0507 - val_mean_absolute_error: 0.1640 - val_r2_keras: 0.5581
Epoch 11/500
 - 3s - loss: 0.0382 - mean_absolute_error: 0.1460 - r2_keras: 0.6709 - val_loss: 0.0523 - val_mean_absolute_error: 0.1709 - val_r2_keras: 0.5457
Epoch 12/500
 - 3s - loss: 0.0360 - mean_absolute_error: 0.1410 - r2_keras: 0.6904 - val_loss: 0.0527 - val_mean_absolute_error: 0.1696 - val_r2_keras: 0.5398
Best val_r2_keras score: 0.5809796204190772 Epochs: 12
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-c4eaafcd036c> in <module>()
      2 
      3 }
----> 4 h = ta.Scan(X_train.values, y_train.values, params, build_deep_model, val_split=0.2, dataset_name='em-wave-pos', experiment_no='1')

c:\users\admin\anaconda3\lib\site-packages\talos\scan\Scan.py in __init__(self, x, y, params, model, dataset_name, experiment_no, x_val, y_val, val_split, shuffle, round_limit, grid_downsample, random_method, seed, search_method, reduction_method, reduction_interval, reduction_window, reduction_threshold, reduction_metric, reduce_loss, last_epoch_value, talos_log_name, clear_tf_session, functional_model, disable_progress_bar, print_params, debug)
    164         # input parameters section ends
    165 
--> 166         self._null = self.runtime()
    167 
    168     def runtime(self):

c:\users\admin\anaconda3\lib\site-packages\talos\scan\Scan.py in runtime(self)
    169 
    170         self = scan_prepare(self)
--> 171         self = scan_run(self)

c:\users\admin\anaconda3\lib\site-packages\talos\scan\scan_run.py in scan_run(self)
     19                      disable=self.disable_progress_bar)
     20     while len(self.param_log) != 0:
---> 21         self = scan_round(self)
     22         self.pbar.update(1)
     23     self.pbar.close()

c:\users\admin\anaconda3\lib\site-packages\talos\scan\scan_round.py in scan_round(self)
     45 
     46     _hr_out = run_round_results(self, _hr_out)
---> 47     self._val_score = get_score(self)
     48     write_log(self)
     49     self.result.append(_hr_out)

c:\users\admin\anaconda3\lib\site-packages\talos\metrics\score_model.py in get_score(self)
     36         # all other cases
     37         else:
---> 38             y_pred = self.keras_model.predict_classes(self.x_val)
     39 
     40         return Performance(y_pred, self.y_val, self.shape, self.y_max).result

AttributeError: 'Model' object has no attribute 'predict_classes'

I read a stackoverflow post about this error and they said it’s because predict_classes only exist in Sequential model.

I’m also confused because I’m building a regressor not a classifier, why is talos trying to predict classes?

How do I fix this issue?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mikkokotilacommented, Nov 17, 2018

That will not work because it will just try to install the latest version from pypi. You have to follow:

pip uninstall talos 
pip install git+https://github.com/autonomio/talos@daily-dev  
1reaction
mikkokotilacommented, Nov 17, 2018

In the older version (which I think you are in) you still have to declare that the model is functional through Scan(functional_model=True) but in the latest version (0.4.4) this is no longer needed and you can run both Sequential and Functional models as-is. Can you make sure you have the latest version:

pip uninstall talos 
pip install git+https://github.com/autonomio/talos@daily-dev 

After that you should not have this problem anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError: 'Model' object has no attribute 'predict_classes'
The predict_classes method is only available for the Sequential class (which is the class of your first model) but not for the Model...
Read more >
'Sequential' object has no attribute 'predict_classes ... - GitHub
AttributeError: 'Sequential' object has no attribute 'predict_classes' # ... model.predict_classes is deprecated, So you can try replacing.
Read more >
'Sequential' object has no attribute 'predict_classes' ( Solved )
This attributeError comes when there is no module predict_classes provided by the Keras packages. The other reason could be that you are using...
Read more >
'Sequential' object has no attribute 'predict_classes'
I'm trying to run my model but facing "'Sequential' object has no attribute 'predict_classes' " I have. Name: tensorflow Version: 2.9.1 ...
Read more >
sequential' object has no attribute 'predict_classes' - You.com
The predict_classes () function is actually a method of the Keras Classifier class, not the Sequential class. In order to use it, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found