private static void CreateLayerInGraph(ComponentGraph graph, ComponentVertex vertex, RandomNumberGenerator <float> random) { if (graph.InDegree(vertex) == 0) { // use some arbitrary layout to start // source layer must be input layer that overrides it vertex.Layer = Layer.CreateFromArchitecture(new Shape(Shape.BWHC, -1, 100, 100, 100), vertex.Architecture, random); } else { IList <Shape> shapes = new List <Shape>(); foreach (Edge <ComponentVertex> edge in graph.InEdges(vertex)) { if (edge.Source.Layer == null) { NetworkGraphBuilder.CreateLayerInGraph(graph, edge.Source, random); } shapes.Add(edge.Source.Layer.OutputShape); } vertex.Layer = shapes.Count == 1 ? Layer.CreateFromArchitecture(shapes[0], vertex.Architecture, random) : Layer.CreateFromArchitecture(shapes, vertex.Architecture, random); } }
/// <inheritdoc /> public override bool Equals(object obj) { if (obj == this) { return(true); } ComponentVertex other = obj as ComponentVertex; if (other == null) { return(false); } return(this.Key == other.Key && this.Architecture == other.Architecture && object.ReferenceEquals(this.Layer, other.Layer)); }
private static ComponentGraph ParseArchitecture(string architecture, bool isNested) { // split architecture into elements that include layers, modules, and edges IList <string> components = ComponentGraph.SplitArchitecture(architecture, NetworkGraphBuilder.Delimiter); // extract elements that defines edges and modules (in A=... format) Dictionary <string, string> elements; Dictionary <string, ComponentGraph> modules; NetworkGraphBuilder.ParseComponents(components, out elements, out modules); // components must now contain edges only (in A-B-... format) ComponentGraph graph = ComponentGraph.FromComponents(components, elements, modules); // process nested graphs if (isNested) { // nested graphs must start with a single split layer IList <ComponentVertex> sources = graph.Sources.ToList(); if (sources.Count > 1) { ComponentVertex split = new ComponentVertex( Guid.NewGuid().ToString(), string.Format(CultureInfo.InvariantCulture, "SP{0}", sources.Count)); graph.AddEdges(sources.Select(x => new Edge <ComponentVertex>(split, x))); } // nested graphs must end with a single concat layer IList <ComponentVertex> sinks = graph.Sinks.ToList(); if (sinks.Count > 1) { ComponentVertex concat = new ComponentVertex(Guid.NewGuid().ToString(), "CONCAT"); graph.AddEdges(sinks.Select(x => new Edge <ComponentVertex>(x, concat))); } } return(graph); }