Paste: z
Author: | z |
Mode: | factor |
Date: | Thu, 20 Jul 2023 00:32:10 |
Plain Text |
import tensorflow as tf
def smape_loss(y_true, y_pred):
epsilon = 0.1 # 防止分母为0
return 100 * tf.reduce_mean(2 * tf.abs(y_pred - y_true) / (tf.abs(y_pred) + tf.abs(y_true) + epsilon))
# 创建线性回归模型
model = LinearRegression()
# 编译模型,使用自定义的SMAPE损失函数
model.compile(optimizer='sgd', loss=smape_loss)
# 训练模型
model.fit(X_train_poly, y_train, epochs=100, batch_size=32, validation_split=0.1)
def smape_metric(y_true, y_pred):
epsilon = 0.1 # 防止分母为0
return 100 * tf.reduce_mean(2 * tf.abs(y_pred - y_true) / (tf.abs(y_pred) + tf.abs(y_true) + epsilon))
# 创建线性回归模型
model = LinearRegression()
# 编译模型,使用自定义的SMAPE损失函数和指标函数
model.compile(optimizer='sgd', loss=smape_loss, metrics=[smape_metric])
# 训练模型
model.fit(X_train_poly, y_train, epochs=100, batch_size=32, validation_split=0.1)
New Annotation