question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error using K.round() in custom loss function

See original GitHub issue

In my custom loss function I’m trying to use K.round(y_pred) as part of my calculation. This gives me a ValueError: None values not supported. When I remove the K.round() it all works perfectly. Does anyone know what the problem might be? Thank you very much.

Here is the full error log:

Traceback (most recent call last):
  File "explore_network_structure.py", line 141, in <module>
    history = model.fit(x_train, y_train, nb_epoch=2000, batch_size=batch_size, shuffle=True, verbose=1, class_weight=[class_weight_A, class_weight_B], sample_weight=train_weight, callbacks=[csv_logger, early_stopping])
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 620, in fit
    sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1079, in fit
    self._make_train_function()
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 696, in _make_train_function
    self.total_loss)
  File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 154, in get_updates
    v = self.momentum * m - lr * g  # velocity
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 750, in binary_op_wrapper
    y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 657, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 346, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Dref360commented, Aug 8, 2017

@yushuinanrong why up? K.round is not differentiable, you cannot use it in a loss function.

2reactions
oxridercommented, Oct 25, 2016

Here is my custom loss:

def func_custom_loss(y_true, y_pred):
    return K.mean(y_pred[:,0], axis=-1)

Currently it is working fine but if I replace y_pred[:,0] with K.round(y_pred[:,0]) it will spit the above error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting an error with my custom loss function in Keras
I'm creating a custom loss function that incorporates a lot of code and equations. When I try to compile my model as so:....
Read more >
Stuck while writing custom loss - Google Groups
I am using writing a custom loss for an LSTM, logically defined as abs(y_true - y_pred) * weight_dict[round(y_pred)] Here, y can take real...
Read more >
Custom loss function for binary classificatio in Keras gets error
If I try to round the pred values (e.g. K.round ) or convert it to binary to count the number of 1 s,...
Read more >
tf.keras.losses.Loss | TensorFlow v2.11.0
Loss base class. ... call() : Contains the logic for loss calculation using y_true , y_pred . ... using AUTO or SUM_OVER_BATCH_SIZE will...
Read more >
How To Build Custom Loss Functions In Keras For Any Use ...
Now when our model is going to be trained, it will use the Mean Squared Error loss function to compute the loss, update...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found