Ошибка значения: ввод 0 последовательного слоя несовместим со слоем :: ожидаемый min_ndim=3, найден ndim=2. Получена полная форма: (10, 24)

#python #tensorflow #keras #conv-neural-network

Вопрос:

 import numpy as np
import pandas as pd
from numpy.random import seed
import tensorflow as tf
from tensorflow import keras
from keras import Sequential
from keras.layers import Dense, Conv1D, MaxPooling2D, Activation
from sklearn.model_selection import train_test_split

seed(1)
tf.random.set_seed(2)
droprate = 0.5

dataset = pd.read_csv('filecounts.csv')
data = np.array(pd.get_dummies(dataset['counts']))

model = Sequential()

model.add(Conv1D(8, kernel_size=3, padding="same", activation="relu",input_shape=(12, 12, 10)))
model.add(MaxPooling2D(pool_size=2))
...
model.add(Conv1D(4, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=2))
...
model.add(Conv1D(1, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=2))
...
model.add(Activation("softmax"))

sgd = keras.optimizers.SGD(learning_rate=1)

train, test = train_test_split(data, test_size=0.5)

model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train, epochs=100, batch_size=10)
_, accuracy = model.evaluate(test, verbose=0, steps=1)

print('Accuracy: %.2f' % (accuracy*100))
 

Комментарии:

1. Пожалуйста, поделитесь полным треком ошибок. Неясно, происходит ли это при обучении или тестировании или при чтении данных.

2. Какова форма train ?

3. drive.google.com/drive/my-drive?hl=id @alift вот полная ошибка

Ответ №1:

Conv1D ожидает ввода тензора формы 3 D с shape: batch_shape (steps, input_dim) и вывода тензора формы 3 D с shape: batch_shape (new_steps, filters) с или без padding='same' .

Ошибка связана с тем, что MaxPooling2D ожидает 4D тензора с формой (batch_size, rows, cols, channels) .

Рабочий пример кода

 import tensorflow as tf
import numpy as np
import tensorflow.keras as keras

X_train = np.random.random((12,12,10))
y_train = np.random.random((12, 1))

model = tf.keras.Sequential()

model.add(keras.layers.Conv1D(8, kernel_size=3, padding="same", activation="relu",input_shape=(12, 10)))
model.add(keras.layers.MaxPool1D(pool_size=2))

model.add(keras.layers.Conv1D(4, kernel_size=3, padding="same", activation="relu"))
model.add(keras.layers.MaxPool1D(pool_size=2))

model.add(keras.layers.Conv1D(1, kernel_size=3, padding="same", activation="relu"))
model.add(keras.layers.MaxPool1D(pool_size=2))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units = 128, activation = 'relu'))
model.add(keras.layers.Dense(units = 1, activation = 'softmax'))

model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])

model.fit(X_train, y_train, epochs = 15)
 

Вывод

 Epoch 1/10
1/1 [==============================] - 1s 944ms/step - loss: 0.6914 - accuracy: 0.0000e 00
Epoch 2/10
1/1 [==============================] - 0s 9ms/step - loss: 0.6900 - accuracy: 0.0000e 00
Epoch 3/10
1/1 [==============================] - 0s 9ms/step - loss: 0.6885 - accuracy: 0.0000e 00
Epoch 4/10
1/1 [==============================] - 0s 7ms/step - loss: 0.6870 - accuracy: 0.0000e 00
Epoch 5/10
1/1 [==============================] - 0s 8ms/step - loss: 0.6856 - accuracy: 0.0000e 00
Epoch 6/10
1/1 [==============================] - 0s 9ms/step - loss: 0.6841 - accuracy: 0.0000e 00
Epoch 7/10
1/1 [==============================] - 0s 8ms/step - loss: 0.6828 - accuracy: 0.0000e 00
Epoch 8/10
1/1 [==============================] - 0s 14ms/step - loss: 0.6814 - accuracy: 0.0000e 00
Epoch 9/10
1/1 [==============================] - 0s 7ms/step - loss: 0.6801 - accuracy: 0.0000e 00
Epoch 10/10
1/1 [==============================] - 0s 11ms/step - loss: 0.6789 - accuracy: 0.0000e 00
<keras.callbacks.History at 0x7eff56169810>