Exemplo n.º 1
0
        public static double[] Predict(string s, WriteToCMDLine write)
        {
            NeuralNetwork net  = GetNetwork(write);
            Alpha         a    = new Alpha(write);
            AlphaContext  ctxt = new AlphaContext(datatype, write);

            double[] Results = a.Forward(s, ctxt);
            Results.WriteArray("Alpha Results : ", write);
            for (int i = 0; i < net.Layers.Count(); i++)
            {
                Results = net.Layers[i].Output(Results);
            }
            return(Results);
        }
Exemplo n.º 2
0
        public static double Propogate
            (Sample s, WriteToCMDLine write, bool tf = false)
        {
            double error = 0;
            var    Pred  = Predict(s.TextInput, CMDLibrary.WriteNull);

            if (s.DesiredOutput.ToList().IndexOf(s.DesiredOutput.Max()) != Pred.ToList().IndexOf(Pred.Max()) || tf)
            {
                NeuralNetwork net      = GetNetwork(write);
                var           Samples  = s.ReadSamples(24);
                Alpha         a        = new Alpha(write);
                AlphaContext  ctxt     = new AlphaContext(datatype, write);
                NetworkMem    NetMem   = new NetworkMem(net);
                NetworkMem    AlphaMem = new NetworkMem(a.Network);
                NetworkMem    CtxtMem  = new NetworkMem(ctxt.Network);

                Parallel.For(0, Samples.Count(), j =>
                {
                    AlphaMem am = new AlphaMem(Samples[j].TextInput.ToCharArray());
                    var output  = a.Forward(Samples[j].TextInput, ctxt, am);
                    var F       = net.Forward(output, dropout, write);
                    error      += CategoricalCrossEntropy.Forward(F.Last().GetRank(0), Samples[j].DesiredOutput).Max();

                    var DValues = net.Backward(F, Samples[j].DesiredOutput, NetMem, write);
                    a.Backward(Samples[j].TextInput, DValues, ctxt, am, AlphaMem, CtxtMem);
                });
                NetMem.Update(Samples.Count(), 0.00001, net);
                AlphaMem.Update(Samples.Count(), 0.00001, a.Network);
                CtxtMem.Update(Samples.Count(), 0.00001, ctxt.Network);
                write("Pre Training Error : " + error);

                net.Save();
                a.Network.Save();
                ctxt.Network.Save(datatype);

                error = 0;
                Parallel.For(0, Samples.Count(), j =>
                {
                    AlphaMem am = new AlphaMem(Samples[j].TextInput.ToCharArray());
                    var output  = a.Forward(Samples[j].TextInput, ctxt, am);
                    var F       = net.Forward(output, dropout, write);
                    error      += CategoricalCrossEntropy.Forward(F.Last().GetRank(0), Samples[j].DesiredOutput).Max();
                });
                write("Post Training Error : " + error);

                s.Save();
            }
            return(error);
        }
Exemplo n.º 3
0
        public void Propogate
            (Sample s, WriteToCMDLine write)
        {
            var check = Predict(s);

            if (s.DesiredOutput.ToList().IndexOf(s.DesiredOutput.Max()) != check.ToList().IndexOf(check.Max()))
            {
                Alpha         a       = new Alpha(write);
                AlphaContext  ctxt1   = new AlphaContext(datatype, write);
                AlphaContext  ctxt2   = new AlphaContext(datatype, write, 1);
                var           Samples = s.ReadSamples();
                List <string> lines   = new List <string>();
                for (int i = 0; i < 5; i++)
                {
                    NetworkMem ObjMem   = new NetworkMem(Network);
                    NetworkMem AlphaMem = new NetworkMem(a.Network);
                    NetworkMem CtxtMem1 = new NetworkMem(ctxt1.Network);
                    NetworkMem CtxtMem2 = new NetworkMem(ctxt2.Network);

                    Parallel.For(0, Samples.Count(), j =>
                    {
                        AlphaMem am                    = new AlphaMem(Samples[j].TextInput.ToCharArray());
                        Samples[j].TextOutput          = a.Forward(Samples[j].TextInput, ctxt1, am);
                        AlphaMem am2                   = new AlphaMem(Samples[j].SecondaryText.ToCharArray());
                        Samples[j].SecondaryTextOutput = a.Forward(Samples[j].SecondaryText, ctxt2, am2);
                        var F = Forward(Samples[j]);
                        lines.AddRange(Samples[j].OutputError(CategoricalCrossEntropy.Forward(F.Last(), Samples[j].DesiredOutput)));

                        var DValues = Backward(Samples[j], F, ObjMem);
                        var DV1     = DValues.ToList().Take(Alpha.DictSize).ToArray();
                        var DV2     = Enumerable.Reverse(DValues).Take(Alpha.DictSize).Reverse().ToArray();
                        a.Backward(Samples[j].TextInput, DV1, ctxt1, am, AlphaMem, CtxtMem1);
                        a.Backward(Samples[j].SecondaryText, DV2, ctxt2, am2, AlphaMem, CtxtMem2);
                    });
                    ObjMem.Update(1, 0.0001, Network);
                    AlphaMem.Update(1, 0.00001, a.Network);
                    CtxtMem1.Update(1, 0.0001, ctxt1.Network);
                    CtxtMem2.Update(1, 0.0001, ctxt2.Network);
                }
                lines.ShowErrorOutput();
                Network.Save();
                a.Network.Save();
                ctxt1.Save();
                ctxt2.Save();

                s.Save();
            }
        }
        public double[] Predict(Sample s)
        {
            Alpha a     = new Alpha(CMDLibrary.WriteNull);
            var   input = a.Forward(s.TextInput, new AlphaContext(datatype, CMDLibrary.WriteNull)).ToList();

            input.AddRange(a.Forward(s.SecondaryText, new AlphaContext(datatype, CMDLibrary.WriteNull, 1)));
            input.AddRange(s.ValInput);
            var Results = input.ToArray();

            for (int i = 0; i < Network.Layers.Count(); i++)
            {
                Results = Network.Layers[i].Output(Results);
            }

            return(Results);
        }
