【发布时间】:2019-11-09 09:52:53
【问题描述】:
我正在尝试为我的深度学习模型创建自定义损失函数,但遇到了错误。
我将在这里给出一个我不想使用的代码示例,但如果我了解如何使这个小损失函数工作,那么我想我将能够使我的长期损失函数工作.所以我非常想寻求帮助以使下一个功能正常工作,就是这样。
model.compile(optimizer='rmsprop',loss=try_loss(pic_try), metrics=
['accuracy'])
def try_loss(pic):
def try_2_loss(y_true,y_pred):
return tf.py_function(func=try_3_loss,inp=[y_pred,pic], Tout=tf.float32)
return try_2_loss
def try_3_loss(y_pred,pic):
return tf.reduce_mean(pic)
现在我想知道以下内容: 1. 我在 model.compile 行中输入的图片是否需要是张量?它可以是一个numpy数组吗? 2.在我的try_3_loss函数中,我可以将tf.reduce_mean替换为np.mean吗? 3.在我的try_3_loss函数中,是否可以对y_pred使用普通的numpy命令,比如np.mean(y_pred)?
我主要是想使用尽可能多的 numpy 命令。
我尝试使用各种东西,我尝试让我的图片成为一个 numpy 数组,我尝试在我的 try_3_loss 函数中使用 np.mean (pic),我尝试让我的图片成为张量对象,然后在我的 try_3_project 中使用 tf.reduce_mean 并且我尝试在运行 model.compile 行之前执行 sess.run(pic) 并且在上述所有情况下我都收到以下错误:
TypeError Traceback (most recent call
last)
<ipython-input-75-ff45de7120bc> in <module>()
----> 1 model.compile(optimizer='rmsprop',loss=try_loss(pic_try),
metrics=['accuracy'])
1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in
compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode,
weighted_metrics, target_tensors, **kwargs)
340 with K.name_scope(self.output_names[i] +
'_loss'):
341 output_loss = weighted_loss(y_true, y_pred,
--> 342 sample_weight,
mask)
343 if len(self.outputs) > 1:
344 self.metrics_tensors.append(output_loss)
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in
weighted(y_true, y_pred, weights, mask)
418 weight_ndim = K.ndim(weights)
419 score_array = K.mean(score_array,
--> 420 axis=list(range(weight_ndim,
ndim)))
421 score_array *= weights
422 score_array /= K.mean(K.cast(K.not_equal(weights, 0),
K.floatx()))
TypeError: 'NoneType' object cannot be interpreted as an integer
【问题讨论】:
标签: deep-learning customization loss-function