Пример #1
0
        public void LSTM_Test_Params_Count()
        {
            //define values, and variables
            Variable x = Variable.InputVariable(new int[] { 2 }, DataType.Float, "input");
            Variable y = Variable.InputVariable(new int[] { 3 }, DataType.Float, "output");

            //Number of LSTM parameters
            var lstm1 = RNN.RecurrenceLSTM(x, 3, 3, DataType.Float, device, false, Activation.TanH, false, false, 1);

            var ft     = lstm1.Inputs.Where(l => l.Uid.StartsWith("Parameter")).ToList();
            var consts = lstm1.Inputs.Where(l => l.Uid.StartsWith("Constant")).ToList();
            var inp    = lstm1.Inputs.Where(l => l.Uid.StartsWith("Input")).ToList();

            //bias params
            var bs      = ft.Where(p => p.Name.Contains("_b")).ToList();
            var totalBs = bs.Sum(v => v.Shape.TotalSize);

            Assert.Equal(12, totalBs);
            //weights
            var ws      = ft.Where(p => p.Name.Contains("_w")).ToList();
            var totalWs = ws.Sum(v => v.Shape.TotalSize);

            Assert.Equal(24, totalWs);
            //update
            var us      = ft.Where(p => p.Name.Contains("_u")).ToList();
            var totalUs = us.Sum(v => v.Shape.TotalSize);

            Assert.Equal(36, totalUs);

            //total number opf parameters
            var totalOnly = totalBs + totalWs + totalUs;

            Assert.Equal(72, totalOnly);
        }
Пример #2
0
        public void lstm_test01()
        {
            //define values, and variables
            Variable x       = Variable.InputVariable(new int[] { 4 }, DataType.Float, "input");
            var      xValues = Value.CreateBatchOfSequences <float>(new int[] { 4 }, mData, device);

            //
            var lstm00 = RNN.RecurrenceLSTM(x, 3, 3, DataType.Float, device, false, Activation.TanH, true, true, 1);

            //
            LSTMReccurentNN lstmNN = new LSTMReccurentNN(1, 1, device);
            //lstm implme reference 01
            var lstmCell = lstmNN.CreateLSTM(x, "output1");
            var lstm01   = CNTKLib.SequenceLast(lstmCell.h);

            //lstme implementation refe 02
            var lstm02 = LSTMSequenceClassifier.LSTMNet(x, 1, device, "output1");

            //
            var wParams00 = lstm00.Inputs.Where(p => p.Uid.Contains("Parameter")).ToList();
            var wParams01 = lstm00.Inputs.Where(p => p.Uid.Contains("Parameter")).ToList();
            var wParams02 = lstm00.Inputs.Where(p => p.Uid.Contains("Parameter")).ToList();

            //parameter count
            Assert.Equal(wParams00.Count, wParams01.Count);
            Assert.Equal(wParams00.Count, wParams02.Count);

            //structure of parameters test
            Assert.Equal(wParams00.Where(p => p.Name.Contains("_b")).Count(), wParams01.Where(p => p.Name.Contains("_b")).Count());
            Assert.Equal(wParams00.Where(p => p.Name.Contains("_w")).Count(), wParams01.Where(p => p.Name.Contains("_w")).Count());
            Assert.Equal(wParams00.Where(p => p.Name.Contains("_u")).Count(), wParams01.Where(p => p.Name.Contains("_u")).Count());
            Assert.Equal(wParams00.Where(p => p.Name.Contains("peep")).Count(), wParams01.Where(p => p.Name.Contains("peep")).Count());
            Assert.Equal(wParams00.Where(p => p.Name.Contains("stabilize")).Count(), wParams01.Where(p => p.Name.Contains("stabilize")).Count());


            //check structure of parameters with originaly developed lstm
            //chech for arguments
            Assert.True(lstm01.Arguments.Count == lstm02.Arguments.Count);
            for (int i = 0; i < lstm01.Arguments.Count; i++)
            {
                testVariable(lstm01.Arguments[i], lstm01.Arguments[i]);
            }

            ///
            Assert.True(lstm01.Inputs.Count == lstm02.Inputs.Count);
            for (int i = 0; i < lstm01.Inputs.Count; i++)
            {
                testVariable(lstm01.Inputs[i], lstm02.Inputs[i]);
            }

            ///
            Assert.True(lstm01.Outputs.Count == lstm02.Outputs.Count);
            for (int i = 0; i < lstm01.Outputs.Count; i++)
            {
                testVariable(lstm01.Outputs[i], lstm02.Outputs[i]);
            }
        }
