tensorflow2自定义损失函数
一、总结
一句话总结:
直接定义函数,然后在compile时传给loss即可
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"])
二、tensorflow2自定义损失函数
转自或参考:tensorflow2.x学习笔记十七:自定义网络层、模型以及损失函数
https://blog.csdn.net/qq_39507748/article/details/105256541
一、自定义网络层layer
- 继承
tf.keras.layers.Layer
类 - 使用
tf.keras.layers.Lambda
类
下面这个例子就包含了以上两种形式:
from tensorflow import keras
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)
customized_softplus = keras.layers.Lambda(lambda x : tf.nn.softplus(x))
model = keras.models.Sequential([
CustomizedDenseLayer(30, activation='relu',
input_shape=x_train.shape[1:]),
CustomizedDenseLayer(1),
customized_softplus,
])
二、自定义模型model
- 继承
tf.keras.Model
类
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
self.dropout = tf.keras.layers.Dropout(0.5)
def call(self, inputs, training=False):
x = self.dense1(inputs)
if training:
x = self.dropout(x, training=training)
return self.dense2(x)
model = MyModel()
三、自定义损失函数loss
直接定义函数,然后在compile时传给loss参数即可
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"])