Keras 简明教程

Keras - Customized Layer

Keras 允许创建我们自己的自定义层。一旦创建一个新层,它就可以在任何模型中毫无限制地使用。让我们在这章中学习如何创建新层。

Keras allows to create our own customized layer. Once a new layer is created, it can be used in any model without any restriction. Let us learn how to create new layer in this chapter.

Keras 提供了一个基础 layer 类,Layer 可以对其进行子类化以创建我们自己的自定义层。让我们创建一个简单的层,它将根据正态分布找到权重,然后在训练期间对输入及其权重的乘积求和进行基本计算。

Keras provides a base layer class, Layer which can sub-classed to create our own customized layer. Let us create a simple layer which will find weight based on normal distribution and then do the basic computation of finding the summation of the product of input and its weight during training.

Step 1: Import the necessary module

首先,让我们导入必要的模块 −

First, let us import the necessary modules −

from keras import backend as K
from keras.layers import Layer

在此,

Here,

  1. backend is used to access the dot function.

  2. Layer is the base class and we will be sub-classing it to create our layer

Step 2: Define a layer class

让我们通过对 Layer class 进行子类化来创建一个新类 MyCustomLayer

Let us create a new class, MyCustomLayer by sub-classing Layer class

class MyCustomLayer(Layer):
   ...

Step 3: Initialize the layer class

让我们如下所示初始化我们的新类 −

Let us initialize our new class as specified below −

def __init__(self, output_dim, **kwargs):
   self.output_dim = output_dim
   super(MyCustomLayer, self).__init__(**kwargs)

在此,

Here,

  1. Line 2 sets the output dimension.

  2. Line 3 calls the base or super layer’s init function.

Step 4: Implement build method

build 是主要方法,其唯一目的是正确构建层。它可以执行与层内部工作相关的任何操作。完成自定义功能后,我们可以调用基础类 build 函数。我们的自定义 build 函数如下 −

build is the main method and its only purpose is to build the layer properly. It can do anything related to the inner working of the layer. Once the custom functionality is done, we can call the base class build function. Our custom build function is as follows −

def build(self, input_shape):
   self.kernel = self.add_weight(name = 'kernel',
      shape = (input_shape[1], self.output_dim),
      initializer = 'normal', trainable = True)
   super(MyCustomLayer, self).build(input_shape)

在此,

Here,

  1. Line 1 defines the build method with one argument, input_shape. Shape of the input data is referred by input_shape.

  2. Line 2 creates the weight corresponding to input shape and set it in the kernel. It is our custom functionality of the layer. It creates the weight using ‘normal’ initializer.

  3. Line 6 calls the base class, build method.

Step 5: Implement call method

call 方法在训练过程中执行层的精确工作。

call method does the exact working of the layer during training process.

我们的自定义 call 方法如下

Our custom call method is as follows

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

在此,

Here,

  1. Line 1 defines the call method with one argument, input_data. input_data is the input data for our layer.

  2. Line 2 return the dot product of the input data, input_data and our layer’s kernel, self.kernel

Step 6: Implement compute_output_shape method

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

在此,

Here,

  1. Line 1 defines compute_output_shape method with one argument input_shape

  2. Line 2 computes the output shape using shape of input data and output dimension set while initializing the layer.

实现 build, callcompute_output_shape ,即完成了创建自定义层。最终的完整代码如下:

Implementing the build, call and compute_output_shape completes the creating a customized layer. The final and complete code is as follows

from keras import backend as K from keras.layers import Layer
class MyCustomLayer(Layer):
   def __init__(self, output_dim, **kwargs):
      self.output_dim = output_dim
      super(MyCustomLayer, self).__init__(**kwargs)
   def build(self, input_shape): self.kernel =
      self.add_weight(name = 'kernel',
      shape = (input_shape[1], self.output_dim),
      initializer = 'normal', trainable = True)
      super(MyCustomLayer, self).build(input_shape) #
      Be sure to call this at the end
   def call(self, input_data): return K.dot(input_data, self.kernel)
   def compute_output_shape(self, input_shape): return (input_shape[0], self.output_dim)

Using our customized layer

我们创建一个简单的模型,使用如下指定得自定义层:

Let us create a simple model using our customized layer as specified below −

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(MyCustomLayer(32, input_shape = (16,)))
model.add(Dense(8, activation = 'softmax')) model.summary()

在此,

Here,

  1. Our MyCustomLayer is added to the model using 32 units and (16,) as input shape

运行应用程序将打印模型摘要,如下所示:

Running the application will print the model summary as below −

Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param
#================================================================
my_custom_layer_1 (MyCustomL (None, 32) 512
_________________________________________________________________
dense_1 (Dense) (None, 8) 264
=================================================================
Total params: 776
Trainable params: 776
Non-trainable params: 0
_________________________________________________________________