2

我正在为我的 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

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

我在这里先向您的帮助表示感谢

4

1 回答 1

0

您可以将任何新的损失函数添加到 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-05-25T12:10:23.520 回答