## 1. 基本概念

``````[
['Hello', 'Hi'],
['Who', 'are', 'you'],
['what', 'time', 'is', 'it', 'now']
]
``````

``````[
[1, 2],
[3, 4, 5],
[6, 7, 8, 9, 10]
]
``````

``````[
[1, 2, 0, 0, 0],
[3, 4, 5, 0, 0],
[6, 7, 8, 9, 10]
]
``````

``````[
[True, True, False, False, False],
[True, True, True,  False, False],
[True, True, True,  True,  True ]
]
``````

## 2. TensorFlow 中的 Padding

``````import tensorflow as tf

inputs = [
[1, 2],
[3, 4, 5],
[6, 7, 8, 9, 10]
]

)
print(inputs)
``````

``````[[ 1  2  0  0  0]
[ 3  4  5  0  0]
[ 6  7  8  9 10]]
``````

## 3. TensorFlow 中的 Mask

``````model = tf.keras.Sequential([
tf.keras.layers.Embedding(10000, 32),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
``````

``````Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding (Embedding)        (None, None, 32)          320000
_________________________________________________________________
global_average_pooling1d (Gl (None, 32)                0
_________________________________________________________________
dense (Dense)                (None, 64)                2112
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 65
=================================================================
Total params: 322,177
Trainable params: 322,177
Non-trainable params: 0
_________________________________________________________________
``````

``````model2 = tf.keras.Sequential([
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model2.summary()
``````

``````Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
embedding_2 (Embedding)      (None, None, 32)          320000
_________________________________________________________________
global_average_pooling1d_2 ( (None, 32)                0
_________________________________________________________________
dense_4 (Dense)              (None, 64)                2112
_________________________________________________________________
dense_5 (Dense)              (None, 1)                 65
=================================================================
Total params: 322,177
Trainable params: 322,177
Non-trainable params: 0
_________________________________________________________________
``````

``````print(model(inputs))
print(model2(inputs))
``````

``````tf.Tensor(
[[0.5014145 ]
[0.50220466]
[0.5058755 ]], shape=(3, 1), dtype=float32)

tf.Tensor(
[[0.4913409 ]
[0.49880138]
[0.49782944]], shape=(3, 1), dtype=float32)
``````

``````model3 = tf.keras.Sequential([
tf.keras.layers.Embedding(10000, 32),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model3.summary()
``````

``````Model: "sequential_10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
_________________________________________________________________
embedding_10 (Embedding)     (None, 256, 32)           320000
_________________________________________________________________
global_average_pooling1d_10  (None, 32)                0
_________________________________________________________________
dense_20 (Dense)             (None, 64)                2112
_________________________________________________________________
dense_21 (Dense)             (None, 1)                 65
=================================================================
Total params: 322,177
Trainable params: 322,177
Non-trainable params: 0
_________________________________________________________________
``````