I tried to build my own custom layer in tensorflow/keras
that enforces the layer to be symmetric and what I ended up with is the following:
import tensorflow as tf
from tensorflow.python.framework.ops import enable_eager_execution
enable_eager_execution()
class MyDenseLayer(tf.keras.layers.Layer):
def __init__(self, num_outputs):
super(MyDenseLayer, self).__init__()
self.num_outputs = num_outputs
def build(self, input_shape):
X = tf.random.uniform([int(input_shape[-1]),self.num_outputs],minval=0,maxval=1,dtype=tf.dtypes.float32,)
k = tf.Variable(X, name="kernel")
self.kernel = 0.5 * (k+tf.transpose(k))
def call(self, input):
return tf.matmul(input, self.kernel)
layer = MyDenseLayer(5)
print(layer(tf.ones([3, 5])))
print(layer.trainable_variables)
So far, so good. What I don't understand this: why does the last line
print(layer.trainable_variables)
give me an empty list:
[]
I thought that layer.trainable_variables
would show me what my matrix looks like so that I could check whether it is symmetric or not.