博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Keras(八)实战自定义损失函数、DenseLayer
阅读量:4202 次
发布时间:2019-05-26

本文共 8779 字,大约阅读时间需要 29 分钟。

本文将介绍如下内容:

  • 自定义损失函数
  • 自定义DenseLayer

一,自定义损失函数

1,在TF中实现自定义损失函数

在TF中实现的demo如下:

import tensorflow as tf import numpy as np y_pred = np.array([1,4,3,2,6,5,9])y_true = np.array([1,4,3,2,1,4,9])print(tf.reduce_mean(tf.square(y_pred - y_true)))# ---output------tf.Tensor(3, shape=(), dtype=int64)

结合之前的实例如下:

def customized_mse(y_true, y_pred):    return tf.reduce_mean(tf.square(y_pred - y_true))model = keras.models.Sequential([    keras.layers.Dense(30, activation='relu',                       input_shape=x_train.shape[1:]),    keras.layers.Dense(1),])model.summary()model.compile(loss=customized_mse, optimizer="sgd",              metrics=["mean_squared_error"])callbacks = [keras.callbacks.EarlyStopping(    patience=5, min_delta=1e-2)]
2,总结代码如下:
#!/usr/bin/env python3# -*- coding: utf-8 -*-import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npimport sklearnimport pandas as pdimport osimport sysimport timeimport tensorflow as tffrom tensorflow import keras# 1,打印使用的python库的版本信息print(tf.__version__)print(sys.version_info)for module in mpl, np, pd, sklearn, tf, keras:    print(module.__name__, module.__version__)    # 2,下载并使用sklearn中的“fetch_california_housing”数据集from sklearn.datasets import fetch_california_housinghousing = fetch_california_housing()print(housing.DESCR)print(housing.data.shape)print(housing.target.shape)# 3,拆分数据集中的数据为 训练数据、验证数据、测试数据from sklearn.model_selection import train_test_splitx_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)# 4,在将数据带入到模型之前,先进行预处理-训练、验证、测试数据标准化from sklearn.preprocessing import StandardScalerscaler = StandardScaler()x_train_scaled = scaler.fit_transform(x_train)x_valid_scaled = scaler.transform(x_valid)x_test_scaled = scaler.transform(x_test)# 5,构建模型、模型层级图、编译模型(添加损失函数、优化器)、添加回调函数def customized_mse(y_true, y_pred):    return tf.reduce_mean(tf.square(y_pred - y_true))model = keras.models.Sequential([    keras.layers.Dense(30, activation='relu',                       input_shape=x_train.shape[1:]),    keras.layers.Dense(1),])model.summary()model.compile(loss=customized_mse, optimizer="sgd",              metrics=["mean_squared_error"])callbacks = [keras.callbacks.EarlyStopping(    patience=5, min_delta=1e-2)]# 6,训练构建的模型history = model.fit(x_train_scaled, y_train,                    validation_data = (x_valid_scaled, y_valid),                    epochs = 100,                    callbacks = callbacks,                    verbose=0)# 7,得到训练曲线图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)# 8,调用估计器-估计测试数据集print(model.evaluate(x_test_scaled, y_test, verbose=0))#---output------_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================dense_591 (Dense)            (None, 30)                270       _________________________________________________________________dense_592 (Dense)            (None, 1)                 31        =================================================================Total params: 301Trainable params: 301Non-trainable params: 0_________________________________________________________________[0.3701427737871806, 0.3701428]

二,自定义DenseLayer

1,对于tf.keras.layers.Dense的理解
layer = tf.keras.layers.Dense(100)layer = tf.keras.layers.Dense(100, input_shape=(None, 5))temp = layer(tf.zeros([10, 5]))print(temp.shape)# ---output-----(10, 100)

a)这里介绍tf.keras.layers.Dense的两个属性:

tf.keras.layers中的variables和trainable_variables

variables和trainable_variables的数值如下:

print(layer.variables)print(layer.trainable_variables)

b)tf.keras.layers的计算公式如下:

x * w + b

w 等同于"kernel";b等同于"bias"

