Пример #1
0
        public void TestRegistryResolverModifyHierarchy()
        {
            Registry         rootRegistry = new Registry(tags: "root");
            RegistryResolver resolver     = new RegistryResolver(rootRegistry);
            Registry         trainer1     = new Registry(rootRegistry, tags: "trainer");
            Registry         trainer2     = new Registry(rootRegistry, new[] { "trainer" });

            Registry trainer1Architecture = new Registry(trainer1, new[] { "architecture" });
            Registry trainer2Architecture = new Registry(trainer2, new[] { "architecture" });

            rootRegistry["trainer1"] = trainer1;
            rootRegistry["trainer2"] = trainer2;

            trainer1["architecture"] = trainer1Architecture;
            trainer2["architecture"] = trainer2Architecture;

            trainer1Architecture["complexity"] = 2;
            trainer2Architecture["complexity"] = 3;

            Assert.AreEqual(new[] { 2, 3 }, resolver.ResolveGet <int>("*.architecture.complexity"));

            Registry differentTrainer1Architecture = new Registry(trainer1, "architecture");

            trainer1["architecture"] = differentTrainer1Architecture;
            differentTrainer1Architecture["complexity"] = 5;

            resolver.ResolveSet("*.architecture.complexity", 11, false, typeof(int));

            Assert.AreEqual(new[] { 11, 11 }, resolver.ResolveGet <int>("*.architecture.complexity"));
        }
Пример #2
0
        private void ApplyValueModifiers(IRegistry localRegistry, IComputationHandler handler)
        {
            if (_valueModifiers.Count == 0)
            {
                return;
            }

            RegistryResolver resolver = new RegistryResolver(localRegistry);

            foreach (string identifier in _valueModifiers.Keys)
            {
                string[] fullyResolvedIdentifiers;
                object[] values = resolver.ResolveGet <object>(identifier, out fullyResolvedIdentifiers);

                for (int i = 0; i < values.Length; i++)
                {
                    object   value     = values[i];
                    INDArray asNDArray = value as INDArray;
                    INumber  asNumber  = value as INumber;

                    if (asNDArray != null)
                    {
                        foreach (IValueModifier modifier in _valueModifiers[identifier])
                        {
                            asNDArray = modifier.Modify(fullyResolvedIdentifiers[i], asNDArray, handler);
                        }
                        values[i] = asNDArray;
                    }
                    else if (asNumber != null)
                    {
                        foreach (IValueModifier modifier in _valueModifiers[identifier])
                        {
                            asNumber = modifier.Modify(fullyResolvedIdentifiers[i], asNumber, handler);
                        }
                        values[i] = asNumber;
                    }
                    else
                    {
                        double?asDouble = value as double?;

                        if (asDouble != null)
                        {
                            foreach (IValueModifier modifier in _valueModifiers[identifier])
                            {
                                asDouble = modifier.Modify(fullyResolvedIdentifiers[i], asDouble.Value, handler);
                            }
                            values[i] = asDouble.Value;
                        }
                    }

                    resolver.ResolveSet(fullyResolvedIdentifiers[i], values[i]);
                }
            }
        }
Пример #3
0
        public void TestRegistryResolverModifyDirect()
        {
            Registry         rootRegistry = new Registry(tags: "root");
            RegistryResolver resolver     = new RegistryResolver(rootRegistry);

            Registry trainer1 = new Registry(rootRegistry, tags: "trainer");
            Registry trainer2 = new Registry(rootRegistry, tags: "trainer");

            rootRegistry["trainer1"] = trainer1;
            rootRegistry["trainer2"] = trainer2;

            //declare parameters in registry
            trainer1["accuracy"] = 0.0f;
            trainer2["accuracy"] = 0.0f;

            resolver.ResolveSet("trainer1.accuracy", 1.0f, false, typeof(float));
            resolver.ResolveSet("trainer2.accuracy", 2.0f, false, typeof(float));

            Assert.AreEqual(1.0f, resolver.ResolveGet <float>("trainer1.accuracy")[0]);
            Assert.AreEqual(2.0f, resolver.ResolveGet <float>("trainer2.accuracy")[0]);
        }
