微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Imblearn给出“'tuple'没有属性'rank'”错误

如何解决Imblearn给出“'tuple'没有属性'rank'”错误

OS:Ubuntu 20.04.1, Tensorflow:2.2.0, keras:2.3.0-tf, imblearn:0.7.0

我正在尝试在一个极不平衡的数据集(〜42000个样本,2300个阳性)上运行神经网络,我想确保每次运行一批时,正样本和负样本的数量大致相等。我一直在尝试使用imblearn's BalancedBatchGenerator,但是每次运行它时,我都遇到一个元组没有属性“等级””错误。有人遇到过这个问题,并且知道我可以更改以解决此问题吗?

inputSize = train_X.shape[1]

# define the keras model
model_unweighted = tf.keras.models.Sequential()
model_unweighted.add(tf.keras.Input(shape=(inputSize,)))


# Hidden Layers
model_unweighted.add(Dense(units=30,activation='tanh'))
model_unweighted.add(Dense(units=30,activation='tanh'))
model_unweighted.add(Dense(units=41,activation='relu'))
model_unweighted.add(Dense(units=30,activation='relu'))

# Final Layer

model_unweighted.add(Dense(1,activation='sigmoid'))
 
# Compiling and Fitting
model_unweighted.compile(optimizer="Adam",loss='binary_crossentropy',metrics=['accuracy',tf.keras.metrics.Precision(),tf.keras.metrics.Recall(),tf.keras.metrics.AUC()])

training_generator = BalancedBatchGenerator(train_X,train_Y,sampler=RandomUnderSampler(),batch_size=40,random_state=42)


model_unweighted.fit_generator(generator=training_generator,epochs=8,verbose=0)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-ce6d9dee0646> in <module>
     30 # fit the keras model on the dataset
     31 
---> 32 model_unweighted.fit_generator(generator=training_generator,verbose=0)
     33 
     34 #model_unweighted.evaluate(train_X,train_Y)

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/util/deprecation.py in new_func(*args,**kwargs)
    322               'in a future version' if date is None else ('after %s' % date),323               instructions)
--> 324       return func(*args,**kwargs)
    325     return tf_decorator.make_decorator(
    326         func,new_func,'deprecated',~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit_generator(self,generator,steps_per_epoch,epochs,verbose,callbacks,validation_data,validation_steps,validation_freq,class_weight,max_queue_size,workers,use_multiprocessing,shuffle,initial_epoch)
   1463     """
   1464     _keras_api_gauge.get_cell('fit_generator').set(True)
-> 1465     return self.fit(
   1466         generator,1467         steps_per_epoch=steps_per_epoch,~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self,*args,**kwargs)
     64   def _method_wrapper(self,**kwargs):
     65     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 66       return method(self,**kwargs)
     67 
     68     # Running inside `run_distribute_coordinator` already.

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit(self,x,y,batch_size,validation_split,sample_weight,initial_epoch,validation_batch_size,use_multiprocessing)
    800          training_utils.RespectCompiledTrainableState(self):
    801       # Creates a `tf.data.Dataset` and handles batch and epoch iteration.
--> 802       data_handler = data_adapter.DataHandler(
    803           x=x,804           y=y,~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self,model)
   1098 
   1099     adapter_cls = select_data_adapter(x,y)
-> 1100     self._adapter = adapter_cls(
   1101         x,1102         y,sample_weights,model,**kwargs)
    899     self._keras_sequence = x
    900     self._enqueuer = None
--> 901     super(KerasSequenceAdapter,self).__init__(
    902         x,903         shuffle=False,# Shuffle is handed in the _make_callable override.

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self,**kwargs)
    790       return tensor_shape.TensorShape([None for _ in shape.as_list()])
    791 
--> 792     output_shapes = nest.map_structure(_get_dynamic_shape,peek)
    793     output_types = nest.map_structure(lambda t: t.dtype,peek)
    794 

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/util/nest.py in map_structure(func,*structure,**kwargs)
    615 
    616   return pack_sequence_as(
--> 617       structure[0],[func(*x) for x in entries],618       expand_composites=expand_composites)
    619 

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
    615 
    616   return pack_sequence_as(
--> 617       structure[0],618       expand_composites=expand_composites)
    619 

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py in _get_dynamic_shape(t)
    786       shape = t.shape
    787       # UnkNown number of dimensions,`as_list` cannot be called.
--> 788       if shape.rank is None:
    789         return shape
    790       return tensor_shape.TensorShape([None for _ in shape.as_list()])

AttributeError: 'tuple' object has no attribute 'rank'

fit_generator似乎已被弃用,但显然也适合takes generator functions。不幸的是,我使用它们遇到了同样的问题。

inputSize = train_X.shape[1]

# define the keras model
model_unweighted = tf.keras.models.Sequential()
model_unweighted.add(tf.keras.Input(shape=(inputSize,activation='sigmoid'))

#model.add(Dropout(rate=0.25))     

model_unweighted.compile(optimizer="Adam",random_state=42)
# fit the keras model on the dataset

model_unweighted.fit(training_generator,verbose=0)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-19-7371bfbaa230> in <module>
     30 # fit the keras model on the dataset
     31 
---> 32 model_unweighted.fit(training_generator,train_Y)

~/anaconda3/envs/deep_learning/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self,`as_list` cannot be called.
--> 788       if shape.rank is None:
    789         return shape
    790       return tensor_shape.TensorShape([None for _ in shape.as_list()])

AttributeError: 'tuple' object has no attribute 'rank'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。