简体   繁体   中英

TensorFlow: How do I use make a convolutional layer for tabular (1-D) features?

Using TensorFlow in Python, I am making a neural network that has a 1 dimensional array as input. I would like to add a convolutional layer to the network, but can't seem to get it to work.

My training data looks something like this:

n_samples = 20
length_feature = 10
features = np.random.random((n_samples, length_feature))
labels = np.array([1 if sum(e)>5 else 0 for e in features])

If I make a neural network like this one

model = keras.Sequential([
    keras.layers.Dense(10, activation='relu', input_shape=(length_feature, )),
    keras.layers.Dense(2, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(features, labels, batch_size=5, validation_split = 0.2, epochs=10)

and this works just fine. But if I add a convolutional layer like this

model = keras.Sequential([
    keras.layers.Dense(10, activation='relu', input_shape=(length_feature, )),
    keras.layers.Conv1D(kernel_size = 3, filters = 2),
    keras.layers.Dense(2, activation='softmax')
])

then I get the error

ValueError: Input 0 of layer conv1d_4 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 10]

How can I add a convolutional layer to my neural network?

Conv1D expects a 3D output( batch_size , width , channels ). But the dense layers produces a 2D output. Simply change your model to the following,

model = keras.Sequential([
    keras.layers.Dense(10, activation='relu', input_shape=(length_feature, )),
    keras.layers.Lambda(lambda x: K.expand_dims(x, axis=-1))
    keras.layers.Conv1D(kernel_size = 3, filters = 2),
    keras.layers.Dense(2, activation='softmax')
])

Where K is either keras.backend or tf.keras.backend depending on which one you used to get layers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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