示例#1
0
                                                                                               public static ANNDef ReadANNFromKeras(string fnH5Weights, string fnJSONStructure)
        {
            ANNDef ann = new ANNDef();

            Console.WriteLine("** LOAD file " + fnH5Weights);
            if (!File.Exists(fnH5Weights))
            {
                throw new Exception("*** File not found");
            }

            const bool READ_ONLY = true;
            long       file      = Hdf5.OpenFile(fnH5Weights, READ_ONLY);

            if (file < 0)
            {
                throw new Exception("unable to find/open file " + fnH5Weights);
            }

            bool inputLayerIsSparse = false;

            JsonDocument            o       = JsonDocument.Parse(File.ReadAllText(fnJSONStructure));
            List <(string, ANNDef)> subnets = new();

            inputLayerIsSparse = ExtractNetwork(ann, subnets, file, inputLayerIsSparse, o.RootElement, null);
            if (subnets.Count > 0)
            {
                ann.InputSubnetworks = subnets;
            }
            H5G.close(file);

            return(ann);
        }
示例#2
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;
        }
示例#3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="def"></param>
        /// <param name="multiCount"></param>
        public ANNCalcResult(ANNDef def, int multiCount)
        {
            if (multiCount <= 0)
            {
                throw new ArgumentException("multiCount must be positive");
            }

            Parent     = def;
            MultiCount = multiCount;
            LayerOutputBuffersSingle = new float[def.Layers.Count][];

            for (int i = 0; i < def.Layers.Count; i++)
            {
                LayerOutputBuffersSingle[i] = new AlignedFloatArray(def.Layers[i].WidthOut, 128).GetManagedArray();
            }
        }
示例#4
0
 private static bool ExtractNetwork(ANNDef ann, List <(string, ANNDef)> subnets, long file,
示例#5
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);
                }
            }
        }