首页 > 解决方案 > Keras:使用 K.gradients 进行梯度上升

问题描述

我目前正在尝试训练 Keras 模型以最大化其损失函数。由于内置优化器选择最小化损失,我想知道是否有可能使用 Keras 执行梯度上升。

我尝试使用负损失函数,但没有成功。

关于如何解决这个问题的任何想法?

当前尝试:

loss = -K.log(other_parameter_input)

grad = K.gradients(loss, self.model.trainable_weights)
self.get_gradients = K.function(inputs=[self.price_vector_input, 
                                  self.prev_weight_input, self.cash_bias_input, mu, y,
                                  self.model.output], outputs=grad)

updates = [self.learning_rate * g for g in grads]

modelWeights = self.model.get_weights()
updateWeights = [np.add(wT, uT) for (wT, uT) in zip(modelWeights, updates)]
self.model.set_weights(updateWeights)

标签: pythontensorflowkeras

解决方案


简单地使用乘法逆,所以loss = 1 / -K.log(other_parameters)突然又是一个最小化问题......不要成为最大化损失的人。


推荐阅读