Keras自定义图层不返回权重,与普通图层不同

时间:2019-03-31 14:43:00

标签: tensorflow deep-learning keras-layer tensorflow-layers

我正在尝试获取图层的权重。当使用keras层并将输入连接到它时,它似乎可以正常工作。 但是,当将其包装到我的自定义层中时,它将不再起作用。那是错误还是我想念的东西?

编辑:注意事项:

我读到可以在自定义层的build()中定义可训练的变量。但是,由于自定义层由密集的keras层(以及稍后可能的更多keras层)组成,因此这些层应该已经定义了可训练变量和weight / bias初始值设定项。 (在TestLayer的 init ()中,我看不到用将在TestLayer的build()中定义的变量覆盖它们的方法。

class TestLayer(layers.Layer):
    def __init__(self):
        super(TestLayer, self).__init__()
        self.test_nn = layers.Dense(3)

    def build(self, input_shape):
        super(TestLayer, self).build(input_shape)


    def call(self, inputs, **kwargs):
        test_out = test_nn(inputs) # which is test_in
        return test_out


test_in = layers.Input((2,))
test_nn = layers.Dense(3)
print(test_nn.get_weights()) # empty, since no connection to the layer
test_out = test_nn(test_in)
print(test_nn.get_weights()) # layer returns weights+biases

testLayer = TestLayer()
features = testLayer(test_in)
print(testLayer.get_weights()) # Problem: still empty, even though connected to input.

2 个答案:

答案 0 :(得分:1)

不幸的是,Keras不支持在其他图层中使用图层。 我过去曾遇到此问题,并打开了一个问题here,但团队向我证实这是故意的。

您可以在自定义图层中定义一个方法,例如:

var mic = new Tone.UserMedia()
var animate = function(){
    requestAnimationFrame(animate);
        console.log(mic.volume);

或子类化Dense层,并使用def dense(X, f_in, f_out): W = self.add_weight(name='kernel', shape=(f_in, f_out)) b = self.add_weight(name='bias', shape=(f_out, )) return K.dot(X, W) + b 的输出。

答案 1 :(得分:1)

documentation说,build()方法应该调用您没有的add_weight()

  

应该调用add_weight(),然后调用上级的build()

如果您要继承layers.Layer,则也无需在类内部定义密集层。 这是您应该如何子类化:

import tensorflow as tf
from tensorflow.keras import layers

class TestLayer(layers.Layer):
    def __init__(self, outshape=3):
        super(TestLayer, self).__init__()
        self.outshape = outshape

    def build(self, input_shape):
        self.kernel = self.add_weight(name='kernel',
                                      shape=(int(input_shape[1]), self.outshape),
                                      trainable=True)

        super(TestLayer, self).build(input_shape)


    def call(self, inputs, **kwargs):
        return tf.matmul(inputs, self.kernel)

test_in = layers.Input((2,))

testLayer = TestLayer()
features = testLayer(test_in)
print(testLayer.get_weights())
#[array([[-0.68516827, -0.01990592,  0.88364804],
#       [-0.459718  ,  0.19161093,  0.39982545]], dtype=float32)]

Here是子类化Layer类的更多示例。

但是,如果您坚持以自己的方式实现它,并且想要使用get_weights(),则必须重写它(在这种情况下,您可以只创建一个没有子类的类):

import tensorflow as tf
from tensorflow.keras import layers

class TestLayer(layers.Layer):
    def __init__(self, outshape=3):
        super(TestLayer, self).__init__()
        self.test_nn = layers.Dense(outshape)
        self.outshape = outshape

    def build(self, input_shape):
        super(TestLayer, self).build(input_shape)

    def call(self, inputs, **kwargs):
        return self.test_nn(inputs)

    def get_weights(self):
        with tf.Session() as sess:
            sess.run([x.initializer for x in self.test_nn.trainable_variables])
            return sess.run(self.test_nn.trainable_variables)

test_in = layers.Input((2,))

testLayer = TestLayer()
features = testLayer(test_in)
print(testLayer.get_weights())
#[array([[ 0.5692867 ,  0.726858  ,  0.37790012],
#       [ 0.2897135 , -0.7677493 , -0.58776844]], dtype=float32), #array([0., 0., 0.], dtype=float32)]