Esempio n. 1
0
 public LearnerVector(LearnerVector other) : this(CNTKLibPINVOKE.new_LearnerVector__SWIG_1(LearnerVector.getCPtr(other)), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Esempio n. 2
0
 public LearnerVectorEnumerator(LearnerVector collection)
 {
     collectionRef = collection;
     currentIndex  = -1;
     currentObject = null;
     currentSize   = collectionRef.Count;
 }
Esempio n. 3
0
 public void SetRange(int index, LearnerVector values)
 {
     CNTKLibPINVOKE.LearnerVector_SetRange(swigCPtr, index, LearnerVector.getCPtr(values));
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Esempio n. 4
0
        public static LearnerVector Repeat(Learner value, int count)
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.LearnerVector_Repeat(Learner.getCPtr(value), count);
            LearnerVector         ret  = (cPtr == global::System.IntPtr.Zero) ? null : new LearnerVector(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Esempio n. 5
0
        public LearnerVector GetRange(int index, int count)
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.LearnerVector_GetRange(swigCPtr, index, count);
            LearnerVector         ret  = (cPtr == global::System.IntPtr.Zero) ? null : new LearnerVector(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Esempio n. 6
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(LearnerVector obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Esempio n. 7
0
        static void Main(string[] args)
        {
            const bool useSparse = false;

            const int   vectorSize          = 300;
            const int   vocabularySize      = 6000;
            const float xMax                = 100f;
            const float alphaOrder          = 0.75f;
            const int   imagimableBatchSize = 1;

            var device = DeviceDescriptor.GPUDevice(0);

            var scalarDimension = new[] { imagimableBatchSize, 1 };
            var matrixSize      = new[] { vectorSize, vocabularySize };
            var vectorDimension = new[] { vocabularySize, 1 };

            var iterationScalarShape = NDShape.CreateNDShape(scalarDimension);
            var iterationMatrixShape = NDShape.CreateNDShape(matrixSize);
            var iterationVectorShape = NDShape.CreateNDShape(vectorDimension);

            var coOccurrences = Variable.InputVariable(iterationScalarShape, DataType.Float, "coOccurrences - " + vocabularySize, null, false);
            var columns       = Variable.InputVariable(iterationScalarShape, DataType.Float, "columns - " + vocabularySize, null, false);
            var rows          = Variable.InputVariable(iterationScalarShape, DataType.Float, "rows - " + vocabularySize, null, false);

            var mainVectors = new Parameter(iterationMatrixShape, DataType.Float, 0d, device);

            PrintDim(mainVectors, nameof(mainVectors));
            var contextVectors = new Parameter(iterationMatrixShape, DataType.Float, 0d, device);

            PrintDim(contextVectors, nameof(contextVectors));
            var mainBiases = new Parameter(iterationVectorShape, DataType.Float, 0d, device);

            PrintDim(mainBiases, nameof(mainBiases));
            var contextBiases = new Parameter(iterationVectorShape, DataType.Float, 0d, device);

            PrintDim(contextBiases, nameof(contextBiases));
            var one = new Constant(iterationScalarShape, DataType.Float, 1d, device);

            PrintDim(one, nameof(one));
            var xmax = new Constant(iterationScalarShape, DataType.Float, xMax, device);

            PrintDim(xmax, nameof(xmax));
            var alpha = new Constant(iterationScalarShape, DataType.Float, alphaOrder, device);

            PrintDim(alpha, nameof(alpha));


            var divide = CNTKLib.ElementDivide(coOccurrences, xmax);

            PrintDim(divide, nameof(divide));
            var pow = CNTKLib.Pow(divide, alpha);

            PrintDim(pow, nameof(pow));
            var weight = CNTKLib.ElementMin(one, pow, "min");

            PrintDim(weight, nameof(weight));

            var oneHotRow = CNTKLib.OneHotOp(rows, vocabularySize, useSparse, new Axis(0));

            PrintDim(oneHotRow, nameof(oneHotRow));
            var oneHotColumn = CNTKLib.OneHotOp(columns, vocabularySize, useSparse, new Axis(0));

            PrintDim(oneHotColumn, nameof(oneHotColumn));

            var mainVector = CNTKLib.Alias(CNTKLib.Times(mainVectors, oneHotColumn));

            PrintDim(mainVector, nameof(mainVector));
            var contextVector = CNTKLib.Alias(CNTKLib.Times(contextVectors, oneHotRow));

            PrintDim(contextVector, nameof(contextVector));

            var mainBias = CNTKLib.Alias(CNTKLib.TransposeTimes(mainBiases, oneHotColumn));

            PrintDim(mainBias, nameof(mainBias));

            var contextBias = CNTKLib.Alias(CNTKLib.TransposeTimes(contextBiases, oneHotRow));

            PrintDim(contextBias, nameof(contextBias));

            var model = CNTKLib.ElementTimes(mainVector, contextVector);

            PrintDim(model, "CNTKLib.ElementTimes(mainVector, contextVector)");

            model = CNTKLib.ReduceSum(model, new Axis(0));
            PrintDim(model, "CNTKLib.ReduceSum(model, new Axis(0))");

            model = CNTKLib.Plus(model, mainBias);
            PrintDim(model, "CNTKLib.Plus(model, mainBias)");

            model = CNTKLib.Plus(model, contextBias);
            PrintDim(model, "CNTKLib.Plus(model, contextBias)");

            model = CNTKLib.Minus(model, CNTKLib.Log(coOccurrences));
            PrintDim(model, "CNTKLib.Minus(model, CNTKLib.Log(coOccurrences))");

            model = CNTKLib.Square(model);
            PrintDim(model, "CNTKLib.Square(model)");

            model = CNTKLib.ElementTimes(model, weight);
            PrintDim(model, "CNTKLib.ElementTimes(model, weight)");

            model = CNTKLib.ReduceSum(model, new Axis(1));
            PrintDim(model, "CNTKLib.ReduceSum(model, new Axis(1))");

            var thisBatchShape = NDShape.CreateNDShape(new[] { imagimableBatchSize });

            var parameterVector = new ParameterVector(model.Parameters().ToList());

            var learner = CNTKLib.SGDLearner(
                parameterVector,
                new TrainingParameterScheduleDouble(0.1, (uint)(vocabularySize * vocabularySize)));

            var learners = new LearnerVector()
            {
                learner
            };
            var trainer = CNTKLib.CreateTrainer(model, model, model, learners);


            var count    = (int)(vocabularySize * vocabularySize * 0.2d * 0.2d);
            var floats   = GetRandomFloats(count).ToArray();
            var fColumns = GetRandomInts(count, 0, vocabularySize).ToArray();
            var fRows    = GetRandomInts(count, 0, vocabularySize).ToArray();

            const int batchSize = 10000;
            var       all       = floats.Zip(fColumns, (f, c) => (f: f, c: c)).Zip(fRows, (tuple, r) => (tuple.c, tuple.c, r))
                                  .ToObservable()
                                  .Buffer(batchSize)
                                  .Select(x => (f: x.Select(y => y.Item1).ToArray(), c: x.Select(y => y.Item2).ToArray(), r: x.Select(y => y.Item3).ToArray()))
                                  .ToArray()
                                  .Wait();

            Console.WriteLine($"count: {count}");

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            for (var e = 0; e < 3; e++)
            {
                for (var i = 0; i < all.Length; i++)
                {
                    var valueTuple = all[i];

                    var cooccurenceValue = Value.CreateBatch(thisBatchShape, valueTuple.f, device);
                    var columnsValue     = Value.CreateBatch(thisBatchShape, valueTuple.c, device);
                    var rowsValue        = Value.CreateBatch(thisBatchShape, valueTuple.r, device);

                    var trainDictionary = new Dictionary <Variable, Value>
                    {
                        { coOccurrences, cooccurenceValue },
                        { columns, columnsValue },
                        { rows, rowsValue }
                    };


                    trainer.TrainMinibatch(trainDictionary, false, device);

                    if (i % 100 == 0)
                    {
                        Console.WriteLine($"e: {e}\ti: {stopwatch.Elapsed:g}");
                    }
                }
            }

            stopwatch.Stop();


            Console.WriteLine($"success: {stopwatch.Elapsed:g}");
        }