Пример #3
0
        /// <summary>
        /// Implementation of custom NN model
        /// </summary>
        /// <param name="data"></param>
        /// <param name="yearVar"></param>
        /// <param name="montVar"></param>
        /// <param name="shopVar"></param>
        /// <param name="itemVar"></param>
        /// <param name="cnt3Var"></param>
        /// <param name="label"></param>
        /// <param name="device"></param>
        /// <returns></returns>
        private static Function PredictFutureSalesModel(List <Variable> variables, DeviceDescriptor device)
        {
            //define features and label vars
            Variable yearVar = variables[0];
            Variable montVar = variables[1];
            Variable shopVar = variables[2];
            Variable itemVar = variables[3];
            Variable cnt3Var = variables[4];
            Variable label   = variables[5];

            //create rnn object
            var ffNet = new FeedForwaredNN(device);

            //predefined parameters
            var H_DIMS    = 11;
            var CELL_DIMS = 3;
            var DROPRATRE = 0.2f;
            var outDim    = label.Shape.Dimensions.Last();

            //embedding layer and dimensionality reduction
            var yearEmb    = Embedding.Create(yearVar, yearVar.Shape.Dimensions[0] - 1, DataType.Float, device, 1, yearVar.Name + "_emb");
            var monthEmb   = Embedding.Create(montVar, montVar.Shape.Dimensions[0] / 2, DataType.Float, device, 1, montVar.Name + "_emb");
            var varshopEmb = Embedding.Create(shopVar, shopVar.Shape.Dimensions[0] / 2, DataType.Float, device, 1, shopVar.Name + "_emb");

            var itemEmb  = Embedding.Create(itemVar, itemVar.Shape.Dimensions[0] / 2, DataType.Float, device, 1, itemVar.Name + "_emb");
            var itemEmb2 = Embedding.Create(itemEmb, itemEmb.Output.Shape.Dimensions[0] / 4, DataType.Float, device, 1, itemEmb.Name + "_emb");

            //join all embedding layers with input variable of previous product sales
            var emb = CNTKLib.Splice(new VariableVector()
            {
                yearEmb, monthEmb, varshopEmb, itemEmb2, cnt3Var
            }, new Axis(0));

            //create recurrence for time series on top of joined layer
            var lstmLayer = RNN.RecurrenceLSTM(emb, H_DIMS, CELL_DIMS, DataType.Float, device, false, Activation.TanH, true, true);

            //create dense on top of LSTM recurrence layers
            var denseLayer = ffNet.Dense(lstmLayer, 33, Activation.TanH);

            //create dropout layer on top of dense layer
            var dropoutLay = CNTKLib.Dropout(denseLayer, DROPRATRE);

            //create dense layer without activation function
            var outLayer = ffNet.Dense(dropoutLay, outDim, Activation.None, label.Name);

            //
            return(outLayer);
        }
Пример #4
0
        /// <summary>
        /// Create cntk model function by providing parameters. The method is able for create:
        ///     - feedforward  with one hidden layer and any number of neurons
        ///     - deep neural network with any number of hidden layers and any number of neurons. Each hidden number has the same number of neurons
        ///     - LSTM NN with any number of hidden layers of LSTM , and any number of LSTM Cells in each layer. Also at the top of the network you can define
        ///             one dense layer and one dropout layer.
        /// </summary>
        /// <param name="nnParams"></param>
        /// <returns></returns>
        public static Function CreateNetwrok(List <NNLayer> layers, List <Variable> inputVars, Variable outpuVar, DeviceDescriptor device)
        {
            DataType type       = DataType.Float;
            Variable inputLayer = null;

            if (inputVars.Count > 1)
            {
                var vv = new VariableVector();
                foreach (var v in inputVars)
                {
                    //check if variable is stores as Sparse then we should create one embedding layer before slice
                    //since mixing sparse and dense data is not supported
                    if (v.IsSparse)
                    {
                        var v1 = Embedding.Create(v, v.Shape.Dimensions.Last(), type, device, 1, v.Name + "_sp_emb");
                        vv.Add(v1);
                    }
                    else
                    {
                        vv.Add(v);
                    }
                }


                //
                inputLayer = (Variable)CNTKLib.Splice(vv, new Axis(0));
            }
            else //define input layer
            {
                inputLayer = inputVars.First();
            }


            //Create network
            var net = inputLayer;
            var ff  = new FeedForwaredNN(device, type);

            //set last layer name to label name
            layers.Last().Name = outpuVar.Name;

            //get last LSTM layer
            var lastLSTM = layers.Where(x => x.Type == LayerType.LSTM).LastOrDefault();

            //
            foreach (var layer in layers)
            {
                if (layer.Type == LayerType.Dense)
                {
                    net = ff.Dense(net, layer.HDimension, layer.Activation, layer.Name);
                }
                else if (layer.Type == LayerType.Drop)
                {
                    net = CNTKLib.Dropout(net, layer.Value / 100.0f);
                }
                else if (layer.Type == LayerType.Embedding)
                {
                    net = Embedding.Create(net, layer.HDimension, type, device, 1, layer.Name);
                }
                else if (layer.Type == LayerType.LSTM)
                {
                    var returnSequence = true;
                    if (layers.IndexOf(lastLSTM) == layers.IndexOf(layer))
                    {
                        returnSequence = false;
                    }
                    net = RNN.RecurrenceLSTM(net, layer.HDimension, layer.CDimension, type, device, returnSequence, layer.Activation,
                                             layer.Peephole, layer.SelfStabilization, 1);
                }
            }

            //check if last layer is compatible with the output
            if (net.Shape.Dimensions.Last() != outpuVar.Shape.Dimensions.Last())
            {
                ff.CreateOutputLayer(net, outpuVar, Activation.None);
            }

            return(net);
        }
