How to extract weights “from input layer to hidden layer” and “from hidden layer to output layer” with Keras API?

前端 未结 1 376
天命终不由人
天命终不由人 2021-01-21 13:27

I am new to Keras and I am trying to get the weights in Keras. I know how to do it in Tensorflow in Python.

Code:

data = np.array(attributes, \'int64\')
         


        
1条回答
  •  野的像风
    2021-01-21 14:04

    You can access and set the weights or parameters of the model's layers using get_weights and set_weights methods. From Keras documentation:

    layer.get_weights(): returns the weights of the layer as a list of Numpy arrays. layer.set_weights(weights): sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of get_weights).

    Each Keras model has a layers attribute which is the list of all the layers in the model. For example, in the sample model you provided, you can get the weights of the first Dense layer by running:

    model.layers[1].get_weights()
    

    It would return a list of two numpy arrays: the first one is the kernel parameters of the Dense layer the second array is the bias parameters.

    0 讨论(0)
提交回复
热议问题