# -*- 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)
自定义Layer层
于 2023-11-28 20:32:10 首次发布