Keras - 我怎样才能从受过训练的模型中获得偏见?

时间:2017-02-05 14:24:44

标签: machine-learning keras

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

  var imageswipe = UIImageView()
  imageswipe.backgroundColor = UIColor.red
  imageswipe.frame = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height / 4)
  imageswipe.center = CGPoint(x: self.size.width / 2, y: self.size.height / 2)

  var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes"))
  rightSwipe.direction = .right

  imageswipe.isUserInteractionEnabled = true
  imageswipe.addGestureRecognizer(rightSwipe)

  view.addSubview(imageswipe)

我会通过以下方式获得权重:

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

但是,通过这种方式,我只得到没有偏差的权重矩阵(5x20,1x20)。我怎样才能得到偏差值?

3 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:1)

这是一个完整的工作示例(由TensorFlow 2和Keras实现)。

import tensorflow as tf
import numpy as np


def get_model():
    inp = tf.keras.layers.Input(shape=(1,))
    # Use the parameter bias_initializer='random_uniform'
    # in case you want the initial biases different than zero.
    x = tf.keras.layers.Dense(8)(inp)
    out = tf.keras.layers.Dense(1)(x)
    model = tf.keras.models.Model(inputs=inp, outputs=out)
    return model


def main():
    model = get_model()
    model.compile(loss="mse")

    weights = model.layers[1].get_weights()[0]
    biases = model.layers[1].get_weights()[1]

    print("initial weights =", weights)
    print("initial biases =", biases)

    X = np.random.randint(-10, 11, size=(1000, 1))
    y = np.random.randint(0, 2, size=(1000, 1))

    model.fit(X, y)

    weights = model.layers[1].get_weights()[0]
    biases = model.layers[1].get_weights()[1]

    print("learned weights =", weights)

    # Biases are similar because they are all initialized with zeros (by default).
    print("learned biases =", biases)


if __name__ == '__main__':
    main()

答案 2 :(得分:0)

您可以使用以下代码查看和输出偏差和权重:

for layer in model.layers:
    g=layer.get_config()
    h=layer.get_weights()
    print (g)
    print (h)

如果您要从验证数据集中寻找权重和偏差,则需要对数据集中的每个向量执行 model.predict

   for i in range(len(valData)):
        ValResults = model.predict(valData[i])
        B_Input_Hidden = model.layers[0].get_weights()[1]
        B_Output_Hidden = model.layers[1].get_weights()[1]