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

TypeError: Layer input_spec must be an instance of InputSpec. #51

Open minwg1021 opened 4 months ago

minwg1021 commented 4 months ago
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)

(train_images, train_labels), (test_images, test_labels) = get_dataset()        # to be done by each user

print(train_images)

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='relu'))
model.add(complex_layers.ComplexAvgPooling2D((2, 2)))
model.add(complex_layers.ComplexConv2D(64, (3, 3), activation='relu'))
model.add(complex_layers.ComplexMaxPooling2D((2, 2)))
model.add(complex_layers.ComplexConv2D(64, (3, 3), activation='relu'))
model.add(complex_layers.ComplexFlatten())
model.add(complex_layers.ComplexDense(64, activation='relu'))
model.add(complex_layers.ComplexDense(10, activation='softmax'))
model.compile(optimizer='adam',
        loss='categorial_crossentropy',
        metrics=['accuracy'])
model.summary()

Hello! I have a question about the Input Spec. The code above is the example code provided in the Doc. When I run the code, I get the following error: TypeError: Layer input_spec must be an instance of InputSpec. Got: InputSpec(min_ndim=4) Is there a way to resolve this issue?

I will be waiting for your response. Thank you!

NEGU93 commented 4 months ago

Hello, I don't see any input_spec layer in your code. Can you provide the full error message trace?

minwg1021 commented 4 months ago
Traceback (most recent call last):
  File "d:\Research\extra\src\DL\AutoEncoder\[2024-06-04]CVNN_ae\tmp.py", line 19, in <module>
    model.add(complex_layers.ComplexConv2D(32, (3, 3), activation='relu'))
  File "C:\Users\USER\anaconda3\envs\cvnn\lib\site-packages\cvnn\layers.py", line 717, in __init__
    super(ComplexConv2D, self).__init__(
  File "C:\Users\USER\anaconda3\envs\cvnn\lib\site-packages\cvnn\layers.py", line 329, in __init__
    self.input_spec = InputSpec(min_ndim=self.rank + 2)
  File "C:\Users\USER\anaconda3\envs\cvnn\lib\site-packages\keras\engine\base_layer.py", line 3075, in __setattr__
    super(tf.__internal__.tracking.AutoTrackable, self).__setattr__(
  File "C:\Users\USER\anaconda3\envs\cvnn\lib\site-packages\tensorflow\python\trackable\base.py", line 205, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\USER\anaconda3\envs\cvnn\lib\site-packages\keras\engine\base_layer.py", line 1243, in input_spec
    raise TypeError(
TypeError: Layer input_spec must be an instance of InputSpec. Got: InputSpec(min_ndim=4)

Thank you for your response! The error message shown above is the entire error message trace.