Пример #4
0
        public void TestRegistryResolverModifyComplex()
        {
            Registry         rootRegistry          = new Registry(tags: "root");
            RegistryResolver resolver              = new RegistryResolver(rootRegistry);
            Registry         trainer1              = new Registry(rootRegistry, tags: "trainer");
            Registry         trainer2              = new Registry(rootRegistry, new[] { "trainer" });
            Registry         weirdtrainer          = new Registry(rootRegistry, new[] { "trainer" });
            Registry         childRegistryToIgnore = new Registry(rootRegistry);

            Registry trainer1Architecture = new Registry(trainer1, new[] { "architecture" });
            Registry weirdarchitecture    = new Registry(weirdtrainer, new[] { "architecture" });

            rootRegistry["trainer1"]      = trainer1;
            rootRegistry["trainer2"]      = trainer2;
            rootRegistry["weirdtrainer"]  = weirdtrainer;
            rootRegistry["childtoignore"] = childRegistryToIgnore;

            trainer1["architecture"]     = trainer1Architecture;
            weirdtrainer["architecture"] = weirdarchitecture;

            trainer1Architecture["complexity"] = 2;
            weirdarchitecture["complexity"]    = 3;

            trainer1["accuracy"] = 0.0f;
            trainer2["accuracy"] = 0.0f;

            resolver.ResolveSet("trainer*.accuracy", 1.0f, false, typeof(float));

            resolver.ResolveSet("*<trainer>.*<architecture>.complexity", 9, false, typeof(int));

            string[] resolved = null;

            Assert.AreEqual(new[] { 1.0f, 1.0f }, resolver.ResolveGet <float>("trainer*.accuracy", out resolved));
            Assert.AreEqual(new[] { "trainer1.accuracy", "trainer2.accuracy" }, resolved);

            Assert.AreEqual(new[] { 9, 9 }, resolver.ResolveGet <int>("*<trainer>.architecture.complexity"));

            Assert.AreEqual(new IRegistry[] { trainer1, trainer2, weirdtrainer }, resolver.ResolveGet <IRegistry>("*<trainer>", out resolved));
            Assert.AreEqual(new[] { "trainer1", "trainer2", "weirdtrainer" }, resolved);
        }
Пример #5
0
        /// <summary>
        ///     Specify how multiple networks are merged into a single one. <see cref="root" /> is <em>not</em>
        ///     considered for the calculation. It is merely the storage container. (Although root can also be in
        ///     <see cref="networks" />).
        /// </summary>
        /// <param name="root">
        ///     The root network that will be modified. Since the <see cref="INetworkMerger" /> does not know how
        ///     to create a <see cref="INetwork" />, it will be passed not returned.
        /// </param>
        /// <param name="networks">
        ///     The networks that will be merged into the <see cref="root" />. Can contain <see cref="root" />
        ///     itself.
        /// </param>
        /// <param name="handler">
        ///     A handler can be specified optionally. If not passed (but required),
        ///     <see cref="ITraceable.AssociatedHandler" /> will be used.
        /// </param>
        public void Merge(INetwork root, IEnumerable <INetwork> networks, IComputationHandler handler = null)
        {
            IRegistryResolver rootResolver = new RegistryResolver(root.Registry);

            string[] mergeKeys = CopyMatchIdentifiers();

            if (mergeKeys.Length == 0)
            {
                Log.Warn($"Attempted merge network {root} with networks {networks} using handler {handler} but no merge keys were set so nothing will happen. This is probably not intended.");
            }

            // mapping of resolved mergeEnetry and all data
            IDictionary <string, IList <object> > resolvedDataArrays = new Dictionary <string, IList <object> >(mergeKeys.Length);
            int numNetworks = 0;

            // fill the mapping of all values
            foreach (INetwork network in networks)
            {
                IRegistryResolver resolver = new RegistryResolver(network.Registry);

                foreach (string mergeKey in mergeKeys)
                {
                    string[] fullyResolvedIdentifiers;
                    object[] values = resolver.ResolveGet <object>(mergeKey, out fullyResolvedIdentifiers);

                    Debug.Assert(fullyResolvedIdentifiers.Length == values.Length);

                    for (int i = 0; i < values.Length; i++)
                    {
                        IList <object> allValuesAtKey = resolvedDataArrays.TryGetValue(fullyResolvedIdentifiers[i], () => new List <object>());

                        allValuesAtKey.Add(values[i]);
                    }
                }

                numNetworks++;
            }

            foreach (KeyValuePair <string, IList <object> > keyDataPair in resolvedDataArrays)
            {
                int numObjects = keyDataPair.Value.Count;

                if (numObjects != numNetworks)
                {
                    _log.Warn($"Inconsistent network states for identifier \"{keyDataPair.Key}\", only {keyDataPair.Value.Count} have it but there are {numNetworks} networks.");
                }

                object merged = Merge(keyDataPair.Value.ToArray(), handler);
                rootResolver.ResolveSet(keyDataPair.Key, merged);
            }
        }
Пример #6
0
 /// <summary>
 /// Set a single given value of a certain type to all matching identifiers. For the detailed supported syntax <see cref="IRegistryResolver"/>.
 /// Note: The individual registries might throw an exception if a type-protected value is set to the wrong type.
 /// </summary>
 /// <typeparam name="T">The type of the value.</typeparam>
 /// <param name="matchIdentifier">The full match identifier. </param>
 /// <param name="value">The value to set.</param>
 /// <param name="addIdentifierIfNotExists">Indicate if the last (local) identifier should be added if it doesn't exist yet.</param>
 /// <param name="associatedType">Optionally set the associated type (<see cref="IRegistry"/>). If no associated type is set, the one of the registry will be used (if set). </param>
 /// <returns>A list of fully qualified matches to the match identifier.</returns>
 public string[] ResolveSet <T>(string matchIdentifier, T value, bool addIdentifierIfNotExists = false, Type associatedType = null)
 {
     return(RegistryResolver.ResolveSet(matchIdentifier, value, addIdentifierIfNotExists, associatedType));
 }