【问题标题】:Tflearn Custom Objective functionTflearn 自定义目标函数
【发布时间】:2017-02-05 05:05:58
【问题描述】:

我正在为我的 tflearn 模型创建一个自定义目标函数。目标函数很复杂,需要我迭代预测和正确的输出,并添加不基于索引的某些部分。我找不到让它与张量数据类型一起工作的方法。

我使用下面的标准列表编写了一个版本。

errorBuild = 0
errorCheck = 0
def CustomLoss(y_pred, y_true):
    for value, index in enumerate(y_true):
        if y_true[index] == 0:
            errorBuild += y_pred[index]
        else:
            errorBuild += y_pred[index] - y_true[index]
            errorCheck += math.abs(errorBuild)

    return errorCheck

似乎没有办法循环遍历张量的各个值。我应该在目标函数中创建一个新会话并评估张量吗?

提前感谢您的帮助

【问题讨论】:

  • 理想情况下,您应该对损失进行矢量化处理(可能涉及cumsum 和 tf.abs(y_pred[1:] - y_true))。如果这不可能,我会查看TensorFlow looping constructs

标签: python tensorflow tflearn


【解决方案1】:

您可以将任何新的损失函数添加到 tflearn/objectives.py 文件 (https://github.com/tflearn/tflearn/blob/master/tflearn/objectives.py)。要使用它,您只需要在回归层中调用它的名称即可。

def my_own_Loss(y_pred, y_true):
    for value, index in enumerate(y_true):
        if y_true[index] == 0:
            errorBuild += y_pred[index]
        else:
            errorBuild += y_pred[index] - y_true[index]
            errorCheck += math.abs(errorBuild)

    return errorCheck

然后调用它:

net = tflearn.regression(net, optimizer='momentum',
                         loss='my_own_Loss',
                         learning_rate=0.1)

【讨论】:

    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多