Giter Site home page Giter Site logo

negu93 / cvnn Goto Github PK

View Code? Open in Web Editor NEW
143.0 11.0 31.0 53.9 MB

Library to help implement a complex-valued neural network (cvnn) using tensorflow as back-end

Home Page: https://complex-valued-neural-networks.readthedocs.io/

License: MIT License

Shell 0.45% Python 80.25% Jupyter Notebook 19.30%
neural-networks tensorflow complex-networks complex-neural-networks complex-numbers deep-learning machine machine-learning python

cvnn's Introduction

Complex-Valued Neural Networks (CVNN)

Done by @NEGU93 - J. Agustin Barrachina

Documentation Status PyPI version Anaconda cvnn version DOI Downloads

Using this library, the only difference with a Tensorflow code is that you should use cvnn.layers module instead of tf.keras.layers.

This is a library that uses Tensorflow as a back-end to do complex-valued neural networks as CVNNs are barely supported by Tensorflow and not even supported yet for pytorch (reason why I decided to use Tensorflow for this library). To the authors knowledge, this is the first library that actually works with complex data types instead of real value vectors that are interpreted as real and imaginary part.

Update:

  • Since v1.12 (28 June 2022), Complex32 and Complex Convolutions in PyTorch.
  • Since v0.2 (25 Jan 2021) complexPyTorch uses complex64 dtype.
  • Since v1.6 (28 July 2020), pytorch now supports complex vectors and complex gradient as BETA. But still have the same issues that Tensorflow has, so no reason to migrate yet.

Documentation

Please Read the Docs

Instalation Guide:

Using Anaconda

conda install -c negu93 cvnn

Using PIP

pip install cvnn

Short example

From "outside" everything is the same as when using Tensorflow.

import numpy as np
import tensorflow as tf

# Assume you already have complex data... example numpy arrays of dtype np.complex64
(train_images, train_labels), (test_images, test_labels) = get_dataset()        # to be done by each user

model = get_model()   # Get your model

# Compile as any TensorFlow model
model.compile(optimizer='adam', metrics=['accuracy'],
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))
model.summary()

# Train and evaluate
history = model.fit(train_images, train_labels, epochs=epochs, validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

The main difference is that you will be using cvnn layers instead of Tensorflow layers. There are some options on how to do it as shown here:

Sequential API

import cvnn.layers as complex_layers

def get_model():
    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), 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'))   
    # An activation that casts to real must be used at the last layer. 
    # The loss function cannot minimize a complex number
    return model

Functional API

import cvnn.layers as complex_layers
def get_model():
    inputs = complex_layers.complex_input(shape=(128, 128, 3))
    c0 = complex_layers.ComplexConv2D(32, activation='cart_relu', kernel_size=3)(inputs)
    c1 = complex_layers.ComplexConv2D(32, activation='cart_relu', kernel_size=3)(c0)
    c2 = complex_layers.ComplexMaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(c1)
    t01 = complex_layers.ComplexConv2DTranspose(5, kernel_size=2, strides=(2, 2), activation='cart_relu')(c2)
    concat01 = tf.keras.layers.concatenate([t01, c1], axis=-1)

    c3 = complex_layers.ComplexConv2D(4, activation='cart_relu', kernel_size=3)(concat01)
    out = complex_layers.ComplexConv2D(4, activation='cart_relu', kernel_size=3)(c3)
    return tf.keras.Model(inputs, out)

Project status

I currently work as a full-time employee and therefore the mantainance of this repository has been reduced or stopped. I would happily welcome anyone who wishes to fork the project or volunteer to step in as a maintainer or owner, allowing the project to keep going.

About me & Motivation

My personal website

I am a PhD student from Ecole CentraleSupelec with a scholarship from ONERA and the DGA

I am basically working with Complex-Valued Neural Networks for my PhD topic. In the need of making my coding more dynamic I build a library not to have to repeat the same code over and over for little changes and accelerate therefore my coding.

Cite Me

Alway prefer the Zenodo citation.

Next you have a model but beware to change the version and date accordingly.

