1. 前言
本篇将简单讲述自定义层
2. 背景
在上篇中,简单实现了自定义Model
,实现鸢尾花的分类任务。但是其实还是没有多大的改变,这里我们将其序列->Input
->自定义Model
核心部简单罗列:
2.1. Sequential
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation='relu'))
model.add(tf.keras.layers.Dense(3, input_shape=(4,), activation='softmax'))
2.2. Input
input_data = tf.keras.Input(shape=(len(x_data[0],)))
h_1 = tf.keras.layers.Dense(4, activation="relu")(input_data)
h_2 = tf.keras.layers.Dense(3, activation="softmax")(h_1)
model = tf.keras.Model(inputs=input_data, outputs=h_2)
2.3. 自定义Model
class MyModel(tf.keras.Model):
def __init__(self, hidden_shape, output_shape):
super(MyModel, self).__init__()
self.layer1 = tf.keras.layers.Dense(hidden_shape, activation='relu')
self.layer2 = tf.keras.layers.Dense(output_shape, activation='softmax')
def call(self, inputs):
h1 = self.layer1(inputs)
out = self.layer2(h1)
return out
model = MyModel(hidden_shape=4, output_shape=3)
以上三种,依次看下来,我们知道没有太多的改变,但明显感觉到的就是其可制定化程度加深了。
本篇将简单记录自定义层。
3. 自定义层
类似的,我们需要继承自父类tf.keras.layers.Layer
。同样的,我们需要复写三个方法:
__init__()
call()
bulid()
在__init__()
方法中,我们需要初始化该层所需要的参数,比如权重矩阵
w
w
w和偏置向量
b
b
b。但是由于初始化需要需要知道输入的张量的形状,故而这里就需要在__init__()
方法中传入其shape
。
在build()
方法中,可以获取到输入张量的形状,故而在这里进行相关变量的初始化操作比较方便和合理。
在build()
方法中,即构建网络结构,调用执行。
【注】我们从上面可以知道__init__()
和build()
中都可以完成对相关变量的初始化操作,区别只在于在build()
中可以直接获取到输入层的shape
而不需要传入。所以在这两个方法中,进行初始化都是可以的。
在这里,还是以鸢尾花分类为例:
3.1 自定义全连接层
class MyLayer(tf.keras.layers.Layer):
def __init__(self, _shape, activation='relu'):
super(MyLayer, self).__init__()
self._shape = _shape
self.activation = activation
def build(self, input_shape):
self.kernel = self.add_weight("kernel", shape=(int(input_shape[-1]), self._shape),initializer='random_normal',trainable=True)
self.bias = self.add_weight(shape=(self._shape,),initializer='random_normal',trainable=True)
def call(self, inputs):
out = tf.matmul(inputs, self.kernel) + self.bias
if self.activation == 'relu':
out = tf.nn.relu(out)
if self.activation == 'softmax':
out = tf.nn.softmax(out)
return out
3.2 调用
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
from sklearn.datasets import load_iris
# 训练集和测试集的划分
from sklearn.model_selection import train_test_split
x_data = load_iris().data # 特征,【花萼长度,花萼宽度,花瓣长度,花瓣宽度】
y_data = load_iris().target # 分类
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.30, random_state=42)
class MyModel(tf.keras.Model):
def __init__(self, hidden_shape, output_shape):
super(MyModel, self).__init__()
self.layer1 = MyLayer(hidden_shape, activation='relu')
self.layer2 = MyLayer(output_shape, activation='softmax')
def call(self, inputs):
h1 = self.layer1(inputs)
out = self.layer2(h1)
return out
model = MyModel(4, 3)
# 调用一次build函数
model.build(input_shape=(None, 4))
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=300)
for key in history.history.keys():
plt.plot(history.epoch, history.history[key])