autonomio / talos

Hyperparameter Experiments with TensorFlow and Keras
https://autonom.io
MIT License
1.62k stars 268 forks source link

KeyError: "None of [...] are in the [columns]" #298

Closed ghost closed 5 years ago

ghost commented 5 years ago

Thanks so much for coming here to raise an issue. Please take a moment to 'check' the below boxes:

If you still have an error, please submit complete trace and a code with:

You can provide the code in pastebin / gist or any other format you like.


I'm trying to optimize only the first node count, but talos seems to return a pandas error. x shape: (6181, 24) y shape: (6181, 1)

p = {
    'first_neuron': [12, 24, 48],
}

Using this model:

def build_and_fit_model(normed_train_data, train_labels, x_val, y_val, params):
    model = keras.Sequential([
        layers.Dense(256, activation=tf.nn.relu, input_shape=[len(train_dataset.keys())]),
        layers.Dense(params['first_neuron'], activation=tf.nn.relu),
        layers.Dense(1)
    ])

    optimizer = tf.keras.optimizers.RMSprop(0.001)

    model.compile(loss='mean_squared_error',
                  optimizer=optimizer,
                  metrics=['mean_absolute_error', 'mean_squared_error'])

    # Saving the model during training
    timestr = time.strftime("%Y%m%d-%H%M%S")
    checkpoint_path = "./models/training_" + filename + "_" + timestr + "/cp-{epoch:04d}.ckpt"
    checkpoint_dir = os.path.dirname(checkpoint_path)

    # Create checkpoint callback
    cp_callback = tf.keras.callbacks.ModelCheckpoint(
        checkpoint_path, verbose=1, save_weights_only=True,
        # Save weights, every 5-epochs.
        period=15)

    # The patience parameter is the amount of epochs to check for improvement
    early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)

    # With early stop
    history = model.fit(normed_train_data, train_labels, epochs=1000,
                        validation_data=[x_val, y_val], verbose=1, callbacks=[cp_callback, early_stop])
    return history, model
t = ta.Scan(x=normed_train_data, y=train_labels, params=p, model=build_and_fit_model, val_split=0.1275)

When trying to run the code it gives the following error:

Traceback (most recent call last):

  File "<ipython-input-30-aaab342cdb2d>", line 86, in <module>
    t = ta.Scan(x=normed_train_data, y=train_labels, params=p, model=build_and_fit_model, val_split=0.1275)

  File "/usr/local/lib/python3.6/dist-packages/talos/scan/Scan.py", line 170, in __init__
    self._null = self.runtime()

  File "/usr/local/lib/python3.6/dist-packages/talos/scan/Scan.py", line 174, in runtime
    self = scan_prepare(self)

  File "/usr/local/lib/python3.6/dist-packages/talos/scan/scan_prepare.py", line 59, in scan_prepare
    self = validation_split(self)

  File "/usr/local/lib/python3.6/dist-packages/talos/utils/validation_split.py", line 21, in validation_split
    random_shuffle(self)

  File "/usr/local/lib/python3.6/dist-packages/talos/utils/validation_split.py", line 65, in random_shuffle
    self.x = self.x[ix]

  File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 2934, in __getitem__
    raise_missing=True)

  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1354, in _convert_to_indexer
    return self._get_listlike_indexer(obj, axis, **kwargs)[1]

  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1161, in _get_listlike_indexer
    raise_missing=raise_missing)

  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1246, in _validate_read_indexer
    key=key, axis=self.obj._get_axis_name(axis)))

KeyError: "None of [Int64Index([1816,   97, 4076, 5584,  801, 3398, 1103, 3790,  673, 2013,\n            ...\n            3667, 1998, 5540, 2843, 2550,  994, 6146, 4547, 3748,  503],\n           dtype='int64', length=6181)] are in the [columns]"

I don't know where the problem lies, so if someone could help me that would be awesome.

mikkokotila commented 5 years ago

Which version of Talos you are on?

import talos
talos.__version__
ghost commented 5 years ago

I'm on 0.4.9

mikkokotila commented 5 years ago

You can overcome this by manually splitting your data, and then passing it explicitly through Scan( ... x_val, y_val ...).

Regarding the issue, in my system I can't replicate it. What I had done is:

generate data in shape of your data

import wrangle
normed_train_data, train_labels = wrangle.utils.create_synth_data(n=6181, features=26)

use the below model

NOTE: as you can see the changes are insubstantial in the sense that the workings of the model is not changed, but the things I changed, threw a non-Talos error before changing it.

The below runs on my system. Can you follow the same steps on your system and see if you can replicate the error you are getting.

import keras
import talos
import tensorflow as tf

import time
import os

def build_and_fit_model(normed_train_data, train_labels, x_val, y_val, params):
    model = keras.Sequential([
        keras.layers.Dense(256, activation=tf.nn.relu, input_shape=[26]),
        keras.layers.Dense(params['first_neuron'], activation=tf.nn.relu),
        keras.layers.Dense(1)
    ])

    model.compile(loss='mean_squared_error',
                  optimizer='RMSprop',
                  metrics=['mean_absolute_error', 'mean_squared_error'])

    # Saving the model during training
    timestr = time.strftime("%Y%m%d-%H%M%S")
    checkpoint_path = 'test' + "_" + timestr + "/cp-{epoch:04d}.ckpt"
    checkpoint_dir = os.path.dirname(checkpoint_path)

    # Create checkpoint callback
    cp_callback = tf.keras.callbacks.ModelCheckpoint(
        checkpoint_path, verbose=1, save_weights_only=True,
        # Save weights, every 5-epochs.
        period=15)

    # The patience parameter is the amount of epochs to check for improvement
    early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)

    # With early stop
    history = model.fit(normed_train_data, train_labels, epochs=1000,
                        validation_data=[x_val, y_val], verbose=1, callbacks=[cp_callback, early_stop])
    return history, model
t = talos.Scan(x=normed_train_data, y=train_labels, params=p, model=build_and_fit_model, val_split=0.1275)
ghost commented 5 years ago

I was passing a dataframe into the model as the data, which talos did not seem to like. The earlier error is now fixed, however because I want to minimize my mean squared error and mean absolute error I was wondering if there is sort of a rounds2low instead of a rounds2high, since this is not what I want. Thank you!

ghost commented 5 years ago

Also, when trying to evaluate the models, I get a AxisError: axis 1 is out of bounds for array of dimension 1 error, even with the wrangler data. Do you have the same problem?

mikkokotila commented 5 years ago

wondering if there is sort of a rounds2low instead of a rounds2high, since this is not what I want. Thank you!

Can you clarify what you mean with this?

Also, when trying to evaluate the models, I get a AxisError: axis 1 is out of bounds for array of dimension 1 error, even with the wrangler data. Do you have the same problem?

Please create a new issue for this.

mikkokotila commented 5 years ago

Closing here as it seems to be resolved and no updates.