Search code examples
cntk

How to access unnamed node in CNTK model


Suppose I use the following code to create a model (simplified) but don't name any of the nodes:

num_classes = 10
input_var = cntk.input_variable((12,))

model = Sequential([
    Dense(256),
    Dense(512),
    Dense(num_classes)
])(input_var)

If I wanted to evaluate the intermediate nodes later, how do I access them?

I've used

cntk.logging.get_node_outputs(model)

to inspect the graph and get the following output:

[Output('Block30733_Output_0', [#], [10]),
 Output('Block30719_Output_0', [#], [512]),
 Output('Block30705_Output_0', [#], [256])]

However, calling model.find_by_name() with those names returns None.


Solution

  • One approach is to do a graph search. You need to find a way to distinguish between the layers you want to evaluate. You can get all the nodes like this.

        blocks = C.logging.graph.depth_first_search(
                model, (lambda x : type(x) == C.Function and x.is_block) , depth = 0)
    

    For the lambda you can add any condition. To get all the blocks, you can use:

    lambda x: True