Esempio n. 1
0
        public void Infer(L2Normalization layer, L2NormalizationLayerArgument argument, InferenceContext context)
        {
            var inputAlloc  = context.MainMemoryMap[layer.Input.Connection.From];
            var outputAlloc = context.MainMemoryMap[layer.Output];

            argument.Flags = K210LayerFlags.MainMemoryOutput;
            argument.MainMemoryInputAddress  = inputAlloc.GetAddress();
            argument.MainMemoryOutputAddress = outputAlloc.GetAddress();
        }
Esempio n. 2
0
        public L2NormalizationLayerArgument DeserializeBin(int offset, K210BinDeserializeContext context)
        {
            var sr       = context.GetReaderAt(offset);
            var argument = new L2NormalizationLayerArgument
            {
                Flags = sr.Read <K210LayerFlags>(),
                MainMemoryInputAddress  = sr.Read <uint>(),
                MainMemoryOutputAddress = sr.Read <uint>(),
                Channels = sr.Read <uint>()
            };

            return(argument);
        }
Esempio n. 3
0
        public void Forward(L2NormalizationLayerArgument argument, ForwardContext context)
        {
            var src  = MemoryMarshal.Cast <byte, float>(context.GetMainRamAt((int)argument.MainMemoryInputAddress));
            var dest = MemoryMarshal.Cast <byte, float>(context.GetMainRamAt((int)argument.MainMemoryOutputAddress));

            float       sum     = 0;
            const float epsilon = 1e-10f;

            for (int oc = 0; oc < argument.Channels; oc++)
            {
                sum += src[oc] * src[oc];
            }
            if (sum < epsilon)
            {
                sum = epsilon;
            }
            sum = 1f / (float)Math.Sqrt(sum);

            for (int oc = 0; oc < argument.Channels; oc++)
            {
                dest[oc] = src[oc] * sum;
            }
        }