在 keras 调谐器中得到了 nan,但是当我训练它时它可以工作
我多次培训了我的网络,并且已经得到了一些结果。然后,我发现了有关Keras Tuner的信息,并想找到最好的超参数。但是调谐器的损失总是变成NAN(如果您定期训练,它将不会得到NAN)。我正在使用Mobilenetv3small作为骨干,并希望找到最佳的层数和单元。 这是我的模型构建:
def build_model(hp):
model = keras.Sequential()
model.add(base)
# Tune the number of layers.
if hp.Boolean('globalMax'):
model.add(layers.GlobalMaxPool2D())
model.add(layers.Flatten())
for i in range(hp.Int("num_layers", 1, 3)):
model.add(
layers.Dense(
# Tune number of units separately.
units=hp.Int(f"units_{i}", min_value=3, max_value=12, step=1),
)
)
if hp.Boolean("dropout"):
model.add(layers.Dropout(rate=0.1))
model.add(layers.Dense(3))
model.compile(loss=mae, optimizer='sgd',metrics=[mae])
return model
我正在使用
`tuner = kt.RandomSearch(
hypermodel=build_model,
objective="val_loss",
executions_per_trial=2,
overwrite=True
)`
,这是输出: 到目前为止最好的Val_loss:Nan 总过去的时间:00H 02M 28S 信息:TensorFlow:Oracle触发出口
有什么问题?我已经检查了任何其他优化器(但是它可以完美地使用。
I trained my network several times and I already got some results. Then I found out about the Keras tuner and wanted to find the best hyperparameters with it. but the loss in tuner always becomes nan ( it won't get nan if you train it regularly). I'm using MobileNetv3Small as the backbone and wanted to find optimal layers numbers and units.
here is my model build:
def build_model(hp):
model = keras.Sequential()
model.add(base)
# Tune the number of layers.
if hp.Boolean('globalMax'):
model.add(layers.GlobalMaxPool2D())
model.add(layers.Flatten())
for i in range(hp.Int("num_layers", 1, 3)):
model.add(
layers.Dense(
# Tune number of units separately.
units=hp.Int(f"units_{i}", min_value=3, max_value=12, step=1),
)
)
if hp.Boolean("dropout"):
model.add(layers.Dropout(rate=0.1))
model.add(layers.Dense(3))
model.compile(loss=mae, optimizer='sgd',metrics=[mae])
return model
and I'm using
`tuner = kt.RandomSearch(
hypermodel=build_model,
objective="val_loss",
executions_per_trial=2,
overwrite=True
)`
and this is the output:
Best val_loss So Far: nan
Total elapsed time: 00h 02m 28s
INFO:tensorflow:Oracle triggered exit
what is the problem? I already checked any other optimizer ( however it works with .fit perfectly), tried removing dropout and even normalization
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我终于找到了问题所在。发生这种情况是因为 keras_tuner 只是试图找到一些小批量的验证,在我的情况下,它将是 nan,因为数量几乎是无限的。在尝试更大的批次并更改损失函数之后,它可以摆脱一直是 Nan 的问题,并找到了一些结果。
So I finally found the problem. It happened because
keras_tuner
is just trying to find some validation with a small batch and in my situation, it will be nan because the number is nearly infinite. after trying a bigger batch, and changing the loss function, it could get out of being Nan all the time and found some results.