Exemple #1
0
        internal float[] Compute(ComputeDevice mathLib, float[] input, ref List <float[]> activations, ref List <float[]> zValues, bool flushMathlibWorkingCache)
        {
            var  current      = input;
            bool applySigmoid = zValues == null;
            PasstroughActivation passtroughActivation = applySigmoid ? null : new PasstroughActivation();

            foreach (var layer in layers)
            {
                current = layer.Compute(mathLib, current, applySigmoid ? activationFunction : passtroughActivation);
                if (zValues != null)
                {
                    zValues.Add((float[])current.Clone());
                    for (int i = 0; i < current.Length; i++)
                    {
                        current[i] = activationFunction.Calculate(current[i]);
                    }
                }
                if (activations != null)
                {
                    activations.Add(current);
                }
            }

            if (flushMathlibWorkingCache)
            {
                mathLib.FlushWorkingCache();
            }

            return(current);
        }
        public override float[] CalculateLayer(float[,] weightMx, float[] bias, float[] prevActivations, IActivationFunction sigmoidFunction)
        {
            float[] ret = new float[weightMx.GetLength(0)];
            for (int m = 0; m < weightMx.GetLength(0); m++)
            {
                float acc = 0.0f;
                for (int k = 0; k < weightMx.GetLength(1); k++)
                {
                    acc += weightMx[m, k] * prevActivations[k];
                }
                acc += bias[m];

                ret[m] = sigmoidFunction.Calculate(acc);
            }
            return(ret);
        }