NEGU93 / cvnn

Library to help implement a complex-valued neural network (cvnn) using tensorflow as back-end
https://complex-valued-neural-networks.readthedocs.io/
MIT License
164 stars 34 forks source link

ValueError: Invalid dtype: complex64 with TF 2.16+ #50

Open zzc121 opened 6 months ago

zzc121 commented 6 months ago

1713414842424 1713414867543

zzc121 commented 6 months ago

I'm not sure why the data type is incorrect, how should I modify it? Thank you.

zzc121 commented 6 months ago

11 maybe the "get_D2NN_model()"is wrong

aatumo commented 6 months ago

Hi, I have similar errors when creating the input layer model.add(complex_layers.ComplexInput(input_shape=(input_features, ))).

image

Then it points me to variables.py file where complex64 is not in the list of ALLOWED_DTYPES. Please see the figure attached.

image

NEGU93 commented 6 months ago

Hello, thank you both for your interest in my work.

@zzc121, does the error occur at the Detector layer? can you try removing it? I am trying to find where the error is. @aatumo, indeed, Tensorflow won't allow this dtype, this verification should be avoided, I don't see what can be wrong in your code. Can you provide me with a MRE?

aatumo commented 6 months ago

@NEGU93 thanks for your prompt reply. I tried the recommendation tf.as_dtype(np.complex64) however I observe the same error.

image

Attached are the MRE file together with the datasets:

#################### MRE ############################ import os os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' import numpy as np from keras.models import Sequential import cvnn import cvnn.layers as complex_layers import tensorflow as tf import pandas as pd

Loading dataset and processing

def load_dataset(): """ loads the input/out IQ samples for both training and test sets

Return:
    x_train -- of shape (3, 698)
    y_train -- of shape (2, 698)
    x_test -- of shape (3, 298)
    y_test -- of shape (2, 298)

"""
x_train_load = pd.read_csv("x_train_IQ_samples.csv", header=None)
y_train_load = pd.read_csv("y_train_IQ_samples.csv", header=None)
x_test_load = pd.read_csv("x_test_IQ_samples.csv", header=None)
y_test_load = pd.read_csv("y_test_IQ_samples.csv", header=None)

# stripping the curly braces from the table
x_train = x_train_load.astype(str).apply(lambda col: col.str.strip(' ()'))
y_train = y_train_load.astype(str).apply(lambda col: col.str.strip(' ()'))
x_test = x_test_load.astype(str).apply(lambda col: col.str.strip(' ()'))
y_test = y_test_load.astype(str).apply(lambda col: col.str.strip(' ()'))

# converting pandas dataframe to numpy array
x_train = x_train.to_numpy()
y_train = y_train.to_numpy()
x_test = x_test.to_numpy()
y_test = y_test.to_numpy()

# checking the shapes of X, y and avgGain
print("x_train.shape: " + str(x_train.shape))
print("y_train.shape: " + str(y_train.shape))
print("x_test.shape: " + str(x_test.shape))
print("y_test.shape: " + str(y_test.shape))

return x_train, y_train, x_test, y_test

CVNN model creation

def create_complex_model(self, input_features, layer_dims, activation_hidden_complex= 'cart_relu', activation_output_complex='cart_tanh', alpha=0.001): """ creates a complex sequential model

Arguments:
input_features -- number of features that are fed to the 1st hidden layer (input features) with a shape of input.shape[0]
layer_dims -- defined the number of layers (hidden + output) and the number of neurons per layer
activation_hidden -- defines the hidden layer activation function that will be used in each neuron
activation_output -- defines the output layer activation function
alpha -- the learning rate

Returns:
model -- the CVNN model
"""

complex_model = Sequential()
L = len(layer_dims)

# Input layer
complex_model.add(complex_layers.ComplexInput(input_shape=(input_features,)), tf.as_dtype(np.complex64))

# Hidden layers
for l in range(L):
    complex_model.add(complex_layers.ComplexDense(units=layer_dims[l], activation=activation_hidden_complex,
                                                  kernel_initializer='ComplexGlorotUniform'))

# Output layer
complex_model.add(complex_layers.ComplexDense(units=2, activation=activation_output_complex))

# Compile CVNN model, compile as TensorFlow model
complex_model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=alpha),
                      loss=cvnn.losses.ComplexAverageCrossEntropy(),
                      metrics=cvnn.metrics.ComplexCategoricalAccuracy())

return complex_model

parameter settings

layer_dims = [12, 7, 6] alpha = 0.0025 activation_hidden_complex = 'cart_relu' activation_output_complex = 'cart_tanh'

x_train_IQ_samples, y_train_IQ_samples, x_test_IQ_samples, y_test_IQ_samples = load_dataset() input_features = x_train_IQ_samples.shape[0]

create model

model_complex = create_complex_model(input_features, layer_dims, activation_hidden_complex, activation_output_complex, alpha)

model_complex.summary()

Training

batch_size = 50 epochs = 10

history = model_complex.fit(x_train_IQ_samples.T, y_train_IQ_samples.T, epochs=epochs, batch_size=batch_size, validation_data=(x_test_IQ_samples.T, y_test_IQ_samples.T), verbose=1) ####################################################

x_test_IQ_samples.csv x_train_IQ_samples.csv y_test_IQ_samples.csv y_train_IQ_samples.csv

angelSAT commented 6 months ago

I am getting the same error

angelSAT commented 6 months ago

URGENT HELP NEEDED WITH THIS!!!!!

NEGU93 commented 6 months ago

