I assume you mean the examples given in either the Keras API documentation or the TensorFlow API for Keras (they both use the same examples).
These examples are creating a Conv1D layer - not a full model. A convolutional layer simply takes an input tensor, applies the convolution operation and returns an output tensor. It doesn't split the input tensor into batches, but assumes the input tensor is a batch.
If you look closely at the input_shape parameter on the conv1d constructor, you will notice that it isn't passing in the entire input_shape
tuple. In the first example, where the input_shape tuple is (4, 10, 128)
, the input_shape parameter is input_shape[1:]
, which is the tuple (10, 128)
. In the second example, where the input_shape tuple is (4, 7, 10, 128)
, the input_shape parameter is input_shape[2:]
, which, again, is the tuple (10, 128)
. So both examples create the same conv1d layer.
The only difference between the two examples is the shape of the input tensor. The input tensor can have as many dimensions as you like, as long as the size of the last two dimensions match the input_shape - i.e. (10, 128). The remaining dimensions specify the size and shape of the batch, which could be different each time the layer is called.
This example code may help make it clearer. It creates a conv1d layer and then calls it using several different shaped input tensors:
import tensorflow as tf
# Input shape used by the conv1D layer
input_shape = (10, 128)
# Create the conv1D layer, which will be used by all examples
conv = tf.keras.layers.Conv1D(32, 3, activation='relu', input_shape=input_shape)
# This is equivalent to the first example - uses a batch shape of (4,)
x1 = tf.random.normal((4,) + input_shape)
y1 = conv(x1)
print(f"Input shape: {x1.shape}; Output shape: {y1.shape}")
>>> Input shape: (4, 10, 128); Output shape: (4, 8, 32)
# This is equivalent to the second example - uses a batch shape of (4, 7)
x2 = tf.random.normal((4, 7) + input_shape)
y2 = conv(x2)
print(f"Input shape: {x2.shape}; Output shape: {y2.shape}")
>>> Input shape: (4, 7, 10, 128); Output shape: (4, 7, 8, 32)
# This has a 3-dimensional batch shape of (4, 7, 5)
x3 = tf.random.normal((4, 7, 5) + input_shape)
y3 = conv(x3)
print(f"Input shape: {x3.shape}; Output shape: {y3.shape}")
>>> Input shape: (4, 7, 5, 10, 128); Output shape: (4, 7, 5, 8, 32)
```