I confirm, the mentioned workaround effectively solved my issue:
  

      github.com/tensorflow/tensorflow
  

  
    
  
	  
  

  
    
      Not every keras.metrics.* accept from_logits=True 12
    

    
      
        opened 
        
          
        
        Aug 10, 2020
      
      

        
          closed 
        
          
        
        Jun 4, 2021
      
        

      
        
          
          adamp87
        
      
    

    
        
          stat:awaiting tensorflower
        
        
          type:bug
        
        
          comp:keras
        
        
          TF 2.3
        
    
  


  
    **System information**
- Have I written custom code: Yes
- OS Platform and Dis…tribution: Linux Ubuntu 18.04.3
- TensorFlow installed from: binary (Docker Image tensorflow:2.3.0-gpu)
- TensorFlow version: v2.3.0-rc2-23-gb36436b087 2.3.0
- Python version: 3.6.9 
- CUDA/cuDNN version: CUDA10.2
- GPU model and memory: RTX 2080 8GB


**Describe the current behavior**
The issue is the same as described by MeghnaNatraj in ticket [37103](https://github.com/tensorflow/tensorflow/issues/37103). The mentioned ticket states the problem has been solved. I was requested to open a new ticket.

If I have a model with an output layer of one neuron and 'sigmoid' activation function and using BinaryCrossentropy(from_logits=False), then the model.fit() function accepts any metrics (e.g. Precision)
If I have the same model as before but without activation function and BinaryCrossentropy(from_logits=True), then model.fit() function will return an error.

**Describe the expected behavior**
- Having a model, which has an output layer without an activation function: keras.layers.Dense(1) # Output range is [-inf, +inf]
- Loss function of the model working with logits: BinaryCrossentropy(from_logits=True)
- Accepting metrics during fitting like keras.metrics.Precision

**Standalone code to reproduce the issue**
I have copied the same test code by MeghnaNatraj in ticket [37103](https://github.com/tensorflow/tensorflow/issues/37103) 

```
# INITIALIZE

import tensorflow as tf
import numpy as np
# define dataset
dataset = np.array([[6,148,72,35,0,33.6,0.627,50,1],
[1,85,66,29,0,26.6,0.351,31,0],
[8,183,64,0,0,23.3,0.672,32,1],
[1,89,66,23,94,28.1,0.167,21,0],
[0,137,40,35,168,43.1,2.288,33,1]])
# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]
```

```
# WORKS: Model output is in range [0, 1]
# define the keras model
model = tf.keras.models.Sequential([tf.keras.layers.Dense(12, input_dim=8, activation='relu'),
                                    tf.keras.layers.Dense(1, activation='sigmoid')])
# compile the keras model
model.compile(loss=tf.keras.losses.BinaryCrossentropy(),
              optimizer=tf.keras.optimizers.RMSprop(),
              metrics=[tf.keras.metrics.Accuracy(name='accuracy'),
                       tf.keras.metrics.Precision(name='precision'),
                       tf.keras.metrics.TruePositives(name='tp')])
# fit the keras model on the dataset
model.fit(X, y, epochs=10, batch_size=10, verbose=0)
```

```
# DOES NOT WORK: Model output is in range [-inf, inf]
# define the keras model
model = tf.keras.models.Sequential([tf.keras.layers.Dense(12, input_dim=8, activation='relu'),
                                    tf.keras.layers.Dense(1)])
# compile the keras model
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer=tf.keras.optimizers.RMSprop(),
              metrics=[tf.keras.metrics.Accuracy(name='accuracy'),
                       tf.keras.metrics.Precision(name='precision'),
                       tf.keras.metrics.TruePositives(name='tp')])
# fit the keras model on the dataset
model.fit(X, y, epochs=10, batch_size=10, verbose=0)

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  assertion failed: [predictions must be <= 1] [Condition x <= y did not hold element-wise:] [x (sequential_8/dense_17/BiasAdd:0) = ] [[22.0911655][62.7297783][35.2793274]...] [y (metrics/precision/Cast_3/x:0) = ] [1]
	 [[{{node metrics/precision/assert_less_equal/Assert/AssertGuard/else/_11/Assert}}]]
	 [[metrics/tp/assert_greater_equal/Assert/AssertGuard/pivot_f/_31/_61]]
  (1) Invalid argument:  assertion failed: [predictions must be <= 1] [Condition x <= y did not hold element-wise:] [x (sequential_8/dense_17/BiasAdd:0) = ] [[22.0911655][62.7297783][35.2793274]...] [y (metrics/precision/Cast_3/x:0) = ] [1]
	 [[{{node metrics/precision/assert_less_equal/Assert/AssertGuard/else/_11/Assert}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_distributed_function_12253]

Function call stack:
distributed_function -> distributed_function

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1098, in fit
    tmp_logs = train_function(iterator)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 780, in __call__
    result = self._call(*args, **kwds)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 840, in _call
    return self._stateless_fn(*args, **kwds)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2829, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 1848, in _filtered_call
    cancellation_manager=cancellation_manager)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 1924, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 550, in call
    ctx=ctx)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  assertion failed: [predictions must be <= 1] [Condition x <= y did not hold element-wise:] [x (sequential/dense_1/BiasAdd:0) = ] [[28.8497276][37.4658966][48.6209221]...] [y (Cast_5/x:0) = ] [1]
         [[{{node assert_less_equal/Assert/AssertGuard/else/_11/assert_less_equal/Assert/AssertGuard/Assert}}]]
         [[assert_less_equal_1/Assert/AssertGuard/pivot_f/_41/_81]]
  (1) Invalid argument:  assertion failed: [predictions must be <= 1] [Condition x <= y did not hold element-wise:] [x (sequential/dense_1/BiasAdd:0) = ] [[28.8497276][37.4658966][48.6209221]...] [y (Cast_5/x:0) = ] [1]
         [[{{node assert_less_equal/Assert/AssertGuard/else/_11/assert_less_equal/Assert/AssertGuard/Assert}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_1165]
```