如何解决未知层:DenseVariational请确保将此对象传递给 `custom_objects` 参数
谁能帮我解决这个错误信息:ValueError: Unknown layer: DenseVariational。请确保将此对象传递给 custom_objects
参数。
当我尝试使用 tf.keras 加载经过训练的贝叶斯神经网络模型时会发生这种情况。它不断返回未知层:DenseVariational。但是训练代码效果很好。有大佬知道怎么解决吗?
我们尝试修改 Densevariational_v2 模块中的 get_config 函数,但它不起作用。感谢并感谢它!!
加载模型的代码:
model = tf.keras.models.load_model('model.h5')
print("Model restored.")
model.summary()
用于训练和保存模型的代码:
bnn = tf.keras.Sequential(layers=[tfpl.DenseVariational(20,make_posterior_fn=posterior,make_prior_fn=prior,kl_weight= 1 / train_size,activation = activation,input_shape = (2,))]+ \
[tfpl.DenseVariational(hidden_layer[i],kl_weight= 1 / train_size,activation = activation) for i in range(1,len(hidden_layer))] + \
[tfpl.DenseVariational(1,activation = tf.keras.activations.softplus)],name="bnn")
losses = []
print("START TRAINING")
for epoch in range(n_epochs):
for batch in range(n_batches):
X_train,X_train_ = get_batch2(stock_path,batch_size) # get batch of theoretical options
training_op(X_train.astype(np.float32),X_train_.astype(np.float32))
epoch_loss = loss(X_test.astype(np.float32),X_test_.astype(np.float32))
losses.append(epoch_loss)
print('Epoch:',epoch,'Loss:',epoch_loss)
bnn.save('model.h5')
print("model saved")
训练代码工作正常,我没有在这里复制整个内容,但它可以将模型保存到 .h5 文件中。只是加载部分,是做不到的。提前致谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。