问题描述
谁能帮我解决这个错误信息: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 (将#修改为@)