i am very new to Tensorflow and just cannot figure out the problem. I am trying to build a CNN, but I keep having issues with the conv1d layer (specifically the input):
expected min_ndim=3, found ndim=2 tensorflow sequential
I already tried: ValueError when using Conv1D layer, but this does not change anything.
Here is the code of the model:
#create feature_colums
from tensorflow import feature_column
feature_columns = []
for header in list(train_df.drop(columns=["LABEL"])):
feature_columns.append(feature_column.numeric_column(header))
feature_layer = tf.keras.layers.DenseFeatures(feature_columns)
model = tf.keras.Sequential([
feature_layer,
#tf.keras.layers.InputLayer(input_shape=(len(feature_columns), 1)),
tf.keras.layers.Dense(1024, activation="relu"),
tf.keras.layers.Conv1D(32, 3, activation="relu"),
#tf.keras.layers.MaxPool1D(pool_size=5),
#tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(512, activation="relu"),
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(32, activation="relu"),
tf.keras.layers.Dense(12, activation="softmax")
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
model.fit(train_ds,
validation_data=val_ds,
epochs=25,
#steps_per_epoch=20,
callbacks=[tensorboard_callback]
)
EDIT: This is how train_ds is created (I followed this tutorial: https://www.tensorflow.org/tutorials/structured_data/feature_columns#create_compile_and_train_the_model):
def df_to_dataset(dataframe, shuffle=True, batch_size=256):
dataframe = dataframe.copy()
labels = dataframe.pop("FAMILY")
ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
print(labels)
if shuffle:
ds = ds.shuffle(buffer_size=len(dataframe))
ds = ds.batch(batch_size)
return ds, labels, dataframe.values.tolist()
Thank you in advance!