Skip to content
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

[Feature request] Complex dtype input support for layers #19860

Open
refraction-ray opened this issue Jun 16, 2024 · 1 comment · May be fixed by #19872
Open

[Feature request] Complex dtype input support for layers #19860

refraction-ray opened this issue Jun 16, 2024 · 1 comment · May be fixed by #19872
Assignees
Labels
type:feature The user is asking for a new feature.

Comments

@refraction-ray
Copy link

See the original issue in tensorflow's repo: tensorflow/tensorflow#65306

Current behavior?

The following code works well for tf2.15 but fails tf2.16.1 with the introduction of keras3,

import tensorflow as tf
print(tf.__version__)

class MyDenseLayer(tf.keras.layers.Layer):
    def __init__(self, num_outputs):
        super(MyDenseLayer, self).__init__()
        self.num_outputs = num_outputs

    def build(self, input_shape):
        self.kernel = self.add_weight(shape=[int(input_shape[-1]),
                                         self.num_outputs], )

    def call(self, inputs):
        kernel = tf.cast(self.kernel, tf.complex64)
        return tf.matmul(inputs, kernel)

layer = MyDenseLayer(10)
layer(tf.zeros([10, 5], dtype=tf.complex64))

For short, tf2.16.1 with newly introduced keras 3 seems not supporting the input tensor for a layer in complex dtype

Relevant log output

[usr/local/lib/python3.10/dist-packages/keras/src/backend/common/variables.py](https://localhost:8080/#) in standardize_dtype(dtype)
    428 
    429     if dtype not in dtypes.ALLOWED_DTYPES:
--> 430         raise ValueError(f"Invalid dtype: {dtype}")
    431     return dtype
    432 

ValueError: Invalid dtype: complex64

It would be better that keras3 can support complex valued input for layers as keras did before. Complex valued input is very common in quantum machine learning use cases.

@mehtamansi29 mehtamansi29 added the type:feature The user is asking for a new feature. label Jun 17, 2024
mehtamansi29 added a commit to mehtamansi29/keras that referenced this issue Jun 18, 2024
Updated complex dtype for ALLOWED_DTYPES=complex64,complex128 in the documentation. Please have a look at the changes and do the needful. Thank you.
Fixes keras-team#19860
@mehtamansi29 mehtamansi29 linked a pull request Jun 18, 2024 that will close this issue
@mehtamansi29
Copy link
Collaborator

Hi @refraction-ray -

We have raised an internal fix for this issue. Soon it will be reflected in the documentation as well. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature The user is asking for a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants