private static void AddLossLayers(NetworkGraph graph) { foreach (Layer layer in graph.Sinks.ToList()) { if (!(layer is LossLayer)) { graph.AddEdge(layer, new SoftMaxLayer(layer.OutputShape)); } } }
public static NetworkGraph CreateNetworkGraph(string architecture, bool addActivationLayers, bool addLossLayer) { if (architecture == null) { throw new ArgumentNullException(nameof(architecture)); } if (string.IsNullOrEmpty(architecture)) { throw new ArgumentException(Properties.Resources.E_InvalidNetArchitecture_NoLayers, nameof(architecture)); } // 1. parse architecture string and build preliminary graph ComponentGraph componentGraph = NetworkGraphBuilder.ParseArchitecture(architecture, false); // 2. create layers in the preliminary graph RandomNumberGenerator <float> random = null; //// new Random(0); foreach (ComponentVertex sink in componentGraph.Sinks) { NetworkGraphBuilder.CreateLayerInGraph(componentGraph, sink, random); } // 3. convert to network graph NetworkGraph graph = new NetworkGraph(); foreach (Edge <ComponentVertex> edge in componentGraph.Edges) { /*NetworkGraph sourceGraph = (edge.Source.Layer as RNNLayer)?.Graph; * NetworkGraph targetGraph = (edge.Target.Layer as RNNLayer)?.Graph; * * if (sourceGraph != null) * { * graph.AddGraph(sourceGraph); * if (targetGraph != null) * { * graph.AddEdges(sourceGraph.Sinks, targetGraph.Sources); * graph.AddGraph(targetGraph); * } * else * { * graph.AddEdges(sourceGraph.Sinks, edge.Target.Layer); * } * } * else if (targetGraph != null) * { * graph.AddEdges(edge.Source.Layer, targetGraph.Sources); * graph.AddGraph(targetGraph); * } * else*/ { graph.AddEdge(edge.Source.Layer, edge.Target.Layer); } } // 4. add missing loss layers if (addLossLayer) { NetworkGraphBuilder.AddLossLayers(graph); } // 5. add missing activation layers if (addActivationLayers) { NetworkGraphBuilder.AddActivationLayers(graph); } // 6. initialize stochastic biases with ReLU activations NetworkGraphBuilder.InitializeReLUs(graph); return(graph); }