使用张量流 Conv1D:如何解决错误"Input 0 of layer " conv1d_9 " is incompatible with the layer: "?



我正在使用TensorFlow对我模拟的超声波信号进行二进制分类,我想使用CNN。我是编程和机器学习的新手,所以我不知道我使用的术语是否正确,请耐心等待。数据被组织成一个名为"sig_data"的数组,其中列是时间步长,行是不同的信号样本。这些值是信号的振幅。标签位于另一个名为"sig_id"的1D数组中,该数组包含值1和0。数据的形状如下:

data shape: (1000, 1000)
label shape: 1000

我已经将数据放入TF数据集,并将其分为训练集、验证集和测试集:

data_ds = tf.data.Dataset.from_tensors((sig_data, sig_id))
train_ds = data_ds.take(700)
val_ds = data_ds.skip(700).take(200)
test_ds = data_ds.skip(900).take(100)
train_ds = train_ds.shuffle(shuffle_buffer_size).batch(batch)
val_ds = val_ds.shuffle(shuffle_buffer_size).batch(batch)
test_ds = test_ds.batch(batch)

我创建的模型是:

model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(1000,1)),
tf.keras.layers.Conv1D(50, 3, activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(
train_ds,
validation_data=val_ds,
batch_size=batch,
epochs=25)

我得到以下错误:

ValueError: Exception encountered when calling layer "sequential_3" (type Sequential).

Input 0 of layer "conv1d_3" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received input with shape (None, 1000, 1000)

我已经查找了这个问题,试图解决它。我认为问题出在输入形状上,所以我试图按照以下方式重塑我的数组:

sig_data_reshaped = np.expand_dims(sig_data, axis=-1)
sig_id_reshaped = np.expand_dims(sig_id, axis=-1)
reshaped data shape: (1000, 1000, 1)
reshaped label shape: (1000, 1)

但当我运行代码时,我仍然会收到一个错误,

Input 0 of layer "conv1d_8" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received input with shape (None, 1000, 1000)

我的错误是由于我如何组织数据集造成的吗?为什么当我将阵列重塑为3D时,它仍然会给我一个错误?

数据集data_ds包含单个形状记录(10001000(。您可以尝试使用from_sensor_slices创建它。

data_ds = tf.data.Dataset.from_tensor_slices((sig_data, sig_id))

相关内容

  • 没有找到相关文章

最新更新