keras - 设计自定义 keras 损失函数 MSE
问题描述
我想制作一个仅使用输出层中的一项的自定义 MSE 损失函数。
我现在拥有的是这样的:
def new_loss(y_true, y_pred):
index_pred = K.argmax(K.abs(y_pred), axis = -1)
pred = tf.gather(y_true, index_pred, axis = 1)
index_true = K.argmin(K.abs(y_true), axis = -1)
true = tf.gather(y_true, index_true, axis = 1)
return K.mean(K.sqrt(K.square(K.log(pred)-K.log(true))))
但它给出了错误,
An operation has `None` for gradient.
我一直在寻找,但在我的场景中找不到任何工作。
我正在使用 Keras 和 Tensorflow 作为后端。
先感谢您。
编辑:
我试过了
def new_loss(y_true, y_pred):
index_pred = K.argmax(K.abs(y_pred), axis = -1)
pred = tf.gather(y_pred, index_pred, axis = 1)
index_true = K.argmin(K.abs(y_true), axis = -1)
true = tf.gather(y_true, index_true, axis = 1)
return K.mean(K.sqrt(K.square(K.log(pred)-K.log(true))))
它没有给出错误。所以问题不是 K.argmax/K.argmin。
解决方案
推荐阅读
- javascript - 我的 sendtowhatsapp.js 不执行我的表单
- arrays - 使用一个数组值访问另一个数组元素 Matlab
- c++ - 强制转换为 bool 是检查是否存在与键匹配的 unordered_map 值的有效方法吗?C++
- python - 给定一个多嵌套的 Python 函数,如何在一些任意嵌套的函数中一直访问闭包变量?
- leaflet - Leaflet - geoJSON multipolygon - bindPopup 与 bindTooltip
- processor - 现代四核笔记本电脑是 i7-7700 英特尔处理器、SISD 类型还是 SIMD 类型或 MIMD 类型?
- javascript - 肝线summernote没有触发文本区域的变化
- linux - 在不同的终端选项卡中运行由一个 bash startscript 启动的不同 bash 脚本
- xslt - 我试图在 XSLT 中按 2 个不同的元素进行分组。首先是 HOSTLOCID,然后是 HOSTVENDORLOCID
- react-native - 如果状态发生变化,动画将不会开始(演示用的小零食)