python 的 CNTK - 激活每一层

CNTK with python - activation for each layer

我正在使用 CNTK 的 python API 来训练我使用 save_model 函数保存的一些 CNN。

现在我想 运行 之后对我的网络进行一些分析。具体来说,我想看看每一层的激活。显然,我可以 运行 我的网络上一些名为 img 的数据,如下所示:

model.eval(img)

但这只会给我网络中 last 层的输出。有没有一些简单的方法也可以从前面的层中获取输出?

实际上,甚至为该任务提供了一个示例:https://github.com/Microsoft/CNTK/tree/master/Examples/Image/FeatureExtraction

让我简要概述一下基本步骤:

重要的是您要获取其输出的节点的名称。

# get the node in the graph of which you desire the output
node_in_graph = loaded_model.find_by_name(node_name)
output_nodes  = combine([node_in_graph.owner])

# evaluate the node e.g. using a minibatch_source
mb = minibatch_source.next_minibatch(1)
output = output_nodes.eval(mb[features_si])

# access the values as a one dimensional vector
out_values = output[0].flatten()
desired_output = out_values[np.newaxis]

基本上你只是像你一样做同样的事情,不同的是你检索了一个中间节点。