Пример #5
0
        public void LSTM_Test_Params_Count_with_peep_selfstabilize()
        {
            //define values, and variables
            Variable x = Variable.InputVariable(new int[] { 2 }, DataType.Float, "input");
            Variable y = Variable.InputVariable(new int[] { 3 }, DataType.Float, "output");

            #region lstm org implemented in cntk for reference
            //lstme implementation refe 02
            var lstmTest02 = LSTMSequenceClassifier.LSTMNet(x, 3, device, "output1");
            var ft2        = lstmTest02.Inputs.Where(l => l.Uid.StartsWith("Parameter")).ToList();
            var totalSize  = ft2.Sum(p => p.Shape.TotalSize);
            //bias params
            var bs2      = ft2.Where(p => p.Name.Contains("_b")).ToList();
            var totalBs2 = bs2.Sum(v => v.Shape.TotalSize);

            //weights
            var ws2      = ft2.Where(p => p.Name.Contains("_w")).ToList();
            var totalWs2 = ws2.Sum(v => v.Shape.TotalSize);

            //update
            var us2      = ft2.Where(p => p.Name.Contains("_u")).ToList();
            var totalUs2 = us2.Sum(v => v.Shape.TotalSize);

            //peephole
            var ph2      = ft2.Where(p => p.Name.Contains("_peep")).ToList();
            var totalph2 = ph2.Sum(v => v.Shape.TotalSize);

            //stabilize
            var st2      = ft2.Where(p => p.Name.Contains("_stabilize")).ToList();
            var totalst2 = st2.Sum(v => v.Shape.TotalSize);
            #endregion

            #region anndotnet old implementation
            //
            //LSTMReccurentNN lstmNN = new LSTMReccurentNN(3, 3, device);
            ////lstm implme reference 01
            //var lstmCell11 = lstmNN.CreateLSTM(x, "output1");
            //var lstmTest01 = CNTKLib.SequenceLast(lstmCell11.h);
            //var ft1 = lstmTest01.Inputs.Where(l => l.Uid.StartsWith("Parameter")).ToList();
            //var consts1 = lstmTest01.Inputs.Where(l => l.Uid.StartsWith("Constant")).ToList();
            //var inp1 = lstmTest01.Inputs.Where(l => l.Uid.StartsWith("Input")).ToList();
            //var pparams1 = ft1.Sum(v => v.Shape.TotalSize);
            #endregion

            //Number of LSTM parameters
            var lstm1 = RNN.RecurrenceLSTM(x, 3, 3, DataType.Float, device, false, Activation.TanH, true, true, 1);

            var ft     = lstm1.Inputs.Where(l => l.Uid.StartsWith("Parameter")).ToList();
            var consts = lstm1.Inputs.Where(l => l.Uid.StartsWith("Constant")).ToList();
            var inp    = lstm1.Inputs.Where(l => l.Uid.StartsWith("Input")).ToList();

            //bias params
            var bs      = ft.Where(p => p.Name.Contains("_b")).ToList();
            var totalBs = bs.Sum(v => v.Shape.TotalSize);
            Assert.Equal(12, totalBs);
            //weights
            var ws      = ft.Where(p => p.Name.Contains("_w")).ToList();
            var totalWs = ws.Sum(v => v.Shape.TotalSize);
            Assert.Equal(24, totalWs);
            //update
            var us      = ft.Where(p => p.Name.Contains("_u")).ToList();
            var totalUs = us.Sum(v => v.Shape.TotalSize);
            Assert.Equal(36, totalUs);
            //peephole
            var ph      = ft.Where(p => p.Name.Contains("_peep")).ToList();
            var totalPh = ph.Sum(v => v.Shape.TotalSize);
            Assert.Equal(9, totalPh);
            //stabilize
            var st      = ft.Where(p => p.Name.Contains("_stabilize")).ToList();
            var totalst = st.Sum(v => v.Shape.TotalSize);
            Assert.Equal(6, totalst);

            var totalOnly          = totalBs + totalWs + totalUs;
            var totalWithSTabilize = totalOnly + totalst;
            var totalWithPeep      = totalOnly + totalPh;

            var totalP      = totalOnly + totalst + totalPh;
            var totalParams = ft.Sum(v => v.Shape.TotalSize);
            Assert.Equal(totalP, totalParams);
            //72- without peep and stab
            //75 - witout peep with stabil +3xm =
            //81 - with peephole and without stabil
            //87 - with peep ans stab 3+9
        }