[英]Using conv1d Layer in Tensorflow Sequential Model
我对 Tensorflow 很陌生,只是无法弄清楚问题所在。 我正在尝试构建一个 CNN,但我一直遇到 conv1d 层(特别是输入)的问题: expected min_ndim=3, found ndim=2 tensorflow sequential
我已经尝试过: ValueError when using Conv1D layer ,但这并没有改变任何东西。
这是 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]
)
编辑:这就是 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()
先感谢您!
您必须将x_train
和y_train
数据分开。 请参阅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,
)
参数x
代表您要训练的数据, y
代表您的标签。 您必须重塑您的train_ds
数据。
我认为这只是输入层形状的原因,model 将形状(rows, features)
的数据集输入到 model 中,因此基于此形状(batch_size, features)
和如您所知术语None
表示批量大小,不需要为输入形状定义,所以在这里:
tf.keras.layers.InputLayer(input_shape=(len(feature_columns), 1))
应该改成
tf.keras.layers.InputLayer(input_shape=(len(feature_columns),))
或者这个
tf.keras.layers.InputLayer(input_shape=(None, len(feature_columns)))
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.
因此,如果我理解正确,请将您的 model 拱形更改为没有 Conv1d 层的 model,甚至根据我上面所说的更改 input_shape。 希望它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.