I've a problem preparing my dataset to feed a 1D CNN.
My CSV has 3025 cols representing a single byte + last col as string label.
Maybe is not a problem of preprocessing but on my network model.
This is my model:
def cnn_1d(num_classes):
model = models.Sequential()
model.add(Conv1D(16, kernel_size=3, strides=1, activation="relu", input_shape=(3025, 1)))
model.add(Conv1D(16, kernel_size=3, strides=1, activation="relu"))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.2))
model.add(Conv1D(32, kernel_size=3, strides=1, activation="relu"))
model.add(Conv1D(32, kernel_size=3, strides=1, activation="relu"))
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(0.2))
model.add(Dense(500, activation="relu"))
model.add(Dense(300, activation="relu"))
model.add(Dense(num_classes, activation="softmax"))
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss="categorical_crossentropy",
metrics=["accuracy"],
)
model.summary()
return model
This is what I tried to preprocess data:
num_classes = 4
df = pd.read_csv("test.csv", header=0)
df["label"] = pd.Categorical(df["label"])
df["label"] = df.label.cat.codes
Y = df.pop("label")
X = df.copy()
x_train, x_test, y_train, y_test = train_test_split(np.asarray(X), np.asarray(Y), test_size=0.33, shuffle=True)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
model = cnn_1d(num_classes)
model.fit(x_train, y_train, epochs=100, batch_size=64, validation_data=(x_test, y_test))
I think I get an error on last Dense layer due to uncorrectly preprocessed labels. This i
ValueError: Shapes (None, 1) and (None, 753, 4) are incompatible
What I'm missing? What I know is that last Dense layer should have num classes as unit count (4 in my example).
I figured out what I was missing:
df["label"] = pd.Categorical(df["label"])
. So before model I added:
y_train = to_categorical(y_train, 4)
y_test = to_categorical(y_test, 4)
Now it works correctly.