Атрибут trainable_variables пользовательского слоя в Keras возвращает пустой список - PullRequest
1 голос
/ 27 апреля 2019

Я попытался создать свой собственный пользовательский слой в tensorflow/keras, который заставляет слой быть симметричным, и в результате я получил следующее:

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)

Пока все хорошо.Что я не понимаю: почему последняя строка

print(layer.trainable_variables)

дает мне пустой список:

[]

Я думал, что layer.trainable_variables покажет мне, как выглядит моя матрицанапример, чтобы я мог проверить, симметричен он или нет.

1 Ответ

0 голосов
/ 27 апреля 2019

Вам нужно добавить переменные, используя add_weight, а затем вызвать метод build(), чтобы создать эту переменную.В качестве альтернативы, вместо непосредственного вызова build() вы можете передать ввод (как вы делаете в своем вопросе), и он будет неявно вызывать метод build().

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):
        def initializer(*args, **kwargs):
            X = tf.random.uniform([int(input_shape[-1]),self.num_outputs],minval=0,maxval=1,dtype=tf.dtypes.float32,)
            kernel = 0.5 * (X+tf.transpose(X))
            return kernel
        self.kernel = self.add_weight(name='kernel',
                                      shape=(input_shape[-1], self.num_outputs),
                                      initializer=initializer,
                                      trainable=True)
        super(MyDenseLayer, self).build(input_shape)

    def call(self, input_):
        return tf.matmul(input_, self.kernel)

layer = MyDenseLayer(5)
layer.build((5, )) # <-- example of input shape
print(layer.trainable_variables)
# [<tf.Variable 'kernel:0' shape=(5, 5) dtype=float32, numpy=
# array([[0.04476559, 0.8396935 , 0.42732996, 0.75126845, 0.7109113 ],
#        [0.8396935 , 0.46617424, 0.71654373, 0.5770991 , 0.38461512],
#        [0.42732996, 0.71654373, 0.75249636, 0.28733748, 0.6064501 ],
#        [0.75126845, 0.5770991 , 0.28733748, 0.9417101 , 0.61572695],
#        [0.7109113 , 0.38461512, 0.6064501 , 0.61572695, 0.6960379 ]],
#       dtype=float32)>]
...