I need to write complex-value neural network in tensorflow but I get an error

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---------------------------------------------------------------------------
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
</code>
<code>--------------------------------------------------------------------------- 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 </code>
---------------------------------------------------------------------------

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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,))
</code>
<code>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,)) </code>
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,))

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
</code>
<code>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() </code>
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:

  • python==3.11.1
  • cvnn==2.0
  • tensorflow==2.15.0
  • tf_keras==2.15.1
  • tensorflow-probability[tf]==0.23.0
  • dm-tree==0.1.8

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật