How to get weights in tf.layers.dense?

前端 未结 7 710
悲哀的现实
悲哀的现实 2020-12-29 07:25

I wanna draw the weights of tf.layers.dense in tensorboard histogram, but it not show in the parameter, how could I do that?

7条回答
  •  不思量自难忘°
    2020-12-29 07:39

    in TF2 weights will output a list in length 2

    weights_out[0] = kernel weight

    weights_out[1] = bias weight

    the second layer weight (layer[0] is the input layer with no weights) in a model in size: 50 with input size: 784

    inputs = keras.Input(shape=(784,), name="digits")
    x = layers.Dense(50, activation="relu", name="dense_1")(inputs)
    x = layers.Dense(50, activation="relu", name="dense_2")(x)
    outputs = layers.Dense(10, activation="softmax", name="predictions")(x)
    
    model = keras.Model(inputs=inputs, outputs=outputs)
    model.compile(...)
    model.fit(...)
    
    kernel_weight = model.layers[1].weights[0]
    bias_weight = model.layers[1].weights[1]
    all_weight = model.layers[1].weights
    print(len(all_weight))                      #  2
    print(kernel_weight.shape)                  # (784,50)
    print(bias_weight.shape)                    # (50,)
    

提交回复
热议问题