属性错误:';numpy.float32';对象没有属性'__指数';
我尝试使用属性错误:';numpy.float32';对象没有属性'__指数';,numpy,tensorflow,keras,deep-learning,data-fitting,Numpy,Tensorflow,Keras,Deep Learning,Data Fitting,我尝试使用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'
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__'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1历史=model.fit(训练生成器,使用多处理=True,每个历元的步骤=1,历元=10,详细=0)
5帧
/tensorflow-2.1.0/python3.6/tensorflow\u core/python/keras/engine/training.py in fit(self、x、y、批处理大小、历元、冗余、回调、验证拆分、验证数据、洗牌、类权重、样本权重、初始历元、每个历元的步数、验证步骤、验证频率、最大队列大小、工作者、使用多处理、**kwargs)
817最大队列大小=最大队列大小,
818名工人=工人,
-->819使用\多处理=使用\多处理)
820
821 def评估(自我,
/tensorflow-2.1.0/python3.6/tensorflow\u core/python/keras/engine/training\u v2.py适合(self、model、x、y、批大小、epoch、verbose、回调、验证拆分、验证数据、混洗、类权重、样本权重、初始历元、每个历元的步骤、验证步骤、验证频率、最大队列大小、工作人员、使用多处理、**kwargs)
233最大队列大小=最大队列大小,
234名工人=工人,
-->235使用多处理=使用多处理)
236
237总样本数=\u获取\u总样本数\u(训练\u数据\u适配器)
/tensorflow-2.1.0/python3.6/tensorflow\u core/python/keras/engine/training\u v2.py进程内\u训练\u输入(模型、x、y、批量大小、历元、样本权重、类权重、每个历元的步数、验证拆分、验证数据、验证步骤、洗牌、分布策略、最大队列大小、工人、使用多处理)
591最大队列大小=最大队列大小,
592名工人=工人,
-->593使用多处理=使用多处理)
594 val_适配器=无
595如果验证数据:
/tensorflow-2.1.0/python3.6/tensorflow\u core/python/keras/engine/training\u v2.py进程内输入(模型、模式、x、y、批量大小、时代、样本权重、类权重、无序排列、步骤、分布策略、最大队列大小、工人、使用多进程)
704最大队列大小=最大队列大小,
705名工人=工人,
-->706使用多处理=使用多处理)
707
708返回适配器
/tensorflow-2.1.0/python3.6/tensorflow\u core/python/keras/engine/data\u adapter.py in\uuuuuuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
941 raise VALUE ERROR(“'sample\u weight'参数在使用时不受支持”
942“`keras.utils.Sequence`作为输入。”)
-->943自身尺寸=透镜(x)
944 self.\u shuffle\u sequence=shuffle
945超级(Kerasequenceadapter,self)。\u初始化__(
/tensorflow-2.1.0/python3.6/tensorflow\u core/python/framework/ops.py in\uuuuuuuuuuuuuuu索引(self)
860
861定义索引(自):
-->862返回self.\u numpy().\u索引
863
864 def_uuubool_uuu(自我):
AttributeError:'numpy.float32'对象没有属性'\uu索引''
有什么建议吗?谢谢正如Monica建议的那样,我用
math.ceil
而不是中的tf.math.ceil
方法修复了错误。请包含完整的回溯,而不是它的精简版本。添加了完整的。谢谢!那看起来很可疑。tf.math.ceil
返回一个张量,甚至不是整数张量。它与示例类似。但您可能是对的,我在导入数学后看到了差异