-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use a custom objective function for a model? #369
Comments
You almost answer the question yourself. Create your objective function like ones in https://github.com/fchollet/keras/blob/master/keras/objectives.py like this, import theano
import theano.tensor as T
epsilon = 1.0e-9
def custom_objective(y_true, y_pred):
'''Just another crossentropy'''
y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
y_pred /= y_pred.sum(axis=-1, keepdims=True)
cce = T.nnet.categorical_crossentropy(y_pred, y_true)
return cce and pass it to compile argument
|
I am trying to write a new objective function like the following: def expert_loss(y_true, y_pred):
# y_pred is n-dimensional, y_true is n+1 dimensional.
return T.sqr(T.dot(y_true[:-1].T, y_pred) - y_true[-1]).mean(axis=-1) In this objective function the dimensions of |
@mthrok , this may be a naive question....but all you need to do is a create a loss function (although it looks like it is not simple if you are not familiar with Theano)? In back prop isnt the gradient needed? |
Hi, can you tell me how to check the dimension of y_true and y_pred?? I want to define a objective function which is dependent on the dice coefficient instead of accuracy and as we are using it for segmentation |
Hey, I also need to create a custom objective function, but in this case it should use the weights of the layer (instead of the output and prediction). Is there any workaround to access the weights? |
@rgalhama To access the weights of any layer after compilation of the entire model you can use Not sure whether this will work during run-time though. |
Is there a way of accessing the rest of the batch? I'm doing unsupervised learning and have a custom objective function which is " batch-wise" --- it depends on every example in the batch. |
Hi, |
@mthrok If I want to write a custom objective function myself, where should I put the function, in the objectives.py of keras file, or directly in my python file? I tried your example code and put it in my python file, but an error occured. 'Exception: Invalid objective: custom_objective' |
@mthrok I used compile(loss='custom_objective', ....) |
@LeZhengThu |
This is slightly off the topic, but how can I get shape of the theano tensor variables that go through a cost function? I am trying to write a custom cost function for an auto-encoder I built. It is basically generalization of 'mean_squared_error' to this case where for each input vector the output is also a vector rather than a value. Mathematically, it is the following: cost = sum_(i=1,...,N ) {||x_i - x'_i||^2} / N, where x_i is a vector. I modified mean_squared_error as follows: def vector_mse(X_true, X_pred):
from keras import backend as K
n_featues = K.shape(X_true)[1]
return n_features * K.mean(K.square(y_pred - y_true), aix=None)
# 'axis=None' makes 'mean' to go over all elements of the tensor,
# so I need to multiply the result with n_features to get the mean as defined in equation above. It fails on 'n_featues = K.shape(X_true)[1]' because K.shape(X_true) return 'Shape.0' instead of a tuple with number of rows/columns. I am not very familiar with theano backend, but I tried evaluating it as 'K.eval(K.shape(X_true))' but it doesn't work. I appreciate any ideas on how to make this work. |
I want to design a customized loss function in which we use the layer outputs in the loss function calculations. One solution that comes to my mind is to create a new input/output pair, let's call them X/Y. Then define X=[y,h_1] and Y=[y_pred,h_2] by Merge(concatenate) them, and then build a new cost function that decouples the Merged symbols and compute mse on each of them. Is this something that might work? |
@M-Taha Did you ever resolve this issue involving shape mismatch? I have the same problem. |
@shamidreza Have you found a cool way to do it? (I mean, avoiding the merge option) |
@curiale Unfortunately, no. I had to go back to my beloved theano for that. |
@shamidreza Thanks. I think that you should open a new issue about this. |
@hadi-ds I met the same problem like yours, have you solved it? |
@kobeee I ended up writing that objective function in theano as follows: def vector_mse(y_true, y_pred):
from theano import tensor as T
diff2 = (y_true - y_pred)**2
return T.mean(T.sum(diff2, axis = -1)) hope this helps. |
@hadi-ds Thank you! I have solved the problem. |
I have a similar problem as @hadi-ds except that mine couldn't be easily solved with tensor functions.
However, unlike Layer, custom loss function doesn't know the shape of tensor, and based on Issue 2801 it seems that keras doesn't support getting tensor shape or tensor split, so how can I implement my objective function? |
Hello, I'm trying to define my own metric,
But I get the following error, "object of type 'Tensor' has no len()". I know Tensor object does not have len attribute, but shape attribute does not work as well. For instance, y_true is represented as such Tensor("dense_4_target:0", shape=(?, ?), dtype=float32) Could you please help me about how to turn the code above into runnable form? |
@joetigger , I managed to work your problem around (but I'm not sure if your Lambdas will work ok, my case was solved with this). It's probably not the most expected solution, but it's the only thing I could do so far, and it works great :D I needed the loss function to carry the result of some keras layers, so, I created those layers as an independent model and appended those to the end of the model. The idea was to train the model using the already processed output instead of the original output: Creating the model for calculating the loss:
Now, let's create a function to join this model to the original model:
Now let's manage preparing our models for training:
Training - Important: you're going to compare with different results now, so, process your training Y:
And finally, for the results:
I hope it helps :D |
@danmoller Thanks for the tip! Yes, your solution would help solve my problem. 👍 |
Hii, I need to define my own loss function, I am using GAN model and my loss will include both adverserial loss and L1 loss between true and generated images, I tried to write a function but the folloeing error. ValueError: ('Could not interpret loss function identifier:', Elemwise{add,no_inplace}.0) my loss function is def loss_function (y_true, y_pred, y_true1, y_pred1):
Thanx in advance |
@hkmztrk Have you solved this problem? It bothers me too! |
Hey @PingHGao, sorry for my late reply! Yeah, check this out! https://stackoverflow.com/questions/43576922/keras-custom-metric-iteration |
Hi,
but it is giving me error. How can i do this. |
Dear experts, I've been trying to find the answer online without success: what are the expected output shape of the tensor for custom loss? Is it a single scalar value or a vector? Thank you Edit:
Can somebody explain me the difference in the behaviour and, most importantly, the reson for the different values? Again, thanks! |
@mverzett , I think there is some implementation level difference.
I do not understand why this should make a difference in your case as the last axis had only a single dimension though. |
I am having trouble converting this function to keras in order to calculate a custom loss.
`#additional loss x_test_normalized = tf.round(36 * inputs) So Basically, I am using a VAE where I want to integrate this additional loss as well. in detect_blocks(x) /opt/aiml4it/anaconda/3-5.2.0-generic/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in iter(self) TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.` |
@mthrok What is the requirement on the input y_pred and y_true? Should they come from logits or from softmax that performs as a probability just as https://github.com/tensorflow/tensorflow/blob/r1.11/tensorflow/python/keras/backend.py? |
Your question (and, tbh, all the others) are better suited for stack overflow, considering they aren't bugs in keras but questions regarding its use. You are also more likely to get useful answers by investing a modicum of time to at least properly format your post. It's currently lacking line breaks and respect for the reader. That being said, I'll give you the hint that Tensorflow was giving you a hint:
That's a pretty useful error message. Did you try � The larger problem, however, is that you haven't grokked how tensorflow actually works. https://www.tensorflow.org/guide/graphs might be a good introduction. Specifically, getting a tensor's length does not make much sense because a tensor's length/shape is fixed at compile time. The process of implementing a custom loss isn't fundamentally different from doing so in standard python. It's just that you cannot manipulate tensors with the python stdlib you are used to. See the tensorflow documentation for the operations that are defined on tensors (such as |
* added perceiver image classifier * removed tfa dependency and replaced tfa LAMB optimizer and Input names * modify comment in tutorial
* added perceiver image classifier * removed tfa dependency and replaced tfa LAMB optimizer and Input names * modify comment in tutorial
New to Keras and DL, so I may be asking really basic questions... appreciate if someone could explain in an easier term. Thanks!
How to use a custom objective function for a model? Any sample code I could look into to reference from?
It seems models can only accept a string of the pre-defined objective functions here : https://github.com/fchollet/keras/blob/master/keras/objectives.py
However, the doc (http://keras.io/models/) sounds like as if I could just plug in a custom function like score(true, pred):
loss: str (name of objective function) or objective function.
The source code also seems to say it's only string: https://github.com/fchollet/keras/blob/master/keras/models.py#L240
The text was updated successfully, but these errors were encountered: