gpt4 book ai didi

python - 类型错误:在 EarlyStopping keras 中使用 restore_best_weights=True 时,类型 'NoneType' 的对象没有 len()

转载 作者:行者123 更新时间:2023-12-03 21:15:25 25 4
gpt4 key购买 nike

我是将 keras 用于深度学习应用程序的新手。我正在尝试使用预先训练的模型执行二元分类。我在 google colab 中运行代码,其中 tensorflow 版本是 2.2.0-rc2 .以下是我正在使用的模型。

vgg19_basemodel = tf.keras.applications.VGG19(include_top = False, weights='imagenet', input_shape=(IMSIZE,IMSIZE,3))
#vgg19_basemodel.summary()

x = vgg19_basemodel.output

x = tf.keras.layers.Conv2D(16, (3,3), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(2,2)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(32, activation="relu")(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(1, activation="sigmoid")(x)

for layer in vgg19_basemodel.layers:
layer.trainable = False

vgg19_model = tf.keras.Model(vgg19_basemodel.input, x)
vgg19_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=LR), loss='binary_crossentropy', metrics=['accuracy'])

#vgg19_model.summary()

以下是我正在使用的回调。
class myCallBack(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('loss') <= EXLOSS and logs.get('accuracy') >= EXACC and logs.get('val_accuracy') >= VALACC):
print("\nCALLBAKC: TRAINING LOSS {} reached.".format(EXLOSS))
self.model.stop_training = True

ccall = myCallBack()

es = tf.keras.callbacks.EarlyStopping(monitor='loss', mode='min', min_delta=0.01, baseline = 0.01, patience=10, restore_best_weights=True)

我正在使用以下方法训练模型:
d3_vgg19_history = vgg19_model.fit(d3_train_generator, 
epochs=EPOCHS,
validation_data=d3_test_generator,
steps_per_epoch=d3_stepsize_train,
validation_steps=d3_stepsize_test,
callbacks=[ccall, es]
)

自定义回调不会产生任何问题,并且在不提前停止的情况下使用时会完美地停止训练。

但是,如果我设置 restore_best_weights=真在提前停止中,当 时产生以下错误epoch_number == 耐心 .

如果我设置 restore_best_weights=假 ,没有问题发生,训练成功结束。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-f6a9ab9579ae> in <module>()
6 steps_per_epoch=d3_stepsize_train,
7 validation_steps=d3_stepsize_test,
----> 8 callbacks=[ccall, esd3]
9 )

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside `run_distribute_coordinator` already.

/usr/local/lib/python3.6/dist-packages/tensorflow/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_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
811 epoch_logs.update(val_logs)
812
--> 813 callbacks.on_epoch_end(epoch, epoch_logs)
814 if self.stop_training:
815 break

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in on_epoch_end(self, epoch, logs)
363 logs = self._process_logs(logs)
364 for callback in self.callbacks:
--> 365 callback.on_epoch_end(epoch, logs)
366
367 def on_train_batch_begin(self, batch, logs=None):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in on_epoch_end(self, epoch, logs)
1483 if self.verbose > 0:
1484 print('Restoring model weights from the end of the best epoch.')
-> 1485 self.model.set_weights(self.best_weights)
1486
1487 def on_train_end(self, logs=None):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in set_weights(self, weights)
1517 expected_num_weights += 1
1518
-> 1519 if expected_num_weights != len(weights):
1520 raise ValueError(
1521 'You called `set_weights(weights)` on layer "%s" '

TypeError: object of type 'NoneType' has no len()

我已经在其他预训练模型中测试了早期停止,即:vgg16、denset201、resnet、xception、inception 等。但是,当 restore_best_weights 设置为 True 时,EarlyStopping 的问题仍然存在并且会弹出相同的错误。
预先感谢您在这种情况下帮助我。如果需要任何其他信息,请告诉我。

最佳答案

发现了“问题”。
它是 None 是有道理的,因为在我的情况下,它没有找到比基线更好的模型。
我摆脱了“基线 = 1.0”,现在它对我有用。

关于python - 类型错误:在 EarlyStopping keras 中使用 restore_best_weights=True 时,类型 'NoneType' 的对象没有 len(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61176084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com