观察卷积
检查卷积层的激活是件很有趣的事。至少这一生要体验一次!
为此,您将用 Keras 的 Model 对象构建一个新模型。它接收输入列表和输出列表。您要提供给这个新模型的输出,是当输入一张 MNIST 数字图片时,模型第一个卷积层的输出。
您在上一个练习中构建的卷积 model 已经替您训练好。它现在可以正确分类 MNIST 手写数字图片。您可以在控制台中通过 model.summary() 进行查看。
让我们来看看该模型第一个卷积层中学到的卷积掩码吧!
本练习是课程的一部分
Keras 深度学习入门
练习说明
- 获取模型中第一个卷积层输出的引用。
- 使用模型第一层的输入以及
first_layer_output作为输出,构建一个新模型。 - 使用这个
first_layer_model对X_test做预测。 - 绘制
X_test中第 1 个数字在第 15 个与第 18 个神经元过滤器上的激活可视化。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Obtain a reference to the outputs of the first layer
first_layer_output = model.layers[____].output
# Build a model using the model's input and the first layer output
first_layer_model = Model(inputs = model.layers[0].input, outputs = ____)
# Use this model to predict on X_test
activations = ____.____(____)
# Plot the activations of first digit of X_test for the 15th filter
axs[0].matshow(activations[____,:,:,14], cmap = 'viridis')
# Do the same but for the 18th filter now
axs[1].matshow(activations[0,:,:,____], cmap = 'viridis')
plt.show()