Example #1
0
        private Variable FindVariable(string name)
        {
            var model = Trainer.Model();
            var va    = FunctionFind.FindVariable(model, name);

            if (va != null)
            {
                return(va);
            }

            var loss = Trainer.LossFunction();

            if (loss != null)
            {
                va = FunctionFind.FindVariable(loss, name);
                if (va != null)
                {
                    return(va);
                }
            }

            var error = Trainer.EvaluationFunction();

            if (error != null)
            {
                va = FunctionFind.FindVariable(error, name);
                if (va != null)
                {
                    return(va);
                }
            }

            return(null);
        }
Example #2
0
        public static WrappedVariable Find(PSObject func, string name)
        {
            Function f = ToFunction(func);

            var w = new FunctionFind(f, name, false, false);
            return w.Results[0];
        }
Example #3
0
        public static WrappedVariable[] FindAll(PSObject func, string name)
        {
            Function f = ToFunction(func);

            var w = new FunctionFind(f, name, true, false);
            return w.Results.Select(x => (WrappedVariable)x).ToArray();
        }
Example #4
0
        public static Value[] Invoke(this Function func, Hashtable arguments, DeviceDescriptor device = null, bool errorWhenArgumentUnused = true)
        {
            if (arguments == null)
            {
                arguments = new Hashtable();
            }

            var inputs = new Dictionary <Variable, Value>();

            foreach (DictionaryEntry entry in arguments)
            {
                Variable key;
                Value    value;

                var entryKey = entry.Key;
                if (entryKey is PSObject psobj)
                {
                    entryKey = psobj.BaseObject;
                }

                if (entryKey is Variable v)
                {
                    key = v;
                }
                else if (entryKey is WrappedVariable wv)
                {
                    key = wv;
                }
                else
                {
                    var va = FunctionFind.FindVariable(func, entryKey.ToString());
                    if (va == null)
                    {
                        if (errorWhenArgumentUnused)
                        {
                            throw new ArgumentException(string.Format("Unknown argument key '{0}'", entryKey.ToString()));
                        }
                        else
                        {
                            continue;
                        }
                    }

                    key = va;
                }

                if (entry.Value is IDataSource <float> ds)
                {
                    value = ds.ToValue(device);
                }
                else
                {
                    value = Converter.ToValue(entry.Value, key.Shape.Dimensions.ToArray());
                }

                inputs.Add(key, value);
            }

            return(Invoke(func, inputs, device, errorWhenArgumentUnused));
        }
Example #5
0
        public static Value[] Invoke(this Function func, IDictionary <string, IDataSource <float> > dataSet, DeviceDescriptor device = null, bool errorWhenArgumentUnused = true)
        {
            var inputs = new Dictionary <Variable, Value>();

            foreach (var entry in dataSet)
            {
                Variable key;
                Value    value;

                var va = FunctionFind.FindVariable(func, entry.Key);;
                if (va == null)
                {
                    if (errorWhenArgumentUnused)
                    {
                        throw new ArgumentException(string.Format("Unknown argument key '{0}'", entry.Key));
                    }
                    else
                    {
                        continue;
                    }
                }

                key = va;

                value = entry.Value.ToValue(device);

                inputs.Add(key, value);
            }

            return(Invoke(func, inputs, device, errorWhenArgumentUnused));
        }
Example #6
0
        public static Variable FindVariable(Function func, string name)
        {
            var w = new FunctionFind(func, name, false, true);

            if (w._variables.Count == 0)
            {
                return(null);
            }

            return(w._variables[0]);
        }
Example #7
0
        private List <Variable> FindVariables(string name)
        {
            var variables = new List <Variable>();

            foreach (var f in _funcs)
            {
                var v = FunctionFind.FindVariables(f, name);
                variables.AddRange(v);
            }

            return(variables);
        }
Example #8
0
        public static Variable Find(Function func, string name)
        {
            var w = new FunctionFind(func, name, false, false);

            var results = w.Results;

            if (results.Length == 0)
            {
                return(null);
            }

            return(results[0]);
        }
Example #9
0
        private void WriteNodes(IEnumerable <NodeGroup.Node> nodes, int depth)
        {
            if (nodes == null)
            {
                return;
            }

            var indent = new string(' ', depth * 4);

            foreach (var node in nodes)
            {
                var value = FunctionFind.Find(_model, node.Uid);
                if (value == null)
                {
                    throw new ApplicationException(string.Format("Can't find network node with uid {0}", node.Uid));
                }

                if (value.IsOutput)
                {
                    // Function

                    var func = value.Owner;

                    var name  = func.OpName + "|" + (func.Output == null ? "(undef)" : string.Join(" x ", func.Output.Shape.Dimensions));
                    var style = "style=\"filled\" fillcolor=\"white\"";

                    _output.AppendFormat("{0}{1} [label=\"{2}\" shape=\"record\" {3}];\r\n", indent, func.Uid, name, style);
                }
                else
                {
                    // Variable

                    var va = value as CNTK.Variable;

                    var style = "style=\"rounded\"";
                    if (va.IsInput)
                    {
                        style = "style=\"rounded, filled\" fillcolor=\"#ffffcc\"";
                    }

                    var name = (string.IsNullOrEmpty(va.Name) ? va.Uid : va.Name) + "|" + string.Join(" x ", va.Shape.Dimensions);
                    _output.AppendFormat("{0}{1} [label=\"{2}\" shape=\"record\" {3}];\r\n", indent, va.Uid, name, style);
                }
            }
        }
Example #10
0
        protected override void EndProcessing()
        {
            Variable input;

            if (InputVariable == null)
            {
                input = null;
            }
            else if (InputVariable is Variable)
            {
                input = InputVariable as Variable;
            }
            else if (InputVariable is string)
            {
                input = FunctionFind.FindVariable(Expression, InputVariable as string);
                if (input == null)
                {
                    throw new ArgumentException("Input variable not found");
                }
            }
            else
            {
                throw new ArgumentException("Input variable should be a CNTK.Variable or a string");
            }

            Value value = null;

            if (InitialValue != null)
            {
                value = Converter.ToValue(InitialValue, input.Shape.Dimensions.ToArray());
            }

            var sampler = new ExpressionSampler(Name, Expression, input, MinibatchSize, value, IterationsPerEpoch);

            WriteObject(sampler);
        }
Example #11
0
        public static Value[] Invoke(this Function func, Dictionary <string, Value> arguments, DeviceDescriptor device = null, bool errorWhenArgumentUnused = true)
        {
            var inputs = new Dictionary <Variable, Value>();

            foreach (var entry in arguments)
            {
                var key = FunctionFind.FindVariable(func, entry.Key);
                if (key == null)
                {
                    if (errorWhenArgumentUnused)
                    {
                        throw new ArgumentException(string.Format("Unknown argument key '{0}'", entry.Key));
                    }
                    else
                    {
                        continue;
                    }
                }

                inputs.Add(key, entry.Value);
            }

            return(Invoke(func, inputs, device, errorWhenArgumentUnused));
        }
Example #12
0
        public static IList <Variable> FindVariables(Function func, string name)
        {
            var w = new FunctionFind(func, name, true, true);

            return(w._variables);
        }