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

WARNING: complex_conv2d_2 - Expected input to be <dtype: 'complex64'>, but received <dtype: 'float32'>. This is normally fixed using ComplexInput() at the start (tf casts input automatically to real). #20

Closed teodorf-bit closed 2 years ago

teodorf-bit commented 2 years ago

Hi again!!

I get the warning

WARNING: complex_conv2d_2 - Expected input to be <dtype: 'complex64'>, but received <dtype: 'float32'>. This is normally fixed using ComplexInput() at the start (tf casts input automatically to real).

in each epoch when I run

x_train, x_test, y_train, y_test = train_test_split(new_features, labels, test_size=0.2, random_state=42) x_train, x_test, y_train, y_test = np.array(x_train,dtype=np.complex64), np.array(x_test,dtype=np.complex64), np.array(y_train,dtype=np.complex64), np.array(y_test,dtype=np.complex64)

model = tf.keras.models.Sequential() model.add(complex_layers.ComplexInput(input_shape=(8,7,3))) # Always use ComplexInput at the start model.add(complex_layers.ComplexConv2D(32, (1, 1), activation='cart_relu')) model.add(complex_layers.ComplexAvgPooling2D((1, 1))) model.add(complex_layers.ComplexConv2D(64, (1, 1), activation='cart_relu')) model.add(complex_layers.ComplexMaxPooling2D((2, 2))) model.add(complex_layers.ComplexConv2D(64, (1, 1), activation='cart_relu')) model.add(complex_layers.ComplexFlatten()) model.add(complex_layers.ComplexDense(64, activation='cart_relu')) model.add(complex_layers.ComplexDense(2, activation='convert_to_real_with_abs')) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) model.summary()

history = model.fit(x_train, y_train, epochs=6, validation_data=(x_test, y_test))

NEGU93 commented 2 years ago

It is indeed strange, at some point the data is becoming real valued. I have some request if you could.

  1. Can you show me the output of model.summary()?
  2. Which version of the code are you using? Be sure to always keep the last version and try to favour PIP over ANACONDA.
  3. Could you provide a MWE? Maybe generate a random dataset and upload the code. If you use Google colab you can even run the code on a cloud.

Thank you very much.

NEGU93 commented 2 years ago

I am closing this issue due to inactivity. If you (or someone) come across this issue again just comment and I will re-open it.

cboily commented 2 years ago

Hello!

Thank you very much for this library to treat complex values.

I have the same issue. The warning appears for the 3rd layer of after the I used the code you provided in the doc of the simple example, https://complex-valued-neural-networks.readthedocs.io/en/latest/cvnn.html.

The code used import numpy as np import cvnn.layers as complex_layers import tensorflow as tf

def get_dataset(): (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data() train_images = train_images.astype(dtype=np.complex64) / 255.0 test_images = test_images.astype(dtype=np.complex64) / 255.0 return (train_images, train_labels), (test_images, test_labels) p=tf.config.list_physical_devices('GPU') tf.config.experimental.set_memory_growth(p[0], True) (train_images, train_labels), (test_images, test_labels) = get_dataset() # to be done by each user

model = tf.keras.models.Sequential() model.add(complex_layers.ComplexInput(input_shape=(32, 32, 3))) # Always use ComplexInput at the start model.add(complex_layers.ComplexConv2D(32, (3, 3), activation='cart_relu')) model.add(complex_layers.ComplexAvgPooling2D((2, 2))) model.add(complex_layers.ComplexConv2D(64, (3, 3), activation='cart_relu')) model.add(complex_layers.ComplexMaxPooling2D((2, 2)))

model.add(complex_layers.ComplexConv2D(64, (3, 3), dtype=np.complex64, activation='cart_relu')) model.add(complex_layers.ComplexFlatten()) model.add(complex_layers.ComplexDense(64, activation='cart_relu')) model.add(complex_layers.ComplexDense(10, activation='convert_to_real_with_abs')) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) model.summary()

p=tf.config.list_physical_devices('GPU')

Train model

history = model.fit(train_images, train_labels, epochs=6, validation_data=(test_images, test_labels))

Evaluate model

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

The output of the terminal Model: "sequential"


Layer (type) Output Shape Param #

complex_conv2d (ComplexConv2 (None, 30, 30, 32) 1792


complex_avg_pooling2d (Compl (None, 15, 15, 32) 0


complex_conv2d_1 (ComplexCon (None, 13, 13, 64) 36992


complex_max_pooling2d (Compl (None, 6, 6, 64) 0


complex_conv2d_2 (ComplexCon (None, 4, 4, 64) 73856


complex_flatten (ComplexFlat (None, 1024) 0


complex_dense (ComplexDense) (None, 64) 131200


complex_dense_1 (ComplexDens (None, 10) 1300

Total params: 245,140 Trainable params: 245,140 Non-trainable params: 0


2022-04-07 17:48:26.016937: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 1228800000 exceeds 10% of free system memory. Epoch 1/6 WARNING: complex_conv2d_2 - Expected input to be <dtype: 'complex64'>, but received <dtype: 'float32'>. This is normally fixed using ComplexInput() at the start (tf casts input automatically to real).

The library was install with pip and the version is 1.0.0.

Thank you very much.

NEGU93 commented 2 years ago

I don't have this problem. Here you have the notebook with the code.

Please be sure to update the library cvnn just in case.