簡體   English   中英

為什么在嘗試運行 Conv1D 層時會出現 Conv2D 錯誤?

[英]Why do I get a Conv2D error trying to run Conv1D layer?

我正在嘗試編寫一個簡單的一維卷積與回歸(一維浮點數)output。

model = Sequential()
model.add(Conv1D(filters=1, kernel_size=8, activation='relu'))
model.add(Dense(1, 'softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x = x_train, y = y_train, epochs=3)

這給了我錯誤:

TypeError: Exception encountered when calling layer "conv1d" (type Conv1D).
Input 'filter' of 'Conv2D' Op has type float32 that does not match type int32 of argument 'input'.
Call arguments received:
• inputs=tf.Tensor(shape=(None, 30931, 4), dtype=int32)

即使我的代碼是錯誤的,我怎么可能在沒有 Conv2D 層的情況下收到 Conv2D 錯誤?

x_train 是一個包含 3361 個訓練示例的 numpy 數組,每個一維數組的長度為 30931,具有 4 個通道的 np.int32 數據。 形狀 = (3361,30931, 4)

y_train 是一個 numpy 數組,包含 3361 個 np.float64 值,我正在訓練 my.network 進行識別。

這種輸入數據格式應該有效嗎? 還是我需要對其進行轉換或使用其他數據類型?

我的 Conv1D 層中是否需要 input_shape 參數? 如果是這樣,它應該是什么?

我意識到這過於簡單化了,並計划了一個更復雜的網絡來訓練更多的例子,但只想先運行它。

您的x_train數據應該是數據類型float 此外,您通常會將 2D 數據展平為 1D 或應用一些全局池化操作,然后再將其送入softmax output 層:

import tensorflow as tf

x_train = tf.random.normal((10,30931, 4), dtype=tf.float32)
y_train = tf.random.uniform((10,), maxval=2, dtype=tf.int32)
y_train = tf.keras.utils.to_categorical(y_train, 2)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(filters=1, kernel_size=8, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(2, 'softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x = x_train, y = y_train, epochs=3)

關於您的錯誤信息, Conv1DConv2D層都在內部使用了tf.nn.convolution操作。 有趣的是,問題是由參數filter引起的,它具有 float 數據類型,無法處理 integer 輸入。

暫無
暫無

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

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