Example #1
0
        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);
            }
        }
Example #2
0
            private static bool AddEdge(ComponentGraph graph, ComponentVertex sourceVertex, ComponentGraph targetGraph)
            {
                if (graph.AddEdges(targetGraph.Sources.Select(x => new Edge <ComponentVertex>(sourceVertex, x))) != targetGraph.Sinks.Count())
                {
                    return(false);
                }

                return(graph.AddEdges(targetGraph.Edges) == targetGraph.Size);
            }
Example #3
0
        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);
        }
Example #4
0
            private static bool AddEdge(ComponentGraph graph, ComponentGraph sourceGraph, ComponentGraph targetGraph)
            {
                graph.AddEdges(sourceGraph.Sinks.Zip(targetGraph.Sources, (s, t) => new Edge <ComponentVertex>(s, t)));

                return(graph.AddEdges(targetGraph.Edges) == targetGraph.Size);
            }
Example #5
0
 private static bool AddEdge(ComponentGraph graph, ComponentGraph sourceGraph, ComponentVertex targetVertex)
 {
     return(graph.AddEdges(sourceGraph.Sinks.Select(x => new Edge <ComponentVertex>(x, targetVertex))) == sourceGraph.Sinks.Count());
 }
Example #6
0
 private static bool AddEdge(ComponentGraph graph, ComponentVertex sourceVertex, ComponentVertex targetVertex)
 {
     return(graph.AddEdge(new Edge <ComponentVertex>(sourceVertex, targetVertex)));
 }
Example #7
0
            public static ComponentGraph FromComponents(IEnumerable <string> components, IDictionary <string, string> elements, IDictionary <string, ComponentGraph> modules)
            {
                ComponentGraph graph = new ComponentGraph();
                Dictionary <string, ComponentVertex> vertices = new Dictionary <string, ComponentVertex>();

                foreach (string component in components)
                {
                    IList <string> parts = ComponentGraph.SplitArchitecture(component, NetworkGraphBuilder.Splitter);
                    if (parts.Count >= 2 && parts.All(x => !string.IsNullOrEmpty(x)))
                    {
                        ComponentVertex sourceVertex = null;
                        ComponentGraph  sourceGraph  = null;

                        for (int i = 0, ii = parts.Count; i < ii; i++)
                        {
                            string         key         = parts[i];
                            ComponentGraph targetGraph = null;

                            if (key.First() == NetworkGraphBuilder.StartQualifier && key.Last() == NetworkGraphBuilder.EndQualifier)
                            {
                                targetGraph = NetworkGraphBuilder.ParseArchitecture(key.Substring(1, key.Length - 2), true);
                            }
                            else
                            {
                                ComponentGraph moduleGraph;
                                if (modules.TryGetValue(key, out moduleGraph))
                                {
                                    targetGraph = moduleGraph.Clone(true) as ComponentGraph;
                                }
                            }

                            if (targetGraph != null)
                            {
                                if (i > 0)
                                {
                                    bool result = sourceVertex != null?ComponentGraph.AddEdge(graph, sourceVertex, targetGraph) : ComponentGraph.AddEdge(graph, sourceGraph, targetGraph);

                                    if (!result)
                                    {
                                        throw new ArgumentException(
                                                  string.Format(CultureInfo.InvariantCulture, Properties.Resources.E_InvalidNetArchitecture_DuplicateEdge, parts[i - 1], parts[i]));
                                    }
                                }

                                sourceVertex = null;
                                sourceGraph  = targetGraph;
                            }
                            else
                            {
                                ComponentVertex targetVertex;

                                string arch;
                                if (elements.TryGetValue(key, out arch))
                                {
                                    if (!vertices.TryGetValue(key, out targetVertex))
                                    {
                                        vertices[key] = targetVertex = new ComponentVertex(key, arch);
                                    }
                                }
                                else
                                {
                                    targetVertex = new ComponentVertex(Guid.NewGuid().ToString(), key);
                                }

                                if (i > 0)
                                {
                                    bool result = sourceVertex != null?ComponentGraph.AddEdge(graph, sourceVertex, targetVertex) : ComponentGraph.AddEdge(graph, sourceGraph, targetVertex);

                                    if (!result)
                                    {
                                        throw new ArgumentException(
                                                  string.Format(CultureInfo.InvariantCulture, Properties.Resources.E_InvalidNetArchitecture_DuplicateEdge, parts[i - 1], parts[i]));
                                    }
                                }

                                sourceVertex = targetVertex;
                                sourceGraph  = null;
                            }
                        }
                    }
                    else if (parts.Count == 1 && !string.IsNullOrEmpty(parts[0]))
                    {
                        graph.AddVertex(new ComponentVertex(Guid.NewGuid().ToString(), parts[0]));
                    }
                    else
                    {
                        throw new ArgumentException(
                                  string.Format(CultureInfo.InvariantCulture, Properties.Resources.E_InvalidNetArchitecture, component));
                    }
                }

                // recreate vertices keys, for the embedded graphs to have unique layers
                foreach (ComponentVertex vertex in graph.Vertices)
                {
                    vertex.Key = Guid.NewGuid().ToString();
                }

                return(graph);
            }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComponentGraph"/> class
 /// using existing graph as a source.
 /// </summary>
 /// <param name="other">The existing <see cref="ComponentGraph"/> to create this graph from.</param>
 /// <param name="cloneVertices">The value indicating whether the graph vertices should be cloned.</param>
 public ComponentGraph(ComponentGraph other, bool cloneVertices)
     : base(other, cloneVertices)
 {
 }
Example #9
0
        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);
        }
Example #10
0
        public VisHost(Player.Player player)
            : base(
                GameWindowSettings.Default,
                //new GameWindowSettings {
                //    IsMultiThreaded = false,
                //    UpdateFrequency = 120
                //},
                new NativeWindowSettings
        {
            Size       = new Vector2i(1600, 900),
            APIVersion = new Version(4, 5),
            Profile    = ContextProfile.Compatability,
            Flags      = ContextFlags.Default,
            Title      = "NeuralBeat3",
            API        = ContextAPI.OpenGL
        })
        {
            Player = player;


            //TargetRenderFrequency = 144f;
            //this.RenderFrequency = 144.0;
            VSync = VSyncMode.On;

            UpdateFrame += VisHost_UpdateFrame;
            RenderFrame += VisHost_RenderFrame;
            Load        += VisHost_Load;
            Unload      += VisHost_Unload;
            Resize      += VisHost_Resize;
            Closed      += VisHost_Closed;
            Closing     += VisHost_Closing;

            //Keyboard.KeyDown += Keyboard_KeyDown;
            //Keyboard.KeyUp += Keyboard_KeyUp;
            this.KeyDown += Keyboard_KeyDown;
            this.KeyUp   += Keyboard_KeyUp;

            // set default shader loader
            ShaderProgram.DefaultLoader = new OpenTKExtensions.Loaders.MultiPathFileSystemLoader(SHADERPATH);


            // framedata setup
            frameData.GlobalTextures = globalTextures;

            // create components
            //components.Add(font = new Font(@"res\font\calibrib.ttf_sdf.2048.png", @"res\font\calibrib.ttf_sdf.2048.txt"), 1);
            components.Add(font            = new Font(@"res\font\lucon.ttf_sdf.1024.png", @"res\font\lucon.ttf_sdf.1024.txt"), 1);
            components.Add(text            = new TextManager("texmgr", font), 2);
            components.Add(keyboardActions = new KeyboardActionManager()
            {
                KeyboardPriority = int.MaxValue
            }, 1);
            components.Add(globalTextures);
            components.Add(frameCounter = new OpenTKExtensions.Components.FrameCounter(font));
            components.Add(switcher     = new ComponentSwitcher()
            {
                KeyForward = new KeySpec(Keys.Tab), KeyBackward = new KeySpec(Keys.Tab, KeyModifiers.Shift)
            });

            switcher.Add(new Renderers.AnalysisDebugRenderer(font, Player));
            switcher.Add(new Renderers.BasicShaderRenderer());
            switcher.Add(new Renderers.ParticleRenderer());

            var graph = new ComponentGraph();

            graph.Add(new ParticleNode()
            {
                Name = "particles"
            });
            graph.Add(new ScreenOutputNode()
            {
                Name = "output"
            });;
            graph.AddEdge(new NodePortReference()
            {
                Node = "particles", Port = "tex"
            }, new NodePortReference()
            {
                Node = "output", Port = "tex"
            });

            switcher.Add(graph);


            //Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
        }