AttributeError: у объекта 'numpy .float32' нет атрибута '__index__' - PullRequest
0 голосов
/ 31 января 2020

Я пытаюсь обучить модель с помощью простого генератора данных, используя Tensorflow.keras.utils.Sequence, но имеет ошибку атрибута numpy.

from tensorflow.keras.utils import Sequence
class DataGenerator(Sequence):
    def __init__(self, dataset, batch_size=16, dim=(1), shuffle=True):
        'Initialization'
        self.dim = dim
        self.batch_size = batch_size
        self.dataset = dataset
        self.shuffle = shuffle
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return tf.math.ceil(len(self.dataset) / self.batch_size)

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]

        # Find list of IDs
        list_IDs_temp = [self.dataset.index[k] for k in indexes]

        # Generate data
        y = dataset.loc[list_IDs_temp,['rating']].to_numpy()
        X = dataset.loc[list_IDs_temp,['user_id', 'item_id']].to_numpy()
        return (X, y)

    def on_epoch_end(self):
        'Updates indexes after each epoch'
        self.indexes = np.arange(len(dataset))
        if self.shuffle == True:
            np.random.shuffle(self.indexes)

И примерку:

history = model.fit(train_generator,use_multiprocessing=True, steps_per_epoch=1, epochs=10, verbose=0)

сообщение об ошибке:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-c17d5c8aa46c> in <module>()
----> 1 history = model.fit(train_generator,use_multiprocessing=True, steps_per_epoch=1, epochs=10, verbose=0)

5 frames
/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    817         max_queue_size=max_queue_size,
    818         workers=workers,
--> 819         use_multiprocessing=use_multiprocessing)
    820 
    821   def evaluate(self,

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    233           max_queue_size=max_queue_size,
    234           workers=workers,
--> 235           use_multiprocessing=use_multiprocessing)
    236 
    237       total_samples = _get_total_number_of_samples(training_data_adapter)

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/training_v2.py in _process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    591         max_queue_size=max_queue_size,
    592         workers=workers,
--> 593         use_multiprocessing=use_multiprocessing)
    594     val_adapter = None
    595     if validation_data:

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/training_v2.py in _process_inputs(model, mode, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    704       max_queue_size=max_queue_size,
    705       workers=workers,
--> 706       use_multiprocessing=use_multiprocessing)
    707 
    708   return adapter

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, standardize_function, shuffle, workers, use_multiprocessing, max_queue_size, **kwargs)
    941       raise ValueError("`sample_weight` argument is not supported when using "
    942                        "`keras.utils.Sequence` as input.")
--> 943     self._size = len(x)
    944     self._shuffle_sequence = shuffle
    945     super(KerasSequenceAdapter, self).__init__(

/tensorflow-2.1.0/python3.6/tensorflow_core/python/framework/ops.py in __index__(self)
    860 
    861   def __index__(self):
--> 862     return self._numpy().__index__()
    863 
    864   def __bool__(self):

AttributeError: 'numpy.float32' object has no attribute '__index__'

Есть предложения? Спасибо

1 Ответ

0 голосов
/ 01 февраля 2020

Как предложено Моникой, я исправил ошибку, используя math.ceil вместо tf.math.ceil в методе __len__.

...