簡體   English   中英

使用 tensorflow Conv1D:如何解決錯誤“層“conv1d_9”的輸入 0 與層不兼容:”?

[英]Using tensorflow Conv1D: how can I solve error "Input 0 of layer "conv1d_9" is incompatible with the layer: "?

我正在使用 TensorFlow 對我模擬的超聲波信號進行二進制分類,我想使用 CNN。 我是編程和機器學習的新手,所以我不知道我使用的術語是否正確,請耐心等待。 數據被組織成一個名為“sig_data”的數組,其中列是時間步長,行是不同的信號樣本。 這些值是信號的幅度。 標簽位於另一個名為“sig_id”的一維數組中,其中包含值 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是:

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)

我已經查過這個試圖解決它。 我認為問題出在輸入形狀上,所以我嘗試按如下方式重塑我的 arrays:

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)

我的錯誤是由於我組織數據集的方式造成的嗎? 為什么我把arrays改造成3D還是報錯?

數據集data_ds包含形狀為 (1000, 1000) 的單個記錄。 您可以嘗試使用from_tensor_slices創建它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM