我正在尝试解决Tensorflow中的约束优化问题。
我想将条件theta_r <= min(theta_tf)
,theta_s >= max(theta_tf)
,n > 1
和Ks > 0
添加到损失函数中。您知道如何向损失函数添加此类约束吗?我正在使用L-BFGS优化器。
下面是我的损失函数的样子。
self.loss = tf.reduce_mean(tf.square(self.theta_tf - self.theta_pred)) + \
tf.reduce_mean(tf.square(self.fbd_predict)) + \
tf.reduce_mean(tf.square(self.f_pred)) + \
self.weights_L2 + self.biases_L2 +\
1.0e-7 * (self.theta_r**2 + self.theta_s**2 + self.alpha**2 + self.n**2 + self.K_s**2)
答案 0 :(得分:0)
您可以通过tf.math.minimum
和tf.math.maximum
表示边界。然后,您可以选择将应用于这些损失的自定义损失函数(例如MSE)并将其加起来。
min_theta_tf = tf.math.reduce_min(theta_tf)
max_theta_tf = tf.math.reduce_max(theta_tf)
loss_theta_r = tf.square(
tf.math.maximum(theta_r, min_theta_tf) # bound to 'min_theta_tf'
- min_theta_tf
)
loss_theta_s = tf.square(
tf.math.minimum(theta_s, max_theta_tf) # bound to 'max_theta_tf'
- max_theta_tf
)
loss_n = tf.square(tf.math.minimum(n, 1) - 1)
loss_Ks = tf.square(tf.math.minimum(Ks, 0))
loss = loss_theta_r + loss_theta_s + loss_n + loss_Ks