How to inspect learned parameters of the layer in Keras?

Wkniuz48 report abuse

My current project includes training the deep neural network for natural text processing. I want to be able to see the learned parameters of the specific layer of the network after training will be finished. Is it possible to do this? I'm using tf.keras implementation of Keras.

Answers

cwizards report abuse

It is possible and relatively easy. You just need to assign a layer to a variable before model creation. Then pass this variable to the model when composing it. In other words, you pass a variable to the model structure instead of defining the layer in-place. After the model will be trained you can access learned parameters of the layer using the corresponding variable.

Wkniuz48 report abuse

Should I specify something when compiling the model?

cwizards report abuse

No. Here is a very basic example, hope it will clarify things:

hiddendenselayer = tf.keras.layers.Dense(512, activation='relu') output_layer = tf.keras.layers.Dense(1, activation='sigmoid')

model = tf.keras.models.Sequential([ tf.keras.layers.Embedding(), tf.keras.layers.Flatten(), firstdenselayer, output_layer ])

model.compile(loss="binarycrossentropy", optimizer='adam', metrics=['accuracy']) model.fit(trainingdata,epochs=5,verbose=1)

print("Hidden layer weights {}".format(hiddendenselayer.getweights())) print("Output layer weights {}".format(outputlayer .get_weights()))

Wkniuz48 report abuse

I got it. Thanks!

Add Answer

Need support?

Just drop us an email to ... Show more