我使用TF2(2.3.0)NN来近似求解ODE:y'+3y=0的函数y
我已经定义了cutsom loss类和函数,在该类和函数中,我试图区分单个输出与单个输入,因此方程成立,前提是y_true
为零:
from tensorflow.keras.losses import Loss
import tensorflow as tf
class CustomLossOde(Loss):
def __init__(self, x, model, name='ode_loss'):
super().__init__(name=name)
self.x = x
self.model = model
def call(self, y_true, y_pred):
with tf.GradientTape() as tape:
tape.watch(self.x)
y_p = self.model(self.x)
dy_dx = tape.gradient(y_p, self.x)
loss = tf.math.reduce_mean(tf.square(dy_dx + 3 * y_pred - y_true))
return loss
但运行以下NN:
import tensorflow as tf
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense
from tensorflow.keras import Input
from custom_loss_ode import CustomLossOde
num_samples = 1024
x_train = 4 * (tf.random.uniform((num_samples, )) - 0.5)
y_train = tf.zeros((num_samples, ))
inputs = Input(shape=(1,))
x = Dense(16, 'tanh')(inputs)
x = Dense(8, 'tanh')(x)
x = Dense(4)(x)
y = Dense(1)(x)
model = Model(inputs=inputs, outputs=y)
loss = CustomLossOde(model.input, model)
model.compile(optimizer=Adam(learning_rate=0.01, beta_1=0.9, beta_2=0.99),loss=loss)
model.run_eagerly = True
model.fit(x_train, y_train, batch_size=16, epochs=30)
现在我从第一个时代得到了0的损失,这没有任何意义
我已经从函数中打印了y_true
和y_test
,它们看起来还可以,所以我怀疑问题出在我未能成功打印的渐变中。
通知任何帮助
在这种情况下,使用高级KerasAPI定义自定义损失有点困难。相反,我将从scracth编写训练循环,因为它允许对您所能做的事情进行更细粒度的控制
我从这两位导游那里得到了灵感:
基本上,我使用了多个磁带可以无缝交互的事实。我使用一个来计算损失函数,另一个来计算要由优化器传播的梯度
相关问题 更多 >
编程相关推荐