Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upChaining networks and extracting intermediate calculations in Neural.Graph #487
Comments
|
If I understand correctly, the problem is to get the output of any layer in the network during the inferencing phase. I met similar issues before and have to manually accumulating the output of each layer: let run' topo x =
let last_node_output = ref (F 0.) in
Array.iteri (fun i n ->
let input = if i = 0 then x else !last_node_output in
let output = run [|input|] n.neuron in
last_node_output := output;
) topo;
!last_node_outputIn Keras that can be done simply by using |
|
I have a proof of concept for point 3 that builds a new network based on only the nodes that a given node depends on (all, not just the previous). It takes an optional argument of node names and replaces those with inputs. This way, it allows starting the prediction at any node. It should be possible to reuse this code for only doing prediction without a new network if that is more desirable. Before I send a [WIP] PR or go into the details, let me ask:
|
|
Yes the nodes in |
|
ordering is not required for |
Short background: I have two inputs x₁ and x₂ and a label y = f₁(x₁) + f₂(x₂). The functions f₁ and f₂ are unknown and approximated by
f1nnandf2nn. So far so good, and I can train a big networkynncontaining the structure forf1nnandf2nn. However, the predicted value y is not of interest, but instead I want f₁ and f₂.In the current form of the
Graphmodule, I couldn't figure out how to do this in a convenient way. At first I extracted these networks by copying neurons, but then moved on to constructing networks for f₁ and f₂ and then createynnusing the neurons fromf1nnandf2nn(without copying), which, by mutability, will be trained in the training of y. Prediction is then accomplished byGraph.model f1nn x.In the applications of interest to me, this is a quite common problem, so some sort of support for this would be great, unless my use case is very niche.
I see three ways to accomplish this.
NetworkorSubnetworknode which does essentially the same thing. This could be more pure, as one does not have to rely on mutability like in 1. Instead, when chaining, everything could be copied, provided theNetworknode has an interface for extracting a copy to use for prediction later. This approach also makes the network printout a bit cleaner, as there are fewer nodes.prevnodes.In my opinion, the second is the cleanest, whereas the third is the most flexible. If anything is of interest, I would gladly work on a PR.