I have complex input to into the neural network, and I also need the neural network to have complex weights, but when writing the code, I get the error as below:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-99efde09531b> in <cell line: 20>()
18
19 # Instantiate the model with the corrected input parameter
---> 20 model = get_complex_model((10,))
21
22 # Compile the model
/usr/local/lib/python3.10/dist-packages/keras/src/activations/__init__.py in get(identifier)
102 if callable(obj):
103 return obj
--> 104 raise ValueError(
105 f"Could not interpret activation function identifier: {identifier}"
106 )
ValueError: Could not interpret activation function identifier: cart_relu
I also tried to use the standard activation function relu
instead of cart_relu
, but it also gives an error. Below is the code I use:
import numpy as np
from cvnn.layers import ComplexDense, ComplexInput
import tensorflow as tf
data = np.random.rand(1000, 10) + 1j * np.random.rand(1000, 10) # Generate synthetic complex-valued data
labels = (np.abs(data).sum(axis=1) > 5).astype(int)
def get_complex_model(input_shape):
# Ensure that the shape argument is provided correctly to ComplexInput
inputs = tf.keras.Input(shape=(10,))
x = ComplexDense(10, activation='cart_relu')(inputs)
model = tf.keras.Model(inputs=inputs, outputs=x)
return model
# Instantiate the model with the corrected input parameter
model = get_complex_model((10,))
def tensorflow_model():
import numpy as np
import tensorflow as tf
from cvnn.layers import ComplexDense, ComplexInput
data = np.random.rand(1000, 10) + 1j * np.random.rand(1000, 10)
labels = (np.abs(data).sum(axis=1) > 5).astype(int)
def get_complex_model(input_shape):
model_ = tf.keras.models.Sequential()
model_.add(ComplexInput(input_shape=input_shape))
model_.add(ComplexDense(50, activation='cart_relu'))
model_.add(ComplexDense(1, activation='convert_to_real_with_abs'))
return model_
model = get_complex_model((10,))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(data, labels, epochs=10, batch_size=32, validation_split=0.2)
test_data = np.random.rand(200, 10) + 1j * np.random.rand(200, 10)
test_labels = (np.abs(test_data).sum(axis=1) > 5).astype(int)
test_loss, test_acc = model.evaluate(test_data, test_labels)
print(f'Test accuracy: {test_acc}')
if __name__ == '__main__':
tensorflow_model()
Output: Test accuracy: 1.0
Environment:
Please note that cvnn is experimental and not maintained, so it might not work with newer versions of Tensorflow. See Invalid dtype: complex64 with TF 2.16