@software{j_agustin_barrachina_2022_7303587,
  author       = {J Agustin Barrachina},
  title        = {NEGU93/cvnn: Complex-Valued Neural Networks},
  month        = nov,
  year         = 2022,
  publisher    = {Zenodo},
  version      = {v2.0},
  doi          = {10.5281/zenodo.7303587},
  url          = {https://doi.org/10.5281/zenodo.7303587}
}

Issues

For any issues please report them in here

This library is tested using pytest.

pytest logo

cvnn's People

Contributors

angelld23 avatar negu93 avatar vandluke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cvnn's Issues

initializers.py (getconfig method)

Hi, I've just started as a PhD student. I work with holograms. That's why I thought I could try your framework to train a model in the complex space instead of the real space.

When running my code I encountered an issue with this method. It says that the "seed" is not a valid argument. I modified the code as follows and it seems to work now. I wanted to propose a pull-request, but the functionality is blocked.

image

nan gradient for ComplexDense with angle-based loss

@NEGU93 Thanks for this great piece of work!

I am having an issue with angle-based loss functions. See here a small example:
https://colab.research.google.com/drive/10y2eBxHMq5HCbHqOsrfKzvrJ7AV_RegZ?usp=sharing

  1. instantiate a model with a single complex dense layer with one unit and no activation;
  2. make an input tensor with a single "zero" sample;
  3. define a reconstruction loss comparing angles of input vs output;
  4. pass the input through the model, get 0 loss, and nan gradients.

The gradients look OK (not nan) when the input is not zero. Maybe I am doing something wrong?

Thanks.

Error while adding a layer

When trying to add a layer using:

model.add(complex_layers.ComplexDense(15, activation='relu'))
I get:

self.w_r = self.add_weight('kernel_r', TypeError: add_weight() got multiple values for argument 'shape'

Can you please help? Thanks

AttributeError: 'Tensor' object has no attribute 'numpy'

Hi Barrachina,

I have been testing your CVNN library and the examples you provided.
For the convolutional code example in the readme file, I'm getting sometimes the following error

........................\Anaconda3\lib\site-packages\cvnn\layers.py:965 pool_function *
res = [tf.convert_to_tensor(flat_in.numpy()[i][arg_ind]) for i, arg_ind in enumerate(flat_argmax.numpy())]
AttributeError: 'Tensor' object has no attribute 'numpy'

Do you have a comment on it?
Sorry if this was already addressed somewhere and I missed it.

Cannot load a trained complex model

Hi,

I can train a model consist of complex convolutional and deconvolutional layers however while loading the trained complex model it pops up such a kind of error.
Any idea how to get rid of this error?
.................................
File "/home/shah/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 321, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown layer: ComplexInput

ComplexConv1D returned "Expected floating point type, got <dtype: 'complex64'>"

Hi, congrats for your work !

I am trying to make the code (in the examples) works with the convolution ComplexConv1D layer without success at all, in spite of several different attempts I've done. The complex inputs were generated in numpy.
The output has been:

File "/home/mhsc/anaconda3/envs/cvnn/lib/python3.6/site-packages/tensorflow/python/ops/init_ops_v2.py", line 1051, in _assert_float_dtype
raise ValueError("Expected floating point type, got %s." % dtype)

ValueError: Expected floating point type, got <dtype: 'complex64'>.

Any help would be immensely appreciated

Complex data type error with TensorFlow Functional API

@NEGU93 first & foremost, thank you for a contribution like this. Development and support for complex-value neural networks is long overdue and much-needed!

I've been starting to explore use of your CVNN library and am encountering an issue out of the gate using your ReadMe as a starting point. Just curious, am I missing something while interacting with tf or did something break on the functional API side? (I'm able to build, train, and test a sequential model based on the ReadMe without issue).

My environment is python 3.8.10, TensorFlow 2.8.0, and CVNN 1.2.13

I've provided my console out and the code example below as well.

Console Out

2022-03-23 16:29:16.489773: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-23 16:29:16.518763: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory
2022-03-23 16:29:16.518785: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2022-03-23 16:29:16.519056: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "model"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_2 (InputLayer)           [(None, 32, 32, 3)]  0           []                               
                                                                                                  
 complex_conv2d (ComplexConv2D)  (None, 30, 30, 32)  1792        ['input_2[0][0]']                
                                                                                                  
 complex_conv2d_1 (ComplexConv2  (None, 28, 28, 32)  18496       ['complex_conv2d[1][0]']         
 D)                                                                                               
                                                                                                  
 complex_max_pooling2d (Complex  (None, 14, 14, 32)  0           ['complex_conv2d_1[1][0]']       
 MaxPooling2D)                                                                                    
                                                                                                  
 complex_conv2d_transpose (Comp  (None, 28, 28, 5)   1290        ['complex_max_pooling2d[1][0]']  
 lexConv2DTranspose)                                                                              
                                                                                                  
 concatenate (Concatenate)      (None, 28, 28, 37)   0           ['complex_conv2d_transpose[1][0]'
                                                                 , 'complex_conv2d_1[1][0]']      
                                                                                                  
 complex_conv2d_2 (ComplexConv2  (None, 26, 26, 4)   2672        ['concatenate[1][0]']            
 D)                                                                                               
                                                                                                  
 complex_conv2d_3 (ComplexConv2  (None, 24, 24, 4)   296         ['complex_conv2d_2[1][0]']       
 D)                                                                                               
                                                                                                  
==================================================================================================
Total params: 24,546
Trainable params: 24,546
Non-trainable params: 0
__________________________________________________________________________________________________
2022-03-23 16:29:17.839433: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 1228800000 exceeds 10% of free system memory.
Epoch 1/4
Traceback (most recent call last):
  File "example_functional.py", line 37, in <module>
    history = model.fit(train_images, train_labels, epochs=epochs, validation_data=(test_images, test_labels), verbose=1)
  File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 1147, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:

    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/engine/training.py", line 860, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/engine/training.py", line 918, in compute_loss
        return self.compiled_loss(
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/engine/compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/losses.py", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/losses.py", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/losses.py", line 1862, in sparse_categorical_crossentropy
        return backend.sparse_categorical_crossentropy(
    File "/home/jeffrey/repos/cvnn/venv/lib/python3.8/site-packages/keras/backend.py", line 5202, in sparse_categorical_crossentropy
        res = tf.nn.sparse_softmax_cross_entropy_with_logits(

    TypeError: Value passed to parameter 'features' has DataType complex64 not in list of allowed values: float16, bfloat16, float32, float64

Example Test Script

import numpy as np
import tensorflow as tf
import cvnn.layers as complex_layers

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)

def get_model():
    inputs = complex_layers.complex_input(shape=(32, 32, 3))  #adjusted input dims to work with available data set
    c0 = complex_layers.ComplexConv2D(32, activation='cart_relu', kernel_size=3)(inputs)
    c1 = complex_layers.ComplexConv2D(32, activation='cart_relu', kernel_size=3)(c0)
    c2 = complex_layers.ComplexMaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(c1)
    t01 = complex_layers.ComplexConv2DTranspose(5, kernel_size=2, strides=(2, 2), activation='cart_relu')(c2)
    concat01 = tf.keras.layers.concatenate([t01, c1], axis=-1)

    c3 = complex_layers.ComplexConv2D(4, activation='cart_relu', kernel_size=3)(concat01)
    out = complex_layers.ComplexConv2D(4, activation='cart_relu', kernel_size=3)(c3)
    return tf.keras.Model(inputs, out)


# Assume you already have complex data... example numpy arrays of dtype np.complex64
(train_images, train_labels), (test_images, test_labels) = get_dataset()        # to be done by each user

model = get_model()   # Get your model

# Compile as any TensorFlow model
model.compile(optimizer='adam', metrics=['accuracy'], loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))
model.summary()

# Train and evaluate
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels), verbose=1)
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

load CVNN model with succes

For the save and the load of a CVNN model i use :

I attach an example for more explanation

1) For save 👍

#Build of the model
    
tf.random.set_seed(1)
init = cvnn.initializers.ComplexGlorotUniform()
acti = 'cart_relu'
model = tf.keras.models.Sequential()
model.add(complex_layers.ComplexInput(input_shape=input_shape))                     
model.add(complex_layers.ComplexConv2D(32, (3, 3),padding = 'same', activation=acti, kernel_initializer=init))
model.add(complex_layers.ComplexMaxPooling2D((2, 2)))
model.add(complex_layers.ComplexConv2D(64, (3, 3),padding = 'same', activation=acti, kernel_initializer=init))
model.add(complex_layers.ComplexFlatten())
model.add(complex_layers.ComplexDense(64, activation=acti, kernel_initializer=init))
model.add(complex_layers.ComplexDense(10, activation='convert_to_real_with_abs', kernel_initializer=init)) 
print(model.summary())

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
              loss=ComplexAverageCrossEntropy(),
              metrics=ComplexCategoricalAccuracy())
# train model
history = model.fit(X_train, y_train, epochs=10, validation_split=0.2, batch_size=32) 

# save the model by :
keras.models.save_model(model,"./models/model.hdf5")

2) For Load


#for the load it is necessary to add a custom_objects which contains all of complex objects used in the build of model
# for this exemple the load like 👍 

model = keras.models.load_model(
    "./models/model.hdf5",
    custom_objects={'Custom>Adam': keras.optimizer_experimental.adam.Adam,
                    'convert_to_real_with_abs': cvnn.activations.convert_to_real_with_abs,
                    'ComplexInput' :complex_layers.ComplexInput,
                    'ComplexConv2D' : complex_layers.ComplexConv2D,
                    'ComplexMaxPooling2D' :complex_layers.ComplexMaxPooling2D,
                    'ComplexFlatten': complex_layers.ComplexFlatten,
                    'ComplexDense': complex_layers.ComplexDense,
                    'ComplexAverageCrossEntropy' :cvnn.losses.ComplexAverageCrossEntropy ,
                    'ComplexCategoricalAccuracy' :cvnn.metrics.ComplexCategoricalAccuracy
                    }
    )

I hope that can help !

Model subclassing compatibility

I've been trying to get this to work with the model subclassing API, but for some reason, the first layer of the model always expects the data to be in float32. Any idea how to get this to work?

Best Activation Function in Complex Domain

Hi, I am working with CVNN. As you already have worked with CVNN please let me know the best activation we can use in CVNN to obtain better performance like ReLU work out in ResNet etc

ValueError: Unknown loss function:ComplexAverageCrossEntropy

【Succeed】from cvnn.losses import ComplexAverageCrossEntropy
【When using lossfunction:】loss='ComplexAverageCrossEntrop()'
【Error happens】ValueError: Unknown loss function:ComplexAverageCrossEntropy

When I use another loss function (Complex Mean Square Error), the same error happens.
Can you give me some advice to find the reason?
If I post two times of this issure, please ignore one or delete.
Thank you very much.
Frank

Return types for Complex Initializers

Hi,
I'm reading the coding details in cvnn.initializers and have a question about it. Do all complex initializers return the real value (dtype=float32) rather than complex value (dtype = complex64)? I have tried following codes and all of them give me real-valued vectors or matrices. If that's the case, do you have any way to initialize kernel weight matrix or bias with complex values?

import cvnn
initializer = cvnn.initializers.ComplexGlorotNormal()
values = initializer(shape=(2, 2))
print(values.numpy())

print(cvnn.initializers.Zeros()(shape=(2,2)))

Thanks for your help.
Cheers.

`fan_in` is None after a layer with `zrelu` activation

Hi @NEGU93 ,

Thanks again for keeping this package updated.

I have noticed an issue with the zrelu activation. The problem is that a layer (in sequential model) that goes after zrelu activation fails to initialize with an error saying that fan_in is of NoneType. It seems fine with other activation functions.

See here a short notebook reproducing the issue: https://gist.github.com/jonasdaugalas/e8a3c44055321f706081b4c4374597d6

Do you have an idea what could be wrong here?

I'm not getting complex valued output

For my thesis I want to do Music Source separation.

To do this, the input of my model is a complex output of the stft
The output should also be a predicted stft for a separate source.

I think this complex implementation should work. but when testing, I'm not able to get complex output.
Could you please help me? maybe even have a call because I'm really struggling with this project

my email is [email protected]

Can you provide the version of the package used? For example, tensorflow, numpy, etc.

Thank you very much for the code for complex valued neural networks. At present, there is no code for complex valued neural network supporting tensorflow. With the continuous updating of tensorflow, the version compatibility problem often exists in the program code based on tensorflow. However, version compatibility issues did not affect the popularity of tensorflow. This program can be uploaded to the official website of pypi to facilitate the spread and development of complex valued neural network, and also to improve the reputation of the author. Can you provide the version of the package used? For example, tensorflow, numpy, etc.

CVNN API 3D layers

Hi,

Can I just say firstly that I love you library sir? As I have been testing it on a pneumonia detection system, and it works beautifully, but I have also had a problem with attempting to make it work on 3D images.

Now to the best of my knowledge that maybe a problem with Tensor flow nightly rather than with CVNN, but I thought you should know about that.

Tom T.

P.S. I too have a very strong interest in machine learning algorithms of this type, and I am actively trying to do some research in the same area, only with Bayesian networks mostly rather than deep learning ones.

using this function layers.complex_input(shape=input_shape + (3,)) gives off dtype error

File /tmp/__autograph_generated_fileidpjfdij.py:205, in outer_factory..inner_factory..tf__call(self, inputs)
...
TypeError: Value passed to parameter 'features' has DataType complex64 not in list of allowed values: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, float16, uint32, uint64, qint8

Call arguments received by layer "complex_conv2d_9" (type ComplexConv2D):
• inputs=tf.Tensor(shape=(None, 12, 12, 512), dtype=complex64)

But using layers.ComplexInput does not but in unet layer it gives off an error

TypeError: Inputs to a layer should be tensors. Got '' (of type ) as input for layer 'complex_conv2d_10'.

Please help

Pytorch implementation

Hi again Jose,

With the recent developments of Pytorch support for complex valued data, is it worth to migrate to Pytorch style of implementation? Even though Pytorch doest not have the detail and custom function of your library (imag_to_real softmax), have you tried/is it possible to build similar architectures in Pytorch (simple FFNs im my case)? Is there advantages in using Pytorch that you do not have in Keras?

Best,
Tomás

Killed!!

When I run your test example my python kernel is "killed" when i run it on Ubuntu 16.04.

It works on Mac though... Do you know what the problem is?

Error: Inputs to a layer should be tensors. Got: <cvnn.layers.core.ComplexInput object at ...>

Hi @NEGU93
First of all thank you for this great library :)

I'm experiencing an error on a model; in particular, I have these two lines

input_users = complex_layers.ComplexInput(input_shape=(dim_embeddings))
x1 = complex_layers.ComplexDense(512, activation="cart_relu")(input_users)

The last line raises the following TypeError: Inputs to a layer should be tensors. Got: <cvnn.layers.core.ComplexInput object at 0x0000012D118F4FD0>

I understood that the input for this model must be ComplexInput (that's why I'm using it), but I have no idea about how to face this issue. Can you please help me? :)

Edit: solved using
input_users = complex_layers.complex_input(shape=(dim_embeddings,))

ComplexUpSampling2D is too slow compiling

Hello!

There are a problem with ComplexUpSampling2D: Is too slow (overmuch) compiling.

For now, to skip this problem I am doing something like this:

def CUpsampling2D(x):
r = UpSampling2D((2, 2))(tf.math.real(x))
i = UpSampling2D((2, 2))(tf.math.imag(x))
return tf.dtypes.complex(r, i, name=None)

But if I have time, I could calmly review to see how it can be solved.

Thank you for your code. It is being very useful for my master. :-)

ComplexConv2D with bias vector slows down training a lot

If use_bias is set to True with ComplexConv2D (i.e. the default) the required time to train will be 2-3 times longer than if no bias is used. With keras real-valued Conv2D the difference due to bias is basically none.

Is that to be expected?

I observed that cpu-usage is a bit higher when enabling bias, but allocated gpu-memory is the same for both cases.

See below for a simple example.

import numpy as np
import tensorflow as tf
import cvnn.layers

n_samples = 10000
data_shape = (n_samples, 128, 256, 2)
input_shape = data_shape[1:]

data = np.csingle(np.random.rand(*data_shape) + 1j*np.random.rand(*data_shape))
labels = np.float32(np.random.rand(n_samples))

use_bias = True # True increases train time by a factor 2-3

