python - 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)
解决方案
简单地使用乘法逆,所以loss = 1 / -K.log(other_parameters)
突然又是一个最小化问题......不要成为最大化损失的人。
推荐阅读
- python - 如何使用 Python NLP 从数据库表中提取与搜索字符串中的关键字匹配的关键字
- python - 使用 4-d 内核确定 3-d 卷积的权重维度
- arrays - 快速循环后如何获取所有数据数组?
- javascript - 可以安全地忽略“警告:不希望服务器 HTML 包含 a”吗?
- python - 解码后的Encoder-Decoder噪声问题
- javascript - Javascript Array 四个元素,但在控制台中显示并且只能访问三个
- python - 尝试使用词汇表翻译文本时出现词汇表配置错误
- pdfminer - 从其位置获取注释文本 (PDFMiner)
- java - 后缀Trie匹配,匹配操作有问题
- python - 在不可下载的环境中解决“找不到资源停用词”