Exemplo n.º 1
0
        public void Test()
        {
            //var test = Enumerable.Range(0, 10 * 3 * 4 * 5).Select(s => (float)s).ToArray();
            //SingleNArray testsingle = new SingleNArray(new mxnet.numerics.nbase.Shape(10, 3, 4, 5), test);

            //var test5_result = testsingle["::-1", "1:13"]["5:1:-1", "::-1", ":10"];
            //var t1 = test5_result.Flat().data;

            //var t2 = testsingle.Argmax(0);

            Random rnd = new Random(0);


            var test = Enumerable.Range(0, 10 * 3 * 4 * 5).Select(s => (float)rnd.Next(0, 50)).ToArray();


            string       testshape    = "(10,3,4,5)";
            SingleNArray testsingle   = new SingleNArray(new mxnet.numerics.nbase.Shape(10, 3, 4, 5), test);
            var          test1_result = testsingle[new int[] { 1, 3, 5, 7 }, new int[] { 0, 2, 0, 2 }];


            var a = new SingleNArray(new Shape(2, 2), new float[] { 1, 2, 3, 4 });
            var b = new SingleNArray(new Shape(1, 2), new float[] { 5, 6 });

            var test2_result = SingleNArray.Concatenate(0, a, b);
        }
Exemplo n.º 2
0
        public void TestArithmetic()
        {
            Random rnd = new Random(0);


            var          test       = Enumerable.Range(0, 10 * 3 * 4 * 5).Select(s => (float)rnd.Next(0, 50)).ToArray();
            string       testshape  = "(10,3,4,5)";
            SingleNArray testsingle = new SingleNArray(new Shape(10, 3, 4, 5), test);


            var test1NpResult = eval_scalar <float>("arr[1::1,1:3].sum()", new Parameter <float>("arr", testshape, test));
            var test1Result   = testsingle["1::1", "1:3"].Sum();

            Assert.AreEqual(test1NpResult, test1Result, "1");



            var test2NpResult = eval_array <float>("arr.argmax(axis=0)", new Parameter <float>("arr", testshape, test));
            var test2Result   = testsingle.Argmax(0);

            CollectionAssert.AreEqual(test2NpResult, test2Result.Flat().Data, "1");

            var a             = new float[] { 1, 2, 3, 4 };
            var b             = new float[] { 5, 6 };
            var aarry         = new SingleNArray(new Shape(2, 2), a);
            var barry         = new SingleNArray(new Shape(1, 2), b);
            var test3NpResult = eval_array <float>("np.concatenate((a,b),0)",
                                                   new Parameter <float>("a", "(2,2)", a),
                                                   new Parameter <float>("b", "(1,2)", b));
            var test3Result = SingleNArray.Concatenate(0, aarry, barry);

            CollectionAssert.AreEqual(test3NpResult, test3Result.Flat().Data, "2");

            //var test4_np_result = eval_array<float>("np.concatenate((a,b),axis = 1)",
            //new Parameter<float>("a", "(2,2)", a),
            //new Parameter<float>("b", "(1,2)", b));
            //var test4_result = SingleNArray.Concatenate(1, aarry, barry);
            //CollectionAssert.AreEqual(test4_np_result, test4_result.Flat().data, "2");
        }
        public List <SingleNArray> Predict(IDataIter inputX, int?numBatch = null, bool returnData = false, bool reset = true)
        {
            if (reset)
            {
                inputX.Reset();
            }

            var dataShapes = inputX.ProvideData;
            var dataNames  = dataShapes.Select(s => s.Key).ToList();

            InitPredictor(dataShapes);

            var batchSize  = inputX.BatchSize;
            var dataArrays = dataNames.Select(name => this._predExec.ArgDict[name]).ToList();
            var outputList = this._predExec.Outputs.Select(s => new List <SingleNArray>()).ToList();

            List <List <SingleNArray> > dataList  = null;
            List <List <SingleNArray> > labelList = null;

            if (returnData)
            {
                dataList  = inputX.ProvideData.Select(s => new List <SingleNArray>()).ToList();
                labelList = inputX.ProvideLabel.Select(s => new List <SingleNArray>()).ToList();
            }

            int i = 0;

            foreach (var batch in inputX)
            {
                ExecutorManager.LoadData(batch, dataArrays);
                this._predExec.Forward(isTrain: false);
                var padded   = batch.Pad;
                var realSize = batchSize - padded;

                foreach (var vitem in outputList.Zip(this._predExec.Outputs, Tuple.Create))
                {
                    vitem.Item1.Add(vitem.Item2.Slice(0, (uint)realSize).AsNumerics());
                }

                if (returnData)
                {
                    for (int j = 0; j < batch.Data.Count; j++)
                    {
                        var x = batch.Data[j];
                        dataList[j].Add(x.Slice(0, (uint)realSize).AsNumerics());
                    }

                    for (int j = 0; j < batch.Data.Count; j++)
                    {
                        var x = batch.Label[j];
                        labelList[j].Add(x.Slice(0, (uint)realSize).AsNumerics());
                    }
                }

                i += 1;
                if (numBatch != null && i == numBatch.Value)
                {
                    break;
                }
            }


            var outputs = outputList.Select(s => SingleNArray.Concatenate(0, s.ToArray())).ToList();

            if (returnData)
            {
                var data  = dataList.Select(s => SingleNArray.Concatenate(0, s.ToArray()));
                var label = labelList.Select(s => SingleNArray.Concatenate(0, s.ToArray()));
            }


            return(outputs);
        }