c)如果想了解其他的参数详情,可以参看如下:

help(layer)
2,在TF中实现自定义DenseLayer
# 5,自定义激活函数# 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.]))# 6,自定义DenseLayer函数# customized 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):        """构建所需要的参数"""        # 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 = 'zeros',                                    trainable = True)        super(CustomizedDenseLayer, self).build(input_shape)        def call(self, x):        """完成正向计算"""        return self.activation(x @ self.kernel + self.bias)
3,总结代码如下:
#!/usr/bin/env python3# -*- coding: utf-8 -*-import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npimport sklearnimport pandas as pdimport osimport sysimport timeimport tensorflow as tffrom tensorflow import keras# 1,打印使用的python库的版本信息print(tf.__version__)print(sys.version_info)for module in mpl, np, pd, sklearn, tf, keras:    print(module.__name__, module.__version__)    # 2,下载并使用sklearn中的“fetch_california_housing”数据集# tf.keras.layers传入,输出的参数from sklearn.datasets import fetch_california_housinghousing = fetch_california_housing()print(housing.DESCR)print(housing.data.shape)print(housing.target.shape)# 3,拆分数据集中的数据为 训练数据、验证数据、测试数据from sklearn.model_selection import train_test_splitx_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)# 4,在将数据带入到模型之前,先进行预处理-训练、验证、测试数据标准化from sklearn.preprocessing import StandardScalerscaler = StandardScaler()x_train_scaled = scaler.fit_transform(x_train)x_valid_scaled = scaler.transform(x_valid)x_test_scaled = scaler.transform(x_test)# 5,自定义激活函数# 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.]))# 6,自定义DenseLayer函数# customized 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):        """构建所需要的参数"""        # 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 = 'zeros',                                    trainable = True)        super(CustomizedDenseLayer, self).build(input_shape)        def call(self, x):        """完成正向计算"""        return self.activation(x @ self.kernel + self.bias)# 7,构建模型、自定义模型层级、自定义激活函数、编译模型(添加损失函数、优化器)、添加回调函数model = keras.models.Sequential([    CustomizedDenseLayer(30, activation='relu',                         input_shape=x_train.shape[1:]),    CustomizedDenseLayer(1),    customized_softplus,    # keras.layers.Dense(1, activation="softplus"),    # keras.layers.Dense(1), keras.layers.Activation('softplus'),])model.summary()model.compile(loss="mean_squared_error", optimizer="sgd")callbacks = [keras.callbacks.EarlyStopping(    patience=5, min_delta=1e-2)]# 8,训练构建的模型history = model.fit(x_train_scaled, y_train,                    validation_data = (x_valid_scaled, y_valid),                    epochs = 100,                    callbacks = callbacks)# 9,得到训练曲线图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)# 10,调用估计器-估计测试数据集model.evaluate(x_test_scaled, y_test, verbose=0)# ---output------0.38935184751370155

转载地址:http://kvili.baihongyu.com/

你可能感兴趣的文章
强大的jQuery焦点图无缝滚动走马灯特效插件cxScroll
查看>>
Yii2.0 数据库查询
查看>>
yii2 db 操作
查看>>
mongodb group 有条件的过滤组合个数。
查看>>
yii2 用命令行操作web下的controller
查看>>
yii2 console的使用
查看>>
关于mongodb的 数组分组 array group
查看>>
MongoDB新的数据统计框架介绍
查看>>
mongodb fulltextsearch 关于语言的设置选项
查看>>
mongodb 增加全文检索索引
查看>>
symfony
查看>>
yourls 短连接 安装
查看>>
yii2 php namespace 引入第三方非namespace库文件时候,报错:Class not found 的解决
查看>>
softlayer 端口开放
查看>>
操作1:mongodb安装
查看>>
操作2:mongodb使用语法
查看>>
如何给分类增加一个属性(后台)
查看>>
linux设置环境变量 临时设置 和 永久设置
查看>>
检查网站在世界各地的打开速度
查看>>
jquery 向上(顶部),向下(底部)滑动
查看>>