Example #1
0
        public double[][] GetNormalizedBinaryCodes(IActivationFunction function, int binaryLength)
        {
            byte[][] codes  = BinaryOutputUtil.GetAllBinaryCodes(binaryLength);
            int      length = codes.GetLength(0);

            double[][] fCodes = new double[length][];
            for (int i = 0; i < length; i++)
            {
                fCodes[i] = Array.ConvertAll(codes[i], s => (double)s);
                NormalizeUtils.NormalizeOneDesiredOutputInPlace(function, fCodes[i]);
            }

            return(fCodes);
        }
Example #2
0
        protected void ReorderOutput(Network network, BasicNeuralDataSet dataset, Dictionary <int, List <BasicMLData> > trackIdFingerprints, double[][] binaryCodes)
        {
            int outputNeurons = network.GetLayerNeuronCount(network.LayerCount - 1);
            int trackCount    = trackIdFingerprints.Count;

            // For each song, compute Am
            double[][] am      = new double[trackCount][];
            int        counter = 0;

            foreach (KeyValuePair <int, List <BasicMLData> > pair in trackIdFingerprints)
            {
                List <BasicMLData> sxSnippet = pair.Value;
                if (sxSnippet.Count < trainingSongSnippets)
                {
                    throw new NetTrainerException("Not enough snippets for a song");
                }

                am[counter] = new double[outputNeurons];
                foreach (BasicMLData snippet in sxSnippet)
                {
                    IMLData actualOutput = network.Compute(snippet);
                    for (int k = 0; k < outputNeurons; k++)
                    {
                        actualOutput[k] /= outputNeurons;
                        am[counter][k]  += actualOutput[k];
                    }
                }

                counter++;
            }

            // Get a collection of tracks (shallow copy)
            int[] unassignedTracks = new int[trackCount];
            int   countTrack       = 0;

            foreach (KeyValuePair <int, List <BasicMLData> > item in trackIdFingerprints)
            {
                unassignedTracks[countTrack++] = item.Key;
            }

            int currItteration = 0;

            // Find binary code - track pair that has min l2 norm across all binary codes
            List <Tuple <int, int> > binCodeTrackPair = BinaryOutputUtil.FindMinL2Norm(binaryCodes, am);

            foreach (Tuple <int, int> pair in binCodeTrackPair)
            {
                // Set the input-output for all fingerprints of that song
                List <BasicMLData> songFingerprints = trackIdFingerprints[unassignedTracks[pair.Item2]];
                foreach (BasicMLData songFingerprint in songFingerprints)
                {
                    for (int i = 0, n = songFingerprint.Count; i < n; i++)
                    {
                        dataset.Data[currItteration].Input[i] = songFingerprint[i];
                    }

                    for (int i = 0, n = binaryCodes[pair.Item1].Length; i < n; i++)
                    {
                        dataset.Data[currItteration].Ideal[i] = binaryCodes[pair.Item1][i];
                    }

                    currItteration++;
                }
            }
        }