编写自己的图层

时间:2017-04-13 00:06:01

标签: tensorflow keras keras-layer

我对keras很新。 我正在尝试使用Keras.io

在Keras中编写(使用Functional API)自定义图层
class MyLayer(Layer):
    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(shape=(input_shape[1], self.output_dim),
                                      initializer='uniform',
                                      trainable=True)
        super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!

    def call(self, x):
        return K.dot(x, self.kernel)

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim)

呼叫:

sourcedistribution = Mylayer(dropout2_target)

错误回溯如下:

File "/root/PycharmProjects/chNET/modelBuild.py", line 77, in 
model = create_network([100, 100, 3])
File "/root/PycharmProjects/chNET/modelBuild.py", line 53, in create_network
sourcedistribution = Mylayer(dropout2_source)
NameError: global name 'Mylayer' is not defined

请帮助理解此错误。

1 个答案:

答案 0 :(得分:1)

我认为唯一的问题是您已定义MyLayer,但您称之为Mylayer