python - 如何在 Keras 中创建随时间变化的损失函数

标签 python machine-learning keras

我想创建一个自定义损失函数,它的权重项会根据我所处的时期进行更新。

例如: 假设我有一个具有 beta 权重的损失函数,其中 beta 在前 20 个时期增加...

def custom_loss(x, x_pred): 
    loss1 = objectives.binary_crossentropy(x, x_pred)
    loss2 = objectives.mse(x, x_pred)
    return (beta*current_epoch/20) * loss1 + loss2

我怎样才能将这样的东西实现到 keras 损失函数中?

最佳答案

查看他们的文档,他们提到您可以使用 theano/Tf 符号函数为每个数据点返回一个标量。 所以你可以做这样的事情

loss = tf.contrib.losses.softmax_cross_entropy(x, x_pred) * 
       (beta * current_epoch / 20 ) +  
       tf.contrib.losses.mean_squared_error

您必须将 x 和 x_pred 作为 x 并将 x_pred 作为 tf.placeholders 传递 我认为对于模型创建,您可以使用 keras,但您必须再次使用 sess.run() 运行计算图

引用资料: https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html#using-keras-models-with-tensorflow

关于python - 如何在 Keras 中创建随时间变化的损失函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40055225/

相关文章:

python - 如何 pickle __main__ 中定义的函数/类(python)

Python 正则表达式 : Find all caps with no following lowercase

python - 如何用迭代器修改可迭代对象的元素? IE。如何在 Python 中获取写迭代器?

machine-learning - 如何在 Tensorflow 中使用经过训练的 CNN 模型进行对象识别

tensorflow - 我们在YOLO的输出层使用哪个激活函数?

python - 如何向使用 Keras 构建的 CNN 模型添加第二个输入参数(第一个是图像)?

tensorflow - Keras 模型保存错误 : TypeError: get_config() missing 1 required positional argument: 'self'

Python Flask 只运行一次代码

apache-spark - Java 中的 1-of-k 编码 Apache Spark

python - 值错误 : Output tensors to a Model must be the output of a TensorFlow `Layer`