I changed the MRE example and it works for me. Here is the link, you can run it. I generated random numpy arrays as I don´t have the csv file. I also did some minor modifications to make it work. I encourage you all to send me a similar link with the code not working and I could try fix it.

angelSAT commented 5 months ago

Thankyou for the reply, I am getting the same error while running your MRE, image_2024-05-07_170520847

aatumo commented 5 months ago

Thank you very much for quick reply! I did manage to run the modified code and it seems to work. The main issue seems to be the python package version mismatch, I tried to install the versions that you used in colab.

I have a follow up questions though:

As shown in the figure below, I see warning casting complex128 to float32. Is this related to the output layer, i.e., Imaginary separated from the Real part?

Is it possible to have an output layer with only one node (units = 1)? That would output the imaginary and real together as a single output sample.

I also observed the loss to be a negative value in many of the epochs, in some cases after certain number of epochs it also outputs nan. Is there any explanation to that? Is it a valid loss value?

image

davidihu commented 5 months ago

I'm seeing the same thing...

When using either "complex_input(shape=...)" or "ComplexInput(input_shape=...)", I get the message: ValueError: Invalid dtype: complex64

Does this appear to be due to an incompatibility between tensorflow and cvnn versions? I'm using tensorflow 2.16.1 and cvnn version 1.2.22. I get the same error with cvnn version 2.0.

The error is flagged in variables.py. This is just like @aatumo indicated 3 weeks ago.

Thanks for any help. Much appreciated.

angelSAT commented 5 months ago

Guys! I have been facing this issue for a few days finally managed to solve it like 5 mins ago!!!!!!! use Tensorflow Version 2.15.0, Keras 2.15.0 use tf-keras 2.15.0 I am so happy I am gonna cry

davidihu commented 5 months ago

Thanks to @angelSAT ! I still have more problems, but this seems to work: Python 3.10.12 keras 2.15.0 tensorflow 2.15.0 tensorflow-probability 0.23.0 # seems to be a required package tf-keras # appears not to be needed cvnn 1.2.22

NEGU93 commented 5 months ago

Ok, that's bad, it seems I need to fix compatibility issues with TF 2.16+. I will check that. Thank you guys for letting me know.

ashkanee commented 5 months ago

Thank you very much for quick reply! I did manage to run the modified code and it seems to work. The main issue seems to be the python package version mismatch, I tried to install the versions that you used in colab.

I have a follow up questions though:

As shown in the figure below, I see warning casting complex128 to float32. Is this related to the output layer, i.e., Imaginary separated from the Real part?

Is it possible to have an output layer with only one node (units = 1)? That would output the imaginary and real together as a single output sample.

I also observed the loss to be a negative value in many of the epochs, in some cases after certain number of epochs it also outputs nan. Is there any explanation to that? Is it a valid loss value?

image

I have the same problem and it would be great if you let us know if the compatibility issue is fixed, thanks.

mahdipourhadi commented 5 months ago

Hi to all I had similar problem (Invalid dtype: complex64) in running my code with cvnn and TF 2.16.0 on GPU. Regarding the above useful comments I tried to run my code with TF 2.15 and the mentioned compatible packages by @angelSAT . But I had a new error as follows:

2 root error(s) found. (0) UNIMPLEMENTED: DNN library is not found. [[{{node model/complex_conv2d/ComplexConv2D_2}}]] [[model/complex_batch_normalization/covariance/strided_slice_1/_28]] (1) UNIMPLEMENTED: DNN library is not found. [[{{node model/complex_conv2d/ComplexConv2D_2}}]] 0 successful operations. 0 derived errors ignored. [Op:__inference_train_function_12492] 2024-05-30 11:13:29.654498: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated. [[{{node PyFunc}}]]

//////////////////////////////////////////////////////////////////////// Can anyone help me with that please?

NEGU93 commented 5 months ago

This sounds more like an unrelated issue. Sounds like a cuDNN error, which is used for convolutions in both TF and XLA. Probably cuDNN is not linked properly.

Check this or this

mahdipourhadi commented 5 months ago

This sounds more like an unrelated issue. Sounds like a cuDNN error, which is used for convolutions in both TF and XLA. Probably cuDNN is not linked properly.

Check this or this

Actually I can run my complex-valued NN on CPU and also conventional CNN on GPU (with Ubuntu OS), but I have a problem in working with cvnn.

NEGU93 commented 5 months ago

Yes, as it is a problem with CUDA, everything using the CPU should work. Perhaps the fact that you can use CNN with GPU is more surprising. Maybe convolutional layers do not need cuDNN? I don't really know what it is used for.

mahdipourhadi commented 5 months ago

Guys! I have been facing this issue for a few days finally managed to solve it like 5 mins ago!!!!!!! use Tensorflow Version 2.15.0, Keras 2.15.0 use tf-keras 2.15.0 I am so happy I am gonna cry

Guys! I could finally solve my problem !!! I just reinstalled Ubuntu and recreated virtual environment as follows:

sudo apt-get install --reinstall ubuntu-desktop

conda create -n CNN_GPU python=3.10.12 conda activate CNN_GPU conda install -c "nvidia/label/cuda-12.2" cuda-toolkit python3 -m pip install nvidia-cudnn-cu12==8.9.* mkdir -p $CONDA_PREFIX/etc/conda/activate.d echo 'CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.file)"))' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/:$CUDNN_PATH/lib' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh source $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

pip install tensorflow==2.15.0 Verify the CPU setup (optional): python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))" Verify the GPU setup (optional): python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

pip install pandas pip install pillow pip install scipy pip install -U scikit-learn pip install matplotlib pip install cvnn pip install tf-keras==2.15.0 pip install tensorflow-probability==0.23.0