Keras: Write custom objective function for keras/tensorflow

Created on 9 Jan 2016  路  5Comments  路  Source: keras-team/keras

Similar to this issue https://github.com/fchollet/keras/issues/369
But backend is tensorflow. I wanted to write auc function as objective.

def auc_objective(y_true, y_pred):
    y_true = y_true.eval()
    y_pred = y_pred.eval()
    auc_score = 1.0 - roc_auc_score(y_true, y_pred)
    return auc_score

But got following error :

     File "run_keras.py", line 96, in auc_objective
     y_true = y_true.eval()
     File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 460, in eval
     return _eval_using_default_session(self, feed_dict, self.graph, session)
     File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2896, in _eval_using_default_session
     raise ValueError("Cannot evaluate tensor using eval(): No default "
     ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use `with sess.as_default()` or pass an explicit session to eval(session=sess)

Tried to set tf.session() but did not work. Any help appreciated.

stale

Most helpful comment

As I understand the logic of the tensorflow and keras basic network building, during building the graph itself you cannot get access to the values inside. Everything that you have are tensors (just abstract containers of future flow, when input will be given) and you can apply only native core (tensorflow in this case) functions to it. Eval will not work and convertion to numpy array is also impossible

All 5 comments

I think the bigger issue here is that the AUC is not a differentiable function of the weights...

Though this doesn't lead to the error you see.

No need to use eval() function, Maybe just form your pred and true label to numpy array.

As I understand the logic of the tensorflow and keras basic network building, during building the graph itself you cannot get access to the values inside. Everything that you have are tensors (just abstract containers of future flow, when input will be given) and you can apply only native core (tensorflow in this case) functions to it. Eval will not work and convertion to numpy array is also impossible

I have just that issue trying to do something very similar as the OP. Curious if someone found a good workaround, or some solution might have been incorporated in keras?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

Was this page helpful?
0 / 5 - 0 ratings