public NeuralNetwork CreateNetwork(NNAlgorithm supportedAlgorithms = NNAlgorithm.None)
        {
            Validate();

            var layers = CreateLayers();

            if (layers == null || layers.Count == 0) throw new InvalidOperationException("Created layer collection is null or empty.");

            var nnType = InitParameters.NeuralNetworkType;

            var constructor = (from ci in nnType.GetConstructors()
                               let pars = ci.GetParameters()
                               where pars.Length == 3 &&
                                  pars[0].ParameterType.IsAssignableFrom(layers.GetType()) &&
                                  pars[1].ParameterType.IsAssignableFrom(InitParameters.GetType()) &&
                                  pars[2].ParameterType.IsAssignableFrom(typeof(NNAlgorithm))
                               select ci).FirstOrDefault();

            if (constructor == null) throw new InvalidOperationException("Architecture compatible constructor not found on Neural Network type '" + nnType + "'.");

            try
            {
                return (NeuralNetwork)constructor.Invoke(new object[] { layers, InitParameters, supportedAlgorithms });
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Cannot invoke Architecture compatible constructor on Neural Network type '" + nnType + "'.", ex);
            }
        }
 public ManagedNeuralNetwork(ICollection<ConnectableLayer> layers, ManagedNNInitParameters parameters, NNAlgorithm supportedAlgorithms = NNAlgorithm.None)
     : base(layers, parameters, supportedAlgorithms)
 {
     Contract.Requires(layers != null);
     Contract.Requires(layers.Count != 0);
     Contract.Requires(parameters != null);
 }
        /// <summary>
        /// Sorts the <see cref="Color"/> values.
        /// </summary>
        /// <remarks>This method try to sort the colors using the Nearest Neighbour algorithm, trying to ensure that the black color (0, 0, 0), if exists in palette, be the first color.
        /// This implementation is based on this article: https://www.alanzucconi.com/2015/09/30/colour-sorting/ </remarks>
        public void Sort()
        {
#if SORT_BY_HSV
            var vectors = this._colors.Select(e => e.ToHSV(ColorFormat.DAC)).ToList();
#elif SORT_BY_HSL
            var vectors = this._colors.Select(e => e.ToHSL(ColorFormat.DAC)).ToList();
#elif SORT_BY_HLV_STEP
            var vectors = this._colors.Select(e => e.Step()).ToList();
#else
            var vectors = this._colors.Select(e => e.Normalize(ColorFormat.DAC)).ToList();
#endif
            int        start = vectors.FindIndex(e => e == Vector3.Zero); // Try to localize the black color.
            List <int> path  = NNAlgorithm.CalculatePath(vectors, start == -1 ? 0 : start, out _);

            this._colors = path.Select(e => this._colors[e]).ToArray();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            String dataFilePath    = args[0];
            String weightsFilePath = args[1];
            String outputFilePath  = args[2];
            int    metric          = Int32.Parse(args[3]);
            int    numThreads      = Int32.Parse(args[4]);


            Stopwatch w = new Stopwatch();

            w.Start();

            List <double[]> samples = CSVIO.Load <double>(dataFilePath);
            List <double[]> weights = CSVIO.Load <double>(weightsFilePath);

            w.Stop();

            long loadingMS = w.ElapsedMilliseconds;

            w.Reset();

            w.Start();

            DistanceFunctions distFunc = distFunc = new SquareEuclideanDistanceFunction();

            if (metric == 2)
            {
                distFunc = new Cosine();
            }
            if (metric == 3)
            {
                distFunc = new Pearson();
            }

            Console.WriteLine("Using distance function with brute force: {0} and numthreads: {1}", metric, numThreads);

            NNAlgorithm nnMethod = null;

            // if euclidean then can use fast kdtree
            if (metric == 1)
            {
                nnMethod = new KDTreeNN(weights, distFunc);
            }
            else
            {
                nnMethod = new BruteForceNN(weights, distFunc);
            }

            List <int[]> nearestNeighbours = NearestNeighbour.GetNearestNeighbours(samples, nnMethod, numThreads);

            w.Stop();

            long vqMS = w.ElapsedMilliseconds;

            w.Reset();


            w.Start();
            CSVIO.Save <int>(outputFilePath, nearestNeighbours);
            w.Stop();

            long savingMS = w.ElapsedMilliseconds;

            Console.WriteLine("Loading Time: {0} NN Time: {1} Saving Time: {2}", loadingMS, vqMS, savingMS);
        }