-1

I'm trying to read my X and y Data from .npy files with np.load() in a tf.data pipeline. But get the following error if i call model.fit(). Have someone a soloution for that problem? I thought i have to give the shape of X_data and y_data to the tf.py_funciton. I am using Tensorflow 2.4

Error:

Input 0 of layer sequential_13 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, None)

Description:

train_filenames,train_label_filenames are lists with filepaths to each .npy file.

So that print(train_filenames[0]) show 'E:\UserData\Mustermann\02_Data\X_Data\Sample0.npy' and np.load(train_filenames[0]).shape is (12, 8002)
np.load(label_filenames[0]).reshape(-1,1).shape is (1, 1)

So one sample have the length of 12 timesteps and 8002 features.

Code:

def load_files_py(train_filenames, train_label_filenames):
   
   X_data = np.load(train_filenames)
   label = np.load(train_label_filenames).reshape(-1,1)
   
   return X_data, label, X_data.shape, label.shape
def parse_function(train_filenames, train_label_filenames):
    
    temp = tf.py_function(load_files_py, inp=[train_filenames, train_label_filenames], Tout=[tf.float32, tf.float32, tf.int32, tf.int32])
    X_data = tf.reshape(temp[0], [temp[2]])
    label = tf.reshape(temp[1], [temp[3]])
    return X_data, label
batch_size = 64

train_dataset = tf.data.Dataset.from_tensor_slices((train_filenames,train_label_filenames))
train_dataset = train_dataset.shuffle(len(train_filenames))
train_dataset = train_dataset.map(parse_function, num_parallel_calls=2)
train_dataset = train_dataset.batch(batch_size)
train_dataset = train_dataset.prefetch(1)

test_dataset = tf.data.Dataset.from_tensor_slices((test_filenames,test_label_filenames))
test_dataset = test_dataset.shuffle(len(test_filenames))
test_dataset = test_dataset.map(parse_function, num_parallel_calls=2)
test_dataset = test_dataset.batch(batch_size)
test_dataset = test_dataset.prefetch(1)
model = tf.keras.models.Sequential([
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16, input_shape = (12, 8002), return_sequences=True)),
    tf.keras.layers.LSTM(16),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(8, activation='relu'),
    tf.keras.layers.Dense(4, activation='relu'),
    tf.keras.layers.Dense(1, activation = 'linear')   
]) 
model.compile(optimizer='adam', loss='mse')

EPOCHS =300

early = tf.keras.callbacks.EarlyStopping('val_loss', patience=20)
history = model.fit(train_dataset,
                    epochs=EPOCHS,
                    validation_data = test_dataset)
pawello2222
  • 46,897
  • 22
  • 145
  • 209

1 Answers1

0

From what I can see, there is no issue with loading the .npy file as the error suggests that there is issue with the model.

In your first layer you are using the argument return_sequences=True and that is actually the issue. This argument returns you a 3-D Array and that is incompatible with the following layer. I will give it a try by removing that parameter.

pratsbhatt
  • 1,498
  • 10
  • 20
  • Thanks for your suggestion. I have tried it, but the error remains. As far as I know I need return_sequences = True, because after the BiLSTM follows a LSTM layer, which needs several hidden states from the BiLSTM layer. – Tensorlearner Jan 15 '21 at 07:31
  • could you paste your code in google colab so that I can run the complete code with the data to figure out what might be causing it? it is difficult to judge without running the code. – pratsbhatt Jan 15 '21 at 12:15
  • 1
    I have created a notebook that uses sample data where the same error exists. https://colab.research.google.com/drive/1TAqcFlwwDlvZCvC4_SXoTBJ8CxC4qXJN?usp=sharing – Tensorlearner Jan 18 '21 at 08:35