简体   繁体   English

正确的损失函数方式

[英]Correct way of loss function

Hi I have been trying to implement a loss function in keras. 您好我一直在尝试在喀拉拉邦实现损失功能。 But i was not able to figure a way to pass more than 2 arguments other than loss(y_true, y_predict) so I thought of using a lambda layer as the last layer and doing my computation in lambda layer itslef and simply returning the value of y_predict in loss function like this 但是我无法找到一种方法来传递除loss(y_true,y_predict)以外的2个以上参数,因此我考虑将lambda层用作最后一层,并在lambda层itslef中进行计算,并简单地返回y_predict的值像这样的损失函数

def loss_function(x):
    loss = some calculations
    return loss

def dummy_loss(y_true, y_pred):
    return y_pred

def primary_network():
    global prim_binary_tensor
    x = VGG16(weights='imagenet', include_top=True, input_shape=image_shape)
    last_layer = Dense(k_bit, activation='tanh', name='Dense11')(x.layers[-1].output)
    last_layer, x = basic_model()
    lambda_layer = Lambda(loss_function)([last_layer, prim_binary_tensor])
    model = Model(inputs=[x.input, prim_binary_tensor], outputs=[lambda_layer])
    model.compile(optimizer="adam", loss=dummy_loss,metrics=['accuracy'])
    return model

So my question are: 所以我的问题是:

1) Am I doing it the right way to calculate the loss? 1)我是否以正确的方式计算损失? Is it guranteed that the lambda layer function is called for each and every image(input_data)? 是否保证为每个图像(input_data)调用lambda层函数?

2) Can someone suggest me how to pass multiple arguments to a loss function? 2)有人可以建议我如何将多个参数传递给损失函数吗?

3) Can the final outcome of a loss function be a scalar or it has to be a vector or matrix? 3)损失函数的最终结果可以是标量还是必须是向量或矩阵?

Answers to your questions: 您的问题的答案:

  1. I don't know whether your approach works, but there is an easier solution. 我不知道您的方法是否可行,但是有一个更简单的解决方案。

  2. You can pass multiple arguments by defining a partial function. 您可以通过定义部分函数来传递多个参数。

  3. The output of a loss function is a scalar. 损失函数的输出是标量。

Here is an example that demonstrates how to pass multiple arguments to a loss function: 这是一个示例,演示如何将多个参数传递给损失函数:

from keras.layers import Input, Dense
from keras.models import Model
import keras.backend as K


def custom_loss(arg1, arg2):
    def loss(y_true, y_pred):
        # Use arg1 and arg2 here as you wish and return loss
        # For example:
        return K.mean(y_true - y_pred) + arg1 + arg2
    return loss

x = Input(shape=(1,))
arg1 = Input(shape=(1,))
arg2 = Input(shape=(1,))
out = Dense(1)(x)
model = Model([x, arg1, arg2], out)
model.compile('sgd', custom_loss(arg1, arg2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在以下代码中定义 tensorflow 2.+ 中的损失 function 的正确方法是什么? - What is the correct way to define the loss function in tensorflow 2.+ in the following code? 在自定义损失 function 中使用 tf.while_loop 的正确方法是什么? - What's the correct way to use tf.while_loop in a custom loss function? 减少骰子损失的正确方法 - Correct way to reduce dimension in dice loss 计算感知损失的 VGG 特征的正确方法 - Correct way to compute VGG features for Perceptual loss 我的自定义损失函数正确吗? (Pytorch) - Is my custom loss function correct? (Pytorch) 测试功能的正确方法 - Correct way to test a function 如何为SGDClassifier的数据计算损失函数的值? - Way to compute the value of the loss function on data for an SGDClassifier? 有没有办法将时间权重传递给损失 function? - Is there a way to pass along temporal weights to a loss function? 有没有办法在 keras 中编写自定义损失 function? - Is there a way to write up a custom loss function in keras? 在 TF2/Keras 中正确实现自动编码器 MSE 损失 function - Correct implementation of Autoencoder MSE loss function in TF2/Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM