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
160 stars 33 forks source link

Installing with anaconda/spider #15

Closed SantaTitular closed 2 years ago

SantaTitular commented 2 years ago

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)

NEGU93 commented 2 years ago

Dear @SantaTitular,

First of all, thank you for using my library and your interest in my work. It is really appreciated.

I believe the issue is that the output activation function (linear) just outputs a complex number, however, categorical cross-entropy is only defined for real numbers. Here you have 2 options:

  1. Use an activation function that although receiving a complex number outputs a real one. Documentation here
  2. Or use a loss function that supports complex numbers. Currently I only implemented one of those and it's here

Hope it helps! let me know if the issue is solved.

SantaTitular commented 2 years ago

Dead @NEGU93 ,

Thanks alot for the response!

Although I did manage to run the code, my problem was the implementation of the library in the anaconda environment. For instance, I replicated the example that you mentioned here, and I got several module errors (e.g., ModuleNotFoundError: No module named 'cvnn.losses' ). Afterwards I just imported the cvnn library (which I installed with conda install -c negu93 cvnn ) and I only have the following modules:

image

Do you think that this might be an environment issue or an incompatibility with some version of spyder, tensorflow, numpy, etc.? I tried reinstalling and ran into several conflicts between libraries and still could not have some of the modules. Am I doing anything wrong?

Thanks in advance!

NEGU93 commented 2 years ago

Conda version is outdated and does not have the losses module. I recommend you install cvnn with pip (within the conda environment, there is no problem with that).

SantaTitular commented 2 years ago

All your suggestions worked wonderfully, thank you so much, your library will be a tremendous help!

Edit: Feel free to close the thread!

NEGU93 commented 2 years ago

Great! I'm very glad I could help. I will then close the issue.