-1

我正在尝试np.load()使用tf.data管道从 .npy 文件中读取我的 X 和 y 数据。但是如果我打电话会得到以下错误model.fit()。有人解决这个问题吗?我想我必须将 X_data 和 y_data 的形状赋予tf.py_funciton. 我正在使用 TensorFlow 2.4

错误:

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

描述:

train_filenames,train_label_filenames 是包含每个 .npy 文件的文件路径的列表。

所以那个print(train_filenames[0])节目是'E:\UserData\Mustermann\02_Data\X_Data\Sample0.npy' _np.load(train_filenames[0]).shape(12, 8002)
np.load(label_filenames[0]).reshape(-1,1).shape(1, 1)

所以一个样本有 12 个时间步长和 8002 个特征。

代码:

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)
4

1 回答 1

0

据我所知,加载 .npy 文件没有问题,因为错误表明模型存在问题。

在您的第一层中,您正在使用参数return_sequences=True,这实际上是问题所在。这个参数返回给你 a3-D Array并且与下一层不兼容。我将通过删除该参数来尝试一下。

于 2021-01-14T21:02:29.480 回答