gpt4 book ai didi

python - keras 模型的权重为 nan

转载 作者:行者123 更新时间:2023-12-01 09:06:34 28 4
gpt4 key购买 nike

我使用以下模型进行回归;输入大小为 2,输出大小为 28。

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

model = Sequential()
model.add(Dense(16, input_dim=2, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(28, activation='linear'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error',optimizer=sgd)

在训练中一切顺利,但是当我保存并重新加载模型时;我作为一个楠正在减肥。

from keras.models import model_from_json

model_json = model.to_json()
with open('/models/model_ar.json', "w") as json_file:
json_file.write(model_json)
model.save_weights('/models/model_wt.h5')

json_file = open('/models/model_ar.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
new_model = model_from_json(loaded_model_json)
# load weights into new model
new_model.load_weights('/models/model_wt.h5')

将权重设为“nan”。将所有权重设为 nan 的原因是什么

    new_model.get_weights()
[array([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan]], dtype=float32),
array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=float32),
array([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan]], dtype=float32),
array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=float32),
array([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan],
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan]], dtype=float32),
array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=float32)]

最佳答案

尝试

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True, clipvalue=0.5)

来自 https://www.dlology.com/blog/how-to-deal-with-vanishingexploding-gradients-in-keras/
您还可以尝试 clipnorm=1. 或尝试使用更小的值的参数之一。

这限制了梯度下降的每一步中权重可以改变的程度。当我遇到同样的问题时,它对我有用,我希望它有帮助!

关于python - keras 模型的权重为 nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52000103/

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