Home > front end >  Using conv1d Layer in Tensorflow Sequential Model
Using conv1d Layer in Tensorflow Sequential Model

Time:08-30

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!

CodePudding user response:

You have to keep x_train and y_train data separated. See the docs for keras fit function:

Model.fit(
    x=None,
    y=None,
    batch_size=None,
    epochs=1,
    verbose="auto",
    callbacks=None,
    validation_split=0.0,
    validation_data=None,
    shuffle=True,
    class_weight=None,
    sample_weight=None,
    initial_epoch=0,
    steps_per_epoch=None,
    validation_steps=None,
    validation_batch_size=None,
    validation_freq=1,
    max_queue_size=10,
    workers=1,
    use_multiprocessing=False,
)

The argument x represents your data to train, and y your labels. You have to reshape your train_ds data.

CodePudding user response:

i think it would just cause of the input layer shape, the model is fed with dataset which is in shape (rows, features) into model, so based on that the will feed in this shape (batch_size, features) and as you know the term None means batch size and not needed to define for input shape, so in here:

tf.keras.layers.InputLayer(input_shape=(len(feature_columns), 1))

should be changed into

tf.keras.layers.InputLayer(input_shape=(len(feature_columns),))

or this one

tf.keras.layers.InputLayer(input_shape=(None, len(feature_columns)))

cause of tensorflow will assume term None in the input shape as batch size.

and basically, as i noticed this type of model (Conv1D) is for sequences but you are trying to implement model for a typical one, which takes features and then lead into output feature, which in this case is as label.

So if i got it correctly, change your model arch into a model without Conv1d layer and even change input_shape based on what i said above. hopefully, it would work.

  • Related