自定义Layer层

# -*- coding: utf-8 -*-

from tensorflow import keras
import numpy as np
import pandas
import matplotlib.pyplot as plt

from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
print(housing.DESCR)

print(housing.target)

print(housing.data)

housing.data.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_all中切割出训练数据和校验数据
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)

x_train.shape

layer = keras.layers.Dense(30, activation='relu', input_shape=(None, 5))

layer

layer.variables

layer(np.zeros((10, 5)))

layer.trainable_variables  # 打印出w和b

layer.trainable_weights    # 打印出w和b

# 自定义layer
class CustomizedDenseLayer(keras.layers.Layer):
    def __init__(self, units, activation=None, **kwargs):
        self.units = units
        self.activation = keras.layers.Activation(activation)
        super().__init__(**kwargs)

    def build(self, input_shape):
        """构建所需要的参数"""
        # None, 8  @  w  + b
        # w  * x + 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='zeros',
                       trainable=True)
        super().build(input_shape)

    def call(self, x):
        """完成正向传播"""
        return self.activation(x @ self.kernel + self.bias)

# 通过lambda函数快速自定义层次
# softplus : log(1 + e^x)
customized_softplus = keras.layers.Lambda(lambda x: tf.nn.softplus(x))

customized_softplus

import tensorflow as tf

customized_softplus([-10., -5., 0., 5., 10.])

# 定义网络
model = keras.models.Sequential([
    # input_dim, input_shape一定要是元组
    # input_shape=(None, 8)
    # input_shape=(8,)
    CustomizedDenseLayer(32, input_shape=x_train.shape[1:]),
    customized_softplus,
    CustomizedDenseLayer(1)
])

model.summary()

# 配置
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['mse'])

callbacks = []
history = model.fit(x_train_scaled, y_train,
          validation_data=(x_valid_scaled, y_valid),
          epochs=20)

history.history

# 定义画图的函数
def plot_learning_curves(history):
    pandas.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
    plt.show()
plot_learning_curves(history)

在这里插入图片描述

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值