model = tf.keras.models.Sequential([
    cvnn.layers.ComplexInput(input_shape=input_shape, dtype=np.complex64),
    cvnn.layers.ComplexConv2D(8, (5, 5), activation='cart_relu', use_bias=use_bias),
    cvnn.layers.ComplexFlatten(),
    cvnn.layers.ComplexDense(1, activation='convert_to_real_with_abs')
])

print("Total size: {} MB".format((data.nbytes+labels.nbytes)/1_000_000))

model.compile(optimizer=tf.optimizers.Adam(learning_rate=1e-04), loss='mean_squared_error', metrics=[tf.keras.metrics.RootMeanSquaredError()])

model.summary()

model.fit(data, labels, epochs=5, verbose=2)

Equivalent Data PreProcessing for complex-valued input

Hi again Jose,

Apologies if this questions is not directly code related but it might be relevant considering some classification examples that I saw in your docs. In real-valued NNs we often use data pre-processing (e.g., skleanr's StandardScaler ). In case we have some complex-valued data, e.g., backscatter parameter such as S11 from a VNA (because I think your familiarized with it 😅), should we also normalize it (despite it already being between 0-1)? Also how do we normalize the complex input in these cases, is it the same as using complex batch norm layer before inputting onto the network?

I previously read your excellent paper (Impact of PolSAR Pre-Processing...) but I dont think it discusses this aspect (I'll re-read it still).

Thanks in advance!
Tomás

cifar40

image

When i directly run this file, it always shows this error. Any ideas on this? Thanks.
The python file is in cvnn-master\examples\cifar410_example.py

A 1DPooling layer would be great !

Dear Barrachina, congratulations once again for your library

It would be so useful whether the CVNN library could also have a 1DPooling layer for 1D problems that arise.

Could you provide that, please, when you have a chance ? Thanks in advance

examples/cv-cnn.py is broken

It says

from cvnn.layers import Convolutional, MaxPooling, Flatten, Dense, AvgPooling
ImportError: cannot import name 'Convolutional' from 'cvnn.layers' (C:\Users\Envoy\anaconda3\lib\site-packages\cvnn\layers.py)

and when I checked the layers file I couldn't see any conv class

"WARNING:tensorflow: You are casting an input of type complex64 to an incompatible dtype float32. This will discard the imaginary part and may not be what you intended."

The issue I have encountered while using the CVNN library is that I have received warning messages indicating that there is a mismatch between the data types of the inputs to certain operations in my code.

"WARNING:tensorflow: You are casting an input of type complex64 to an incompatible dtype float32. This will discard the imaginary part and may not be what you intended."

It seems like the issue may be related to using complex-valued inputs with operations that expect real-valued inputs. But this library is intended to work with complex data types that is what the goal is. Any idea how can I fix this problem?
dd

The attached image is the output of CIFAR 10 examples. https://complex-valued-neural-networks.readthedocs.io/en/latest/code_examples/cifar10.html

I would appreciate any kind of help I receive.

Custom Activation Functions with tensorflow 2.8.2

Hello! Recently there was an update of tensorflow from 2.8.0 to 2.8.2 on google collab and since then my codes involving custom activation functions stopped working properly (it works fine after reinstalling older version of tensorflow).

The error i get is the following: ("c_modu" is the new activation function i added in cvnn/activations.py)

image
Any input on why that is would be greatly appreciated!

Installing with anaconda/spider

Hello,

I came here after reading your article: Complex-Valued Vs. Real-Valued Neural Networks for Classification Perspectives: An Example on Non-Circular Data and I think that your CVNN implementation would be extremely helpfull in my research. However, when I tried to install through Anaconda, I get compatibility issues with the spider and/or tensorflow library. I tried the following situations:

  1. Installing with library through the conda command in an active environment (with tensorflow)
  2. Creating a new environment, start by installing your library with the conda command, and proceeding to install spider and the remaining (necessary) libraries.

In the first situation, after dealing with the conflits, I get the following error when running your example code:

image

In the second situation, I am unable to install tensorflow as anaconda is unable to deal with the conflits (?)

image

Any chance you could help me figuring out the issue or pointing out a different solution?

Thanks alot in advance!

*Edit
PS: the code:

-- coding: utf-8 --

"""
Created on Mon Sep 27 17:24:47 2021

@author: tomas
"""

import numpy as np
from scipy.io import loadmat
import matplotlib.pyplot as plt

import tensorflow as tf
import cvnn.layers as complex_layers

=============================================================================

freq_range = loadmat('freq_range.mat')['freq_range']

#X = loadmat('X.mat')['X']

Y = loadmat('Y.mat')['Y']

=============================================================================

input_shape = (4, 28, 28, 3)
x = tf.cast(tf.random.normal(input_shape), tf.complex64)

model = tf.keras.models.Sequential()
model.add(complex_layers.ComplexInput(input_shape=input_shape[1:]))
model.add(complex_layers.ComplexFlatten())
model.add(complex_layers.ComplexDense(units=64, activation='cart_relu'))
model.add(complex_layers.ComplexDense(units=10, activation='linear'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

y = model(x)

Terrible slow caused by ComplexBatchNormalization()

Hi there, @NEGU93. Thanks for the great effort in making this library. It really accelerate my research in signal recognition task. This TF 2.0 version indeed help me deploy in the edge device with the help of TFlite. However, I found ComplexBatchNormalization() will terribly slow down the training process. Give one example to reproduce:

import numpy as np
from tensorflow.keras.models import Model
import tensorflow
from cvnn.layers import ComplexConv1D, ComplexInput, ComplexDense, ComplexBatchNormalization, ComplexFlatten, complex_input

X_train = np.random.rand(18000, 4096, 2)
Y_train = np.random.randint(0, 9, 18000)
X_test = np.random.rand(2000, 4096, 2)
Y_test = np.random.randint(0, 9, 2000)

inputs = complex_input(shape=X_train.shape[1:])
outs = inputs
outs = (ComplexConv1D(16, 6, strides=1, padding='same', activation='cart_relu'))(outs)
outs = (ComplexBatchNormalization())(outs)

outs = (ComplexConv1D(32, 3, strides=1, padding='same', activation='cart_relu'))(outs)
outs = (ComplexBatchNormalization())(outs)

outs = (ComplexFlatten())(outs)
DL_feature = (ComplexDense(128, activation='cart_relu'))(outs)
outs = (ComplexDense(256, activation='cart_relu'))(DL_feature)
outs = (ComplexDense(256, activation='cart_relu'))(outs)
predictions = (ComplexDense(, activation='cast_to_real'))(outs)

model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer=tensorflow.keras.optimizers.Adam(learning_rate=1e-4),
              loss=tensorflow.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(X_train, Y_train, batch_size=32, epochs=3, verbose=1, validation_data=(X_test, Y_test),
                    callbacks=[checkpoint, earlystopping, learn_rate])

It almost cost me 10 mins to train one epoch. But, when I substitute ComplexBatchNormalization() to BatchNormalization(), it only costs me half min. Any ideas?

Type type error for "ComplexInput"

When I use:

model.add(complex_layers.ComplexInput(input_shape=(input_number,), dtype=np.complex64))
where "input_number=4", it says:

ValueError: Invalid dtype: complex64

Isn't it supposed to get complex inputs or I am missing something?
Thanks for the help.

cifar40_new issue

Hi,
I got a new question related to this work.
I'm trying to use complex inputs by changing dtype=np.complex64:
train_images, test_images = train_images.astype(dtype=np.complex64) / 255.0, test_images.astype(dtype=np.complex64) / 255.0
And I also modify activation = 'crelu', kernel_initializer='ComplexGlorotUniform', init_technique='mirror', as well as model.compile(optimizer='sgd', loss=losses.ComplexAverageCrossEntropy(), metrics=metrics.ComplexAccuracy()). But it encountor this error:
image

The complete modified codes are copied as follows:

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import cvnn.layers as complex_layers
import numpy as np
from cvnn import losses
from cvnn import metrics
from pdb import set_trace
from importlib import reload
import os
import tensorflow
from matplotlib import pyplot as plt

def own_complex_fit(epochs=10):
    tf.random.set_seed(1)
    init = 'ComplexGlorotUniform'
    acti = 'crelu'
    init_tech = 'mirror'
    model = models.Sequential()
    model.add(complex_layers.ComplexConv2D(32, (3, 3), activation=acti, input_shape=(32, 32, 3),
                                           kernel_initializer=init, use_bias=False, init_technique=init_tech))
    model.add(complex_layers.ComplexMaxPooling2D((2, 2)))
    model.add(complex_layers.ComplexConv2D(64, (3, 3), activation=acti, kernel_initializer=init,
                                           use_bias=False, init_technique=init_tech))
    model.add(complex_layers.ComplexMaxPooling2D((2, 2)))
    model.add(complex_layers.ComplexConv2D(64, (3, 3), activation=acti, kernel_initializer=init,
                                           use_bias=False, init_technique=init_tech))
    model.add(complex_layers.ComplexFlatten())
    model.add(complex_layers.ComplexDense(64, activation=acti, kernel_initializer=init,
                                          use_bias=False, init_technique=init_tech))
    model.add(complex_layers.ComplexDense(10, activation=acti, kernel_initializer=init,
                                          use_bias=False, init_technique=init_tech))
    print(model.summary())
    
    
    model.compile(optimizer='sgd', 
                  loss=losses.ComplexAverageCrossEntropy(),
                  metrics=metrics.ComplexAccuracy())
   
    weigths = model.get_weights()
    with tf.GradientTape() as tape:
        loss = model.compiled_loss(y_true=tf.convert_to_tensor(test_labels), y_pred=model(test_images))
        gradients = tape.gradient(loss, model.trainable_weights)  # back-propagation
    history = model.fit(train_images, train_labels, epochs=epochs, validation_data=(test_images, test_labels))
    test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
    logs = {
        'weights_at_init': weigths,
        'loss': loss,
        'gradients': gradients,
        'weights_at_end': model.get_weights()
    }
    return history, logs

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images.astype(dtype=np.complex64) / 255.0, test_images.astype(dtype=np.complex64) / 255.0

reload(tensorflow)
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
own, own_logs = own_complex_fit(epochs=5)
history = own
print(history.history.keys())
#  "Accuracy"
plt.plot(history.history['complex_accuracy'])
plt.plot(history.history['val_complex_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# "Loss"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()

If I change the code model.add(complex_layers.ComplexDense(10, activation=acti, kernel_initializer=init, use_bias=False, init_technique=init_tech)) to model.add(complex_layers.ComplexDense(1, activation=acti, kernel_initializer=init, use_bias=False, init_technique=init_tech)), the error disappears but the train and test accuracy will continue to 0 without learning. Actually it does not make any sence to simply change 10 to 1 since the number of classes for this example is 10. I'm wondering what should I do to debug this issue properly. Many thanks for your help.

Originally posted by @annabelleYan in #16 (comment)

Best way to convert Real data into complex data type

Hi, As you already have worked on CVNN wanted to know your opinion about the following.
I wanted to know the best way to convert real data into complex as just having 0 imaginary parts doesn't seem right and the performance boost isn't great either. Also, vice versa, as just neglecting the imaginary part doesn't we lose information?

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).

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))

Complex-valued labels?

Hi Barrachina,

Thank you for providing the CVNN library.

I would like to train complex-valued datasets with complex-valued labels.
Is that possible?
If yes, please could you explain how?

Implement complex-valued constraint parameter

Hello @NEGU93, thank you for making this library! I'm attempting to use the arguments "use_bias" and "kernel_constraint" within "ComplexDense" as so:

self.d1 = complex_layers.ComplexDense(dim, use_bias=False, kernel_constraint=ConstantWeights())

However, these arguments seem to have no effect -- there are still biases, and the constraints on the weights are not enforced. That is, the layer performs identically to this:

self.d1 = complex_layers.ComplexDense(dim)

I tried the same arguments with tf.keras.layers.Dense and got the desired behaviors. Please let me know if I misunderstand how to use these. Thanks in advance!

Here's the relevant context, in case it helps:

class ConstantWeights(tf.keras.constraints.Constraint):
    def __call__(self, w):
        tf.keras.backend.set_value(w[0, 0], 3)
        tf.keras.backend.set_value(w[1, 2], 3)
        return w

class MyModel(Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.d1 = complex_layers.ComplexDense(dim, use_bias=False, kernel_constraint=ConstantWeights())

    def call(self, x):
        x = self.d1(x)
        return x

model = MyModel()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.