Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

python - 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

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? Is it guranteed that the lambda layer function is called for each and every image(input_data)?

2) Can someone suggest me how to pass multiple arguments to a loss function?

3) Can the final outcome of a loss function be a scalar or it has to be a vector or matrix?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...