[英]Custom loss function Tensorflow 2 [Keras symbolic inputs/outputs do not implement]
我正在尝试使用自定义损失 function 并从简单的 MSE 开始。 不要关注oscillator
function,它只需要用于创建数据。
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Input, Dense
from keras.models import Model
import tensorflow as tf
def oscillator(d_, w0_, x):
assert d_ < w0_
w = np.sqrt(w0_**2 - d_**2)
phi = np.arctan(-d_/w)
A = 1/(2*np.cos(phi))
cos = np.cos(phi+w*x)
sin = np.sin(phi+w*x)
exp = np.exp(-d_*x)
return exp*2*A*cos
# PARAMETERS:
np.random.seed(5)
N = 20
epochs = 2000
d, w0 = 2, 20
nn_dim = 64
# DATA:
x = np.linspace(0,1,100)
y = oscillator(d,w0,x)
x_train = np.sort(np.random.uniform(0,0.35,N)[:,np.newaxis], axis=0)
y_train = oscillator(d,w0,x_train)
tf_y = tf.Variable(y_train,dtype=tf.float32)
# LAYERS:
input_layer = Input(shape=(1,))
Layer_1 = Dense(nn_dim, activation="tanh")(input_layer)
Layer_2 = Dense(nn_dim, activation="tanh")(Layer_1)
output_layer = Dense(1)(Layer_2)
model = Model(inputs=input_layer, outputs=output_layer)
loss_func = tf.reduce_mean(tf.math.squared_difference(tf_y,output_layer))
model.compile(optimizer='adam', loss=loss_func, metrics=['mse'])
md = model.fit(x_train,y_train,epochs=epochs,verbose=1)
y_pred = model.predict(x[:,np.newaxis])
# PLOTTING:
fig = plt.figure()
plt.plot(md.history['loss'], label='training')
plt.legend()
plt.figure()
plt.plot(x,y,label="Exact solution")
plt.scatter(x_train,y_train,label="Data",color="orange")
plt.plot(x,y_pred,label="Prediction",linestyle="--",color="red")
plt.legend()
plt.show()
上面的代码产生以下错误: TypeError: Keras 符号输入/输出不实现__len__
。 You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. 如果您尝试直接断言符号输入/输出,也会引发此错误。 进程以退出代码 1 结束
问题在于loss_func = tf.reduce_mean(tf.math.squared_difference(tf_y,output_layer))
。 我认为这是因为tf_y
和output_layer
的不同维度。 任何想法如何使用output_layer
和y
手动计算 MSE?
我个人从未见过这样定义的损失(而且我几乎不认为它可能会起作用),您通常想要创建一个 function:
def loss_func(tf_y, output_layer):
return tf.reduce_mean(tf.math.squared_difference(tf_y,output_layer))
从文档中:
compile 的 loss 参数:可以是字符串(损失函数的名称),也可以是 tf.keras.losses.Loss 实例。 参见 tf.keras.losses。 损失 function 是具有签名 loss = fn(y_true,y_pred) 的任何可调用损失,其中 y_true 是真实值,y_pred 是模型的预测
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.