简体   繁体   English

在 Tensorflow 中使用 conv1d 层顺序 Model

[英]Using conv1d Layer in Tensorflow Sequential Model

i am very new to Tensorflow and just cannot figure out the problem.我对 Tensorflow 很陌生,只是无法弄清楚问题所在。 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我正在尝试构建一个 CNN,但我一直遇到 conv1d 层(特别是输入)的问题: expected min_ndim=3, found ndim=2 tensorflow sequential

I already tried: ValueError when using Conv1D layer , but this does not change anything.我已经尝试过: ValueError when using Conv1D layer ,但这并没有改变任何东西。

Here is the code of the model:这是 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 ):编辑:这就是 train_ds 的创建方式(我遵循了本教程: 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!先感谢您!

You have to keep x_train and y_train data separated.您必须将x_trainy_train数据分开。 See the docs for keras fit function:请参阅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.参数x代表您要训练的数据, y代表您的标签。 You have to reshape your train_ds data.您必须重塑您的train_ds数据。

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:我认为这只是输入层形状的原因,model 将形状(rows, features)的数据集输入到 model 中,因此基于此形状(batch_size, features)和如您所知术语None表示批量大小,不需要为输入形状定义,所以在这里:

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. tensorflow 的原因将假定输入形状中的术语None作为批量大小。

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. 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.因此,如果我理解正确,请将您的 model 拱形更改为没有 Conv1d 层的 model,甚至根据我上面所说的更改 input_shape。 hopefully, it would work.希望它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM