Exemplo n.º 1
0
        public Volume <T> Run(Op <T> fun, Dictionary <string, Volume <T> > dictionary)
        {
            this.BatchSize = dictionary.Values.Select(o => o.Shape.GetDimension(3)).Max(); // is this correct?

            // Find all PlaceHolders and update their current value
            var visitor = new OpVisitor <T>(op =>
            {
                var placeHolder = op as PlaceHolder <T>;
                if (placeHolder != null)
                {
                    placeHolder.Result = dictionary[placeHolder.Name];
                    placeHolder.SetDirty();
                }

                var variable = op as Variable <T>;
                if (variable != null)
                {
                    this.LearnableVariables[variable.Name] = variable;
                }
            });

            fun.Accept(visitor);

            var result = fun.Evaluate(this);

            this.Step++;

            return(result);
        }
        public static Dictionary <string, object> GetVolumes <T>(this Op <T> op) where T : struct, IEquatable <T>, IFormattable
        {
            var result = new Dictionary <string, object>();

            // Retrieve all ops and assign an Id
            var visitor = new OpVisitor <T>(o =>
            {
                var persistable = o as IPersistable <T>;
                var volume      = persistable?.Result;
                if (volume != null)
                {
                    var dico = new Dictionary <string, object>
                    {
                        ["dim0"] = volume.Shape.GetDimension(0),
                        ["dim1"] = volume.Shape.GetDimension(1),
                        ["dim2"] = volume.Shape.GetDimension(2),
                        ["dim3"] = volume.Shape.GetDimension(3),
                        ["vol"]  = volume.ToArray(),
                    };

                    result.Add(persistable.Name, dico); // we use Add here to it throws when the name is already used
                }
            });

            op.Accept(visitor);

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Automatic differentiation using reverse accumulation
        /// </summary>
        /// <param name="cost"></param>
        /// <param name="gradient">1 will be used as gradient if not specify</param>
        public void Differentiate(Op <T> cost, Op <T> gradient = null)
        {
            if (!this._derivativeComputed)
            {
                var visitor = new OpVisitor <T>(op => { op.Derivate = null; });
                cost.Accept(visitor);

                this.Cost = cost;

                cost.Derivate = gradient ?? ConvNetSharp <T> .One;

                var differentiateVisitor = new DifferentiateVisitor <T>();
                cost.Accept(differentiateVisitor);

                this._derivativeComputed = true;
            }
        }
Exemplo n.º 4
0
        public void Dump(Op <T> fun, string fileName)
        {
            using var sw = new StreamWriter(File.Create(fileName));

            var streamWriter = sw;
            var visitor      = new OpVisitor <T>(op =>
            {
                streamWriter.WriteLine(op);
                streamWriter.WriteLine(op.Result == null ? "[Null]" : op.Result.ToString());
            });

            fun.Accept(visitor);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Automatic differentiation using reverse accumulation
        /// </summary>
        /// <param name="cost"></param>
        public void Differentiate(Op <T> cost)
        {
            if (!this._derivativeComputed)
            {
                this.Cost = cost;

                cost.Derivate = this._cns.Const(ConvNetSharp <T> .One, "1");

                //this._func.Derivate = cost;
                var differentiateVisitor = new DifferentiateVisitor <T>();
                cost.Accept(differentiateVisitor);

                this._derivativeComputed = true;
            }
        }
Exemplo n.º 6
0
        public void UpdatePlaceHolder(Op <T> fun, Dictionary <string, Volume <T> > dictionary)
        {
            // Find all PlaceHolders and update their current value
            var visitor = new OpVisitor <T>(op =>
            {
                if (op is PlaceHolder <T> placeHolder)
                {
                    placeHolder.SetValue(dictionary[placeHolder.Name]);
                }

                if (op is Variable <T> variable && variable.IsLearnable)
                {
                    this.LearnableVariables[variable.Name] = variable;
                }
            });

            fun.Accept(visitor);
        }
        public ViewModel(Op <T> root)
        {
            var colors = CreateColorPalette(20);

            var set = new HashSet <Op <T> >();

            var visitor = new OpVisitor <T>(op =>
            {
                if (!set.Contains(op))
                {
                    set.Add(op);
                }
            });

            root.Accept(visitor);

            var graph = new BidirectionalGraph <object, IEdge <object> >(); // new OpGraph();

            var dico = new Dictionary <Op <T>, OpVertex>();

            foreach (var op in set)
            {
                var colIndex = op.GetType().GetHashCode() % colors.Count;
                var opVertex = new OpVertex
                {
                    Name  = op.Representation,
                    Color = colors[colIndex],
                    Shape = op.Result?.Shape.ToString() != null ? "[" + op.Result.Shape + "]" : string.Empty
                };
                dico[op] = opVertex;
                graph.AddVertex(opVertex);
            }

            foreach (var op in set)
            {
                foreach (var parent in op.Parents)
                {
                    graph.AddEdge(new OpEdge(dico[parent], dico[op]));
                }
            }

            this.Graph = graph;
        }
Exemplo n.º 8
0
        public Op <T> GetVariableByName(Op <T> fun, string name)
        {
            Op <T> result = null;

            var visitor = new OpVisitor <T>(op =>
            {
                if (op is INamedOp <T> variable)
                {
                    if (variable.Name == name)
                    {
                        result = op;
                    }
                }
            });

            fun.Accept(visitor);

            return(result);
        }
Exemplo n.º 9
0
        public void UpdatePlaceHolder(Op <T> fun, Dictionary <string, Volume <T> > dictionary)
        {
            // Find all PlaceHolders and update their current value
            var visitor = new OpVisitor <T>(op =>
            {
                var placeHolder = op as PlaceHolder <T>;
                if (placeHolder != null)
                {
                    placeHolder.Result = dictionary[placeHolder.Name];
                    placeHolder.SetDirty();
                }

                var variable = op as Variable <T>;
                if (variable != null)
                {
                    this.LearnableVariables[variable.Name] = variable;
                }
            });

            fun.Accept(visitor);
        }
Exemplo n.º 10
0
        public void UpdatePlaceHolder(Op <T> fun, Dictionary <string, Volume <T> > dictionary)
        {
            // Find all PlaceHolders and update their current value
            var visitor = new OpVisitor <T>(op =>
            {
                if (op is PlaceHolder <T> placeHolder)
                {
                    if (!dictionary.TryGetValue(placeHolder.Name, out var volume))
                    {
                        throw new Exception($"Cannot find key '{placeHolder.Name}' in the provided dictionary");
                    }

                    placeHolder.SetValue(volume);
                }

                if (op is Variable <T> variable && variable.IsLearnable)
                {
                    this.LearnableVariables[variable.Name] = variable;
                }
            });

            fun.Accept(visitor);
        }
        /// <summary>
        ///     Serialize graph to graphml
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="op">Root op</param>
        /// <param name="costOp">Optional cost Op</param>
        /// <returns></returns>
        public static string ToXml <T>(this Op <T> op, Op <T> costOp = null) where T : struct, IEquatable <T>, IFormattable
        {
            var id   = 0;
            var set  = new Dictionary <Op <T>, string>();
            var keys = new Dictionary <string, KeyDescription>
            {
                { "d0", new KeyDescription {
                      Id = "d0", Name = "type"
                  } },
                { "d1", new KeyDescription {
                      Id = "d1", Name = "root", @for = "graph"
                  } },
                { "d2", new KeyDescription {
                      Id = "d2", Name = "cost", @for = "graph"
                  } }
            };

            // Retrieve all ops and assign an Id
            var visitor = new OpVisitor <T>(o =>
            {
                if (!set.ContainsKey(o))
                {
                    set.Add(o, "n" + id++);
                }
            });

            op.Accept(visitor);
            costOp?.Accept(visitor);

            using (var sw = new StringWriter())
            {
                using (var writer = XmlWriter.Create(sw, new XmlWriterSettings {
                    NewLineOnAttributes = true, Indent = true
                }))
                {
                    var ns = "http://graphml.graphdrawing.org/xmlns";

                    writer.WriteStartDocument();
                    writer.WriteStartElement("graphml", ns);
                    writer.WriteAttributeString("xmlns", ns);

                    // Get all keys
                    var keyId = 3;
                    foreach (var pair in set)
                    {
                        var data = pair.Key.GetData();
                        if (data.Any())
                        {
                            foreach (var o in data)
                            {
                                var name = pair.Key.GetType().Name + "." + o.Key;
                                if (!keys.ContainsKey(name))
                                {
                                    keys[name] = new KeyDescription {
                                        Id = "d" + keyId++, Name = name
                                    };
                                }
                            }
                        }
                    }

                    // Generate key xml
                    foreach (var keyDescription in keys.Values)
                    {
                        writer.WriteStartElement("key");
                        writer.WriteAttributeString("id", keyDescription.Id);
                        writer.WriteAttributeString("for", "node");
                        writer.WriteAttributeString("attr.name", keyDescription.Name);
                        writer.WriteAttributeString("attr.type", "string");
                        writer.WriteEndElement();
                    }

                    writer.WriteStartElement("graph");
                    writer.WriteAttributeString("id", "G");
                    writer.WriteAttributeString("edgedefault", "directed");

                    // Root
                    writer.WriteStartElement("data");
                    writer.WriteAttributeString("key", "d1");
                    writer.WriteString(set[op]);
                    writer.WriteEndElement();

                    // Cost if provided
                    if (costOp != null)
                    {
                        writer.WriteStartElement("data");
                        writer.WriteAttributeString("key", "d2");
                        writer.WriteString(set[costOp]);
                        writer.WriteEndElement();
                    }

                    foreach (var pair in set)
                    {
                        writer.WriteStartElement("node");
                        writer.WriteAttributeString("id", pair.Value);

                        writer.WriteStartElement("data");
                        writer.WriteAttributeString("key", "d0");
                        writer.WriteString(pair.Key.GetType().FullName);
                        writer.WriteEndElement();

                        var data = pair.Key.GetData();
                        if (data.Any())
                        {
                            foreach (var o in data)
                            {
                                var name    = pair.Key.GetType().Name + "." + o.Key;
                                var keyDesc = keys[name];

                                writer.WriteStartElement("data");
                                writer.WriteAttributeString("key", keyDesc.Id);
                                writer.WriteString(o.Value.ToString());
                                writer.WriteEndElement();
                            }
                        }

                        writer.WriteEndElement();
                    }

                    foreach (var pair in set)
                    {
                        foreach (var valueParent in pair.Key.Parents)
                        {
                            writer.WriteStartElement("edge");
                            writer.WriteAttributeString("source", set[valueParent]);
                            writer.WriteAttributeString("target", set[pair.Key]);
                            writer.WriteEndElement();
                        }
                    }

                    writer.WriteEndElement();

                    writer.WriteEndElement();
                }

                return(sw.ToString());
            }
        }