microsoft / CNTK

Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
https://docs.microsoft.com/cognitive-toolkit/
Other
17.5k stars 4.29k forks source link

C# training AccessViolationException #3149

Open chismar opened 6 years ago

chismar commented 6 years ago

While function evaluation works fine (not throwing exceptions), this code instead throws one, when I call tran minibatch. I'm new to CNTK, and could've just done something wrong, or this is a bug.

``

        var device = DeviceDescriptor.CPUDevice;
        var shape = new NDShape(1, boardSize * boardSize);
        boardVariable = Variable.InputVariable(shape, DataType.Float, "State");
        rewardBoard = Variable.InputVariable(shape, DataType.Float, "ActionWithReward");
        var func = TestHelper.Dense(boardVariable, boardSize * boardSize, device, Activation.ReLU);
        var func2 = TestHelper.Dense(func.Output, boardSize * boardSize, device, Activation.ReLU);
        evalFunction = func2;
        var loss = CNTKLib.ReduceMean(CNTKLib.Square(CNTKLib.Minus(boardVariable, rewardBoard)), new Axis(0));
        var meas = CNTKLib.ReduceMean(CNTKLib.Square(CNTKLib.Minus(boardVariable, rewardBoard)), new Axis(0));
        var learningRatePerSample = new CNTK.TrainingParameterScheduleDouble(0.02, 1);

        var parameterLearners =
            new List<Learner>() { Learner.SGDLearner(func2.Parameters(), learningRatePerSample) };
        var trainer = Trainer.CreateTrainer(func2, loss, meas, parameterLearners);
        int numMinibatchesToTrain = 1000;
        int memorySampleSize = 50;

        // train the model
        for (int minibatchCount = 0; minibatchCount < numMinibatchesToTrain; minibatchCount++)
        {
            //stripped stuff that's specific to my hello-world tic-tac-toe
            using (var state = Value.CreateBatch<float>(shape, new float[boardSize * boardSize], device, true))
            using (var actionsWithReward = Value.CreateBatch<float>(shape, new float[boardSize * boardSize], device, true))
                trainer.TrainMinibatch(new Dictionary<Variable, Value>() {
                { boardVariable, state},
                { rewardBoard, actionsWithReward } }, false, device);

        }

``

ke1337 commented 6 years ago

Which version of CNTK are you running on? What's the callstack?

chismar commented 6 years ago

2.5 So, I've resolved the issue. I've accidently been using CNTKLib.Minus with two input arguments, instead of prediction output and an input.

Though I would prefer an actual proper error message, in the mean time changing var loss = CNTKLib.ReduceMean(CNTKLib.Square(CNTKLib.Minus(**boardVariable**, rewardBoard)), new Axis(0)); to var loss = CNTKLib.ReduceMean(CNTKLib.Square(CNTKLib.Minus(**func2**, rewardBoard)), new Axis(0)); Solved the issue. (Although, my network learns how to loose and not win for some reason, but that's up to me to fix, lol)