12 Tensorflow2.x自定义layer层

自定义layer层

构建模型分为以下几步:

  1. 导入数据集,将数据集进行分类、归一化等
  2. 构建模型
  3. 模型编译
  4. 模型训练
  5. 绘制曲线图
  6. 在测试集上进行评估

自定义损失函数在第2步中

方法一:使用子类class 方式自定义dense layer

class CustomizedDenseLayer(keras.layers.Layer):
    def __init__(self, units, activation=None, **kwargs):
        self.units = units
        self.activation = keras.layers.Activation(activation)
        super(CustomizedDenseLayer, self).__init__(**kwargs)
        
    def build(self,input_shape):
        '''构建参数 w b'''
        # x * w + b 
        #input_shape:[None,a] * w:[a,b] --> output_shape:[None,b]
        self.kernel = self.add_weight(name='kernel',
                                      shape=(input_shape[1],self.units),
                                      initializer='uniform',
                                      trainable=True)
        self.bias = self.add_weight(name='bias',
                                    shape=(self.units,),
                                    initializer = 'uniform',
                                    trainable = True)
        super(CustomizedDenseLayer,self).build(input_shape)
        
    def call(self, x):
        '''完成正向计算'''
        return self.activation(x @ self.kernel + self.bias)


model = keras.models.Sequential([
    CustomizedDenseLayer(30,
                         activation='relu',
                         input_shape=x_train.shape[1:]),
    CustomizedDenseLayer(1),
])

model.summary()

在这里插入图片描述

方法2: 使用lambda方式自定义 dense layer

# 例: tf.nn.softplus : log(1+e^x)
customized_softplus = keras.layers.Lambda(lambda x : tf.nn.softplus(x))
print(customized_softplus([-10.,-5.,0.,5.,10.]))

'''
customized_softplus -->
keras.layers.Dense(1,activation='softplus')
'''

在这里插入图片描述

完整的代码如下:(以子类方式为例)

'''1. 导入数据集及数据集分类、归一化'''
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
# print(housing.DESCR)
# print(housing.data.shape)
# print(housing.target.shape)

from sklearn.model_selection import train_test_split

x_train_all, x_test, y_train_all, y_test = train_test_split(
    housing.data, housing.target, random_state = 7)
x_train, x_valid, y_train, y_valid = train_test_split(
    x_train_all, y_train_all, random_state = 11)
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)


'''2.构建模型'''
# 自定义损失函数
class CustomizedDenseLayer(keras.layers.Layer):
    def __init__(self, units, activation=None, **kwargs):
        self.units = units
        self.activation = keras.layers.Activation(activation)
        super(CustomizedDenseLayer, self).__init__(**kwargs)
        
    def build(self,input_shape):
        '''构建参数 w b'''
        # x * w + b 
        #input_shape:[None,a] * w:[a,b] --> output_shape:[None,b]
        self.kernel = self.add_weight(name='kernel',
                                      shape=(input_shape[1],self.units),
                                      initializer='uniform',
                                      trainable=True)
        self.bias = self.add_weight(name='bias',
                                    shape=(self.units,),
                                    initializer = 'uniform',
                                    trainable = True)
        super(CustomizedDenseLayer,self).build(input_shape)
        
    def call(self, x):
        '''完成正向计算'''
        return self.activation(x @ self.kernel + self.bias)


model = keras.models.Sequential([
    CustomizedDenseLayer(30,
                         activation='relu',
                         input_shape=x_train.shape[1:]),
    CustomizedDenseLayer(1),
])

'''3.模型编译'''
model.compile(loss='mse', 
              optimizer="sgd",
              metrics=["mean_squared_error"])
callbacks = [keras.callbacks.EarlyStopping(
    patience=5, min_delta=1e-2)]

'''4.模型训练'''
history = model.fit(x_train_scaled, y_train,
                    validation_data = (x_valid_scaled, y_valid),
                    epochs = 100,
                    callbacks = callbacks)

'''5.绘制变化曲线'''
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
    plt.show()
plot_learning_curves(history)

'''6.在测试集上进行评估'''
model.evaluate(x_test_scaled, y_test)

猜你喜欢

转载自blog.csdn.net/qq_44783177/article/details/108350477