예제 #1
0
파일: RNNCell.cs 프로젝트: arnavdas88/dnn
        /// <summary>
        /// Initializes the <see cref="StochasticLayer"/>.
        /// </summary>
        /// <param name="direction">The cell direction (forward-only or bi-directional).</param>
        /// <param name="numberOfNeurons">The number of neurons in the layer.</param>
        /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param>
        /// <param name="weightsShape">The dimensions of the layer's weights tensor.</param>
        /// <param name="hiddenShape">The dimensions of the layer's hidden weights tensor.</param>
        /// <param name="biasesShape">The dimensions of the layer's biases tensor.</param>
        /// <param name="random">The random numbers generator.</param>
        private protected void Initialize(
            RNNDirection direction,
            int numberOfNeurons,
            MatrixLayout matrixLayout,
            int[] weightsShape,
            int[] hiddenShape,
            int[] biasesShape,
            RandomNumberGenerator <float> random)
        {
            if (hiddenShape == null)
            {
                throw new ArgumentNullException(nameof(hiddenShape));
            }

            if (random == null)
            {
                random = new RandomRangeGenerator(-0.08f, 0.08f);
            }

            if (direction == RNNDirection.BiDirectional && (numberOfNeurons % 2) != 0)
            {
                throw new ArgumentException("The number of neurons in a bi-directional RNN must be even.", nameof(numberOfNeurons));
            }

            this.Direction = direction;

            this.Initialize(numberOfNeurons, matrixLayout, weightsShape, biasesShape, random);

            this.U = new Tensor("hidden weights", hiddenShape);
            this.U.Randomize(random);
        }
예제 #2
0
        /*[TestMethod]
         * public void TrainTest2()
         * {
         *  const int LearnCount = 100;
         *  const int TestCount = 1000;
         *  const int Length = 300;
         *  const double PositiveRatio = 0.1;
         *
         *  // create samples
         *  List<(double[] x, bool y, double weight)> samples = SupportVectorMachineTest.GenerateSamples(LearnCount + TestCount, Length, PositiveRatio).ToList();
         *
         *  // learn
         *  SequentialMinimalOptimization<Accord.Statistics.Kernels.IKernel> optimization = new SequentialMinimalOptimization<Accord.Statistics.Kernels.IKernel>()
         *  {
         *      Kernel = new Accord.Statistics.Kernels.ChiSquare(),
         *      Tolerance = 0.01f,
         *      Strategy = SelectionStrategy.SecondOrder
         *  };
         *
         *  optimization.Learn(
         *      samples.Take(LearnCount).Select(x => x.x.Select(w => (double)w).ToArray()).ToArray(),
         *      samples.Take(LearnCount).Select(x => x.y).ToArray());
         * }*/

        private static IEnumerable <(double[] x, bool y, double weight)> GenerateSamples(int count, int length, double positiveRatio)
        {
            Random random = new Random(0);
            BinarySampleGenerator sampleGenerator = new BinarySampleGenerator(positiveRatio, random);

            double[] weights = new RandomRangeGenerator(-1, 1, random).Generate(length).Select(x => (double)x).ToArray();

            while (count > 0)
            {
                double[] sample = sampleGenerator.Generate(length).Select(x => x ? 1.0 : 0.0).ToArray();

                double res = Matrix.DotProduct(length, sample, 0, weights, 0);
                if (res >= 1)
                {
                    yield return(sample, true, 1.0f);

                    count--;
                }
                else if (res <= -1)
                {
                    yield return(sample, false, 1.0f);

                    count--;
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Initializes the <see cref="GRUCell"/>.
        /// </summary>
        /// <param name="shape">The dimensions of the layer's input tensor.</param>
        /// <param name="direction">The cell direction (forward-only or bi-directional).</param>
        /// <param name="numberOfNeurons">The number of neurons in the layer.</param>
        /// <param name="matrixLayout">Specifies whether the weight matrices are row-major or column-major.</param>
        /// <param name="random">The random numbers generator.</param>
        private void Initialize(
            Shape shape,
            RNNDirection direction,
            int numberOfNeurons,
            MatrixLayout matrixLayout,
            RandomNumberGenerator <float> random)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            if (random == null)
            {
                random = new RandomRangeGenerator(-0.08f, 0.08f);
            }

            int[] axes = shape.Axes;

            // column-major matrix organization - each row contains all weights for one neuron
            // row-major matrix organization - each column contains all weights for one neuron
            int xlen = axes.Skip(1).Aggregate(1, (total, next) => total * next);

            int[] weightsShape = matrixLayout == MatrixLayout.ColumnMajor ?
                                 new[] { xlen, 3 * numberOfNeurons } :
            new[] { 3 * numberOfNeurons, xlen };

            // keep all weights in single channel
            // allocate three matrices (one for each of two gates and one for the state)
            int[] biasesShape = new[] { 3 * numberOfNeurons };

            int hlen = direction == RNNDirection.ForwardOnly ? numberOfNeurons : numberOfNeurons / 2;

            int[] hiddenShape = matrixLayout == MatrixLayout.ColumnMajor ?
                                new[] { hlen, 3 * numberOfNeurons } :
            new[] { 3 * numberOfNeurons, hlen };

            this.Initialize(
                direction,
                numberOfNeurons,
                matrixLayout,
                weightsShape,
                hiddenShape,
                biasesShape,
                random ?? new RandomRangeGenerator(-0.08f, 0.08f));

            this.OutputShape = new Shape(new int[] { shape.GetAxis(0), numberOfNeurons });

            // initialize biases for update and reset gates only
            Vectors.Set(2 * numberOfNeurons, 1.0f, this.B.Weights, 0);
        }