Search code examples
pythonkeraslstm

How to define an spesific loss function for keras LSTM model


Ive just started learning machine learning using python with Keras and I have recently created a basic LSTM RNN.

My question is if it is possible to define a function error myself without having to specifie the target data.

In the basic model I created, I gave the input data and the corresponding target data for training, specifying which function error to use, like "meansquarederror".

My question is if it is possible to define a model in keras in which only the inputs, and a custom function error that would take the output of the NN to calculate an error (completly new metric), where given for training the NN.

What I want is to calculate the error of each input in predicting the outputs with a custom function.

Is that possible?


Solution

  • Ok ive found it is much easier than I thought.

    def custom_loss(y_true, y_pred):
                
        # calculate loss, using y_pred
            
        return loss
      
    model.compile(loss=custom_loss, optimizer='adam')
    

    Its done just like that