2
votes

I have written a custom keras layer and basically set up a kernel that looks like this as an example:

[[w1, 0,  0],
 [w2, w3, 0],
 [0,  w4, w5]]

where w1,...w5 are trainable weights and the zero entries are not trainable.

Now, I want to confirm if everything is working correctly (i.e. whether the kernel still has the zero entries where it should have it after training). I could not find out, how to print the kernel after training. The .get_weights() method just gets me the weights, but I want to print the kernel explictly.

Thank you in advance

1
The weights are the kernel, so just print the weights. - Dr. Snoopy
Yes, but I want to print the entire kernel, not just the weights. In this way I can be on the safe side, that the zero entries stay at zero - RolleRugu
Well that depends then on how you defined the kernel in your code, include it. - Dr. Snoopy
Thank you, I luckily found an answer somewhere else and posted it - RolleRugu

1 Answers

2
votes

So, I was lucky and found an answer in a not-related post. The answer is quite general:

For a tensor, defined as a class member of the custom layer, you need to call its evaluation method with the correct session. That is

import keras.backend as K

# Train your model...

sess = K.get_session()
print(model.get_layer("name_of_your_layer").your_tensor.eval(session=sess))

As an example, to print the kernel of a dense layer after training this is

import numpy as np
from keras.layers import Input, Dense
from keras.models import Model
import keras.backend as K

x = np.random.rand(10,3)

layer_1 = Input(shape=(x.shape[1],))
layer_2 = Dense(units=x.shape[1])(layer_1)

model = Model(inputs=layer_1, outputs=layer_2)
model.compile(optimizer="Adam", loss="MSE")
model.fit(x, x, epochs=5)

sess = K.get_session()
print(model.get_layer("dense_1").kernel.eval(session=sess))