Exemplo n.º 1
0
        public ANNLayerDef(ANNDef parent, string name, int widthIn, int widthOut, Func <float[], object, float[]> customFunc)
        {
            Parent   = parent;
            Name     = name;
            WidthIn  = widthIn;
            WidthOut = widthOut;

            Subtype    = LayerSubtype.CustomFunc;
            CustomFunc = customFunc;
        }
Exemplo n.º 2
0
        public ANNLayerDef(ANNDef parent, string name, int widthIn, int widthOut,
                           float[,] weights, float[] biases,
                           ActivationType activation, LayerSubtype subtype = LayerSubtype.Normal,
                           float[] movingMeans = null, float[] movingVariances = null,
                           float[] betas       = null, float[] gammas = null,
                           float clipValue     = float.NaN,
                           int sparseBinaryInputMaxNonzerPerRow = 0)
        {
            Parent        = parent;
            Name          = name;
            Activation    = activation;
            WidthIn       = widthIn;
            WidthOut      = widthOut;
            Weights       = weights;
            WeightsTr     = new float[weights.GetLength(1), weights.GetLength(0)];
            WeightsFP16Tr = new FP16[weights.GetLength(1), weights.GetLength(0)];
            InputLayerMaxCountNonzeroPerRow = sparseBinaryInputMaxNonzerPerRow;

            if (weights.GetLength(0) != widthIn || weights.GetLength(1) != widthOut)
            {
                throw new Exception("incorrect weight shape");
            }

            //Weights1D = new float[WidthIn * WidthOut];
            Weights1D   = new AlignedFloatArray(WidthIn * WidthOut, 128).GetManagedArray();
            Weights1DTr = new AlignedFloatArray(WidthIn * WidthOut, 128).GetManagedArray();
            int offset = 0;

            for (int i = 0; i < WidthIn; i++)
            {
                for (int j = 0; j < WidthOut; j++)
                {
                    WeightsTr[j, i]              = weights[i, j];
                    WeightsFP16Tr[j, i]          = (FP16)weights[i, j];
                    Weights1D[offset++]          = weights[i, j];
                    Weights1DTr[j * WidthIn + i] = weights[i, j];
                }
            }

            // Prepare copy of weights for AVX2 (8 floats at a time) where stride is 8
            if (WidthOut >= 8)
            {
                Weights1DTrBlocked8 = MathUtils.BlockedMatrix(Weights1DTr, WidthOut, WidthIn, 8, 8);
            }

            Biases      = new AlignedFloatArray(biases.Length, 128, biases).GetManagedArray();
            Subtype     = subtype;
            MovingMeans = movingMeans;
            float[] MovingVariances = movingVariances;
            Betas        = betas;
            Gammas       = gammas;
            ClipValueMax = clipValue;

            if (MovingVariances != null)
            {
                // Precompute standard deviations for speed
                MovingStdDevs = new AlignedFloatArray(MovingVariances.Length, 128, biases).GetManagedArray();
                for (int i = 0; i < MovingStdDevs.Length; i++)
                {
                    MovingStdDevs[i] = (float)System.Math.Sqrt(MovingVariances[i] + EPSILON);
                }
            }
        }