Repository: keras-team/keras

Issue:
Functional model computes wrong output signature in mixed_fp16
**System information**.
- Have I written custom code (as opposed to using a stock example script provided in Keras): no
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Google Colab
- TensorFlow installed from (source or binary): no matter
- TensorFlow version (use command below): 2.9.2, 2.11.0
- Python version: default for colab
- Bazel version (if compiling from source): 
- GPU model and memory: no matter
- Exact command to reproduce: see link below

**Describe the problem**.

According to https://www.tensorflow.org/guide/mixed_precision i set last layer activation dtype to float32 for mixed precision policy. But model calculates wrong output signature.

**Describe the current behavior**.

Even if last layer works in float32, output signature for model is still calculated as float16

**Describe the expected behavior**.

Model should estimate output signature in the same way as output_shape (layer by layer).

**[Contributing](https://github.com/keras-team/keras/blob/master/CONTRIBUTING.md)**.

- Do you want to contribute a PR? (yes/no): no

**Standalone code to reproduce the issue**.

https://colab.research.google.com/drive/1tTpjnOPsamotExpM814o69l58CFjMkcS?usp=sharing



Discussion:
We're thinking about it. It would require significant architecture changes, but it is doable.

Awesome. On the flip side, it's forced me to better understand these operations :)

To be honest I like having to specify both dimensions, because it helps you think about what the layer is doing (projections from space A to space B). It's the "functional" way to think about NNs, in terms of inputs and outputs, rather than in terms of hidden units.

However it is true that when it comes to convolutions and max pooling, computing the right values by hand can be a pain.

I think it would help newer users. Deep learning is complicated, I frequently recommend keras as it's easy to use yet fully featured (and seems to work well!). I think that would help further in terms of approachability.

Getting latest theano fixed the concat issue. Many thanks! I think you should maybe add a comment in the code or docs, as most people will pip install Theano, and who knows when it'll be fixed.

Same - I was about to post an issue asking for help figuring out how to calculate the sizes when stacking convolutional layers. (I'm failing at calcing them)

@ddofer Please refer this excellent source: http://cs231n.github.io/convolutional-networks/

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.


Resolving patch:
diff --git a/keras/engine/base_layer.py b/keras/engine/base_layer.py
--- a/keras/engine/base_layer.py
+++ b/keras/engine/base_layer.py
@@ -963,7 +963,12 @@ def check_type_return_shape(s):
             check_type_return_shape, input_signature
         )
         output_shape = self.compute_output_shape(input_shape)
-        dtype = self._compute_dtype
+
+        try:
+            dtype = self.output.dtype
+        except AttributeError:
+            dtype = self._compute_dtype
+
         if dtype is None:
             input_dtypes = [s.dtype for s in tf.nest.flatten(input_signature)]
             # Default behavior when self.dtype is None, is to use the first


Test patch: