如何从 Keras 中训练有素的模型中获得偏差? [英] How can I get biases from a trained model in Keras?

查看:35
本文介绍了如何从 Keras 中训练有素的模型中获得偏差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个简单的神经网络,

I have built a simple neural network,

model = Sequential()
model.add(Dense(20, input_dim=5, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))

我会得到它的权重:

summary = model.summary()
W_Input_Hidden = model.layers[0].get_weights()[0]
W_Output_Hidden = model.layers[1].get_weights()[0]

print(summary)
print('INPUT-HIDDEN LAYER WEIGHTS:')
print(W_Input_Hidden)
print('HIDDEN-OUTPUT LAYER WEIGHTS:')
print(W_Output_Hidden)

但是,通过这种方式,我只能得到没有偏差的权重矩阵 (5x20 , 1x20).如何获得偏差值?

but, in this way, I only get the weights matrices (5x20 , 1x20) without the biases. How can I get the biases values?

推荐答案

很简单,它只是 get_weights() 返回的数组中的第二个元素(对于密集层):

Quite simple, its just the second element in the array returned by get_weights() (For Dense layers):

B_Input_Hidden = model.layers[0].get_weights()[1]
B_Output_Hidden = model.layers[1].get_weights()[1]

这篇关于如何从 Keras 中训练有素的模型中获得偏差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