Python 学习自定义目标函数
我正在为我的tflearn模型创建自定义目标函数。目标函数很复杂,需要我迭代预测和正确的输出,并添加某些不基于索引的部分。我找不到一种方法来处理tensor数据类型 我使用下面的标准列表编写了一个版本Python 学习自定义目标函数,python,tensorflow,tflearn,Python,Tensorflow,Tflearn,我正在为我的tflearn模型创建自定义目标函数。目标函数很复杂,需要我迭代预测和正确的输出,并添加某些不基于索引的部分。我找不到一种方法来处理tensor数据类型 我使用下面的标准列表编写了一个版本 errorBuild = 0 errorCheck = 0 def CustomLoss(y_pred, y_true): for value, index in enumerate(y_true): if y_true[index] == 0: e
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
似乎没有一种方法可以循环遍历张量的各个值。我应该在目标函数中创建一个新的会话并评估张量吗
提前感谢您的帮助您可以将任何新的损失函数添加到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)
理想情况下,您应该将损失矢量化(可能涉及和tf.abs(y_pred[1:]-y_true))。如果不可能的话,我会看看。