我正在用tf.keras建立一个非常小的顺序模型来进行文本分类,如下所示:
model = tf.keras.Sequential([
embedding_layer,
tf.keras.layers.Conv1D(64, 5, activation='relu', name="conv1d"),
tf.keras.layers.MaxPool1D(pool_size=4, name="maxpool1d"),
tf.keras.layers.Flatten(name="flatten"),
tf.keras.layers.Dense(1, activation='sigmoid', name="dense")
])
Layer (type) Output Shape Param #
=================================================================
inputs (InputLayer) [(None, 2762)] 0
_________________________________________________________________
embedding (Embedding) (None, 2762, 50) 1893250
_________________________________________________________________
conv1d (Conv1D) (None, 2758, 64) 16064
_________________________________________________________________
maxpool1d (MaxPooling1D) (None, 689, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 44096) 0
_________________________________________________________________
dense (Dense) (None, 1) 44097
=================================================================
在测试时,对于输入中给定的句子,我想找到对预测最有用的单词窗口。我想我必须看一下conv1d层或最大池化层的输出,但是我不确定要寻找什么。我认为这与某些层的最高激活有关。
我已经知道我可以使用以下代码行访问给定层的输出值:
intermediate_layer_model = tf.keras.Model(inputs=model.input, outputs=model.get_layer(name="flatten").output)
intermediate_output = intermediate_layer_model.predict(x=test_input, workers=1, use_multiprocessing=False)