Exemplo n.º 5
0
        public static double Propogate
            (WriteToCMDLine write)
        {
            double        error    = 0;
            NeuralNetwork net      = GetNetwork(write);
            var           Samples  = ReadVals(24);
            Alpha         a        = new Alpha(write);
            AlphaContext  ctxt     = new AlphaContext(datatype, write);
            NetworkMem    OLFMem   = new NetworkMem(net);
            NetworkMem    AlphaMem = new NetworkMem(a.Network);
            NetworkMem    CtxtMem  = new NetworkMem(ctxt.Network);

            Parallel.For(0, Samples.Count(), j =>
            {
                AlphaMem am = new AlphaMem(Samples.Keys.ToList()[j].ToCharArray());
                var output  = a.Forward(Samples.Keys.ToList()[j], ctxt, am);
                var F       = net.Forward(output, dropout, write);
                var desired = new double[Enum.GetNames(typeof(Command)).Length];
                desired[Samples.Values.ToList()[j]] = 1;
                error += CategoricalCrossEntropy.Forward(F.Last().GetRank(0), desired).Max();

                var DValues = net.Backward(F, desired, OLFMem, write);
                a.Backward(Samples.Keys.ToList()[j], DValues, ctxt, am, AlphaMem, CtxtMem);
            });
            OLFMem.Update(Samples.Count(), 0.0001, net);
            AlphaMem.Update(Samples.Count(), 0.0001, a.Network);
            CtxtMem.Update(Samples.Count(), 0.0001, ctxt.Network);
            write("Pre Training Error : " + error);

            net.Save();
            a.Network.Save();
            ctxt.Network.Save(datatype);

            error = 0;
            Parallel.For(0, Samples.Count(), j =>
            {
                AlphaMem am = new AlphaMem(Samples.Keys.ToList()[j].ToCharArray());
                var output  = a.Forward(Samples.Keys.ToList()[j], ctxt, am);
                var F       = net.Forward(output, dropout, write);
                var desired = new double[Enum.GetNames(typeof(Command)).Length];
                desired[Samples.Values.ToList()[j]] = 1;
                error += CategoricalCrossEntropy.Forward(F.Last().GetRank(0), desired).Max();
            });
            write("Post Training Error : " + error);
            return(error);
        }
Exemplo n.º 6
0
        public static double Propogate
            (WriteToCMDLine write, bool tf = false)
        {
            double        error    = 0;
            NeuralNetwork net      = GetNetwork(write);
            var           Samples  = datatype.ReadSamples(24);
            Alpha         a        = new Alpha(write);
            AlphaContext  ctxt     = new AlphaContext(datatype, write);
            NetworkMem    OLFMem   = new NetworkMem(net);
            NetworkMem    AlphaMem = new NetworkMem(a.Network);
            NetworkMem    CtxtMem  = new NetworkMem(ctxt.Network);

            Parallel.For(0, Samples.Count(), j =>
            {
                AlphaMem am = new AlphaMem(Samples[j].TextInput.ToCharArray());
                var output  = a.Forward(Samples[j].TextInput, ctxt, am);
                var F       = net.Forward(output, dropout, write);
                error      += CategoricalCrossEntropy.Forward(F.Last().GetRank(0), Samples[j].DesiredOutput).Max();

                var DValues = net.Backward(F, Samples[j].DesiredOutput, OLFMem, write);
                a.Backward(Samples[j].TextInput, DValues, ctxt, am, AlphaMem, CtxtMem);
            });
            OLFMem.Update(Samples.Count(), 0.0001, net);
            AlphaMem.Update(Samples.Count(), 0.0001, a.Network);
            CtxtMem.Update(Samples.Count(), 0.0001, ctxt.Network);
            write("Pre Training Error : " + error);

            net.Save();
            a.Network.Save();
            ctxt.Network.Save(Datatype.OccupantLoadFactor);

            error = 0;
            Parallel.For(0, Samples.Count(), j =>
            {
                AlphaMem am = new AlphaMem(Samples[j].TextInput.ToCharArray());
                var output  = a.Forward(Samples[j].TextInput, ctxt, am);
                var F       = net.Forward(output, dropout, write);
                error      += CategoricalCrossEntropy.Forward(F.Last().GetRank(0), Samples[j].DesiredOutput).Max();
            });
            write("Post Training Error : " + error);

            return(error);
        }