public NeuralNetwork(MathOperationManager mathManager, string modelPath)
        {
            NeuralNetworkStore nnStore = LoadModel(modelPath);

            this.CreateNeuralNetwork(mathManager, nnStore.Configuration);
            this.LoadFromNeuralNetworkStore(nnStore);
        }
 public NeuralLayer(MathOperationManager mathManager, float[] biasData, int neuronCount, int batchSize, bool isInputLayer = false, bool isOutputLayer = false)
 {
     this.IsInputLayer  = isInputLayer;
     this.IsOutputLayer = isOutputLayer;
     this.Data          = mathManager.CreateMatrix(neuronCount, batchSize);
     this.Bias          = mathManager.CreateMatrix(biasData, neuronCount);
     this.ErrorGradient = mathManager.CreateMatrix(neuronCount, batchSize);
 }
 public NeuralLayer(MathOperationManager mathManager, int neuronCount, int batchSize, bool isInputLayer = false, bool isOutputLayer = false)
 {
     this.IsInputLayer  = isInputLayer;
     this.IsOutputLayer = isOutputLayer;
     this.Data          = mathManager.CreateMatrix(neuronCount, batchSize);
     this.Bias          = mathManager.CreateMatrix(ArrayUtilities.GetArrayWithRandomValues(neuronCount), neuronCount);
     this.ErrorGradient = mathManager.CreateMatrix(neuronCount, batchSize);
 }
示例#4
0
 static Scalar()
 {
     MathOperationManager.RegisterMany(new MathOperation[] {
         new Addition <Scalar, Scalar, Scalar>(addScalars),
         new Negation <Scalar, Scalar>(negateScalar),
         new Multiplication <Scalar, Scalar, Scalar>(multiplyScalars),
         new Exponentiation <Scalar, double, Scalar>(exponentiateScalar)
     });
 }
 protected void CreateNeuralNetwork(MathOperationManager mathManager, NeuralNetworkConfiguration configuration)
 {
     this.mathManager             = mathManager;
     this.configuration           = configuration;
     this.currentEpoch            = 0;
     this.currentBatchSize        = NeuralNetworkConfiguration.defaultBatchSize;
     this.neuralLayers            = new List <NeuralLayer>();
     this.biasGradientAccumulator = this.mathManager.CreateMatrix(Enumerable.Repeat <float>(1, this.currentBatchSize).ToArray(), this.currentBatchSize);
     this.CreateNetworkArchitecture();
 }
示例#6
0
 static Quantity()
 {
     MathOperationManager.RegisterMany(new MathOperation[] {
         new Multiplication <BaseQuantity, BaseQuantity, DerivedQuantity>(multiplyBaseQuantities),
         new Multiplication <DerivedQuantity, DerivedQuantity, DerivedQuantity>(multiplyDerivedQuantities),
         new Multiplication <BaseQuantity, DerivedQuantity, DerivedQuantity>(multiplyBaseByDerivedQuantities),
         new Multiplication <DerivedQuantity, BaseQuantity, DerivedQuantity>(multiplyDerivedByBaseQuantities),
         new Exponentiation <BaseQuantity, double, DerivedQuantity>(exponentiateBaseQuantity),
         new Exponentiation <DerivedQuantity, double, DerivedQuantity>(exponentiateDerivedQuantity)
     });
 }
示例#7
0
 static Unit()
 {
     MathOperationManager.RegisterMany(new MathOperation[] {
         new Multiplication <BaseUnit, BaseUnit, DerivedUnit>(multiplyBaseUnits),
         new Multiplication <DerivedUnit, DerivedUnit, DerivedUnit>(multiplyDerivedUnits),
         new Multiplication <BaseUnit, DerivedUnit, DerivedUnit>(multiplyBaseUnitByDerivedUnits),
         new Multiplication <DerivedUnit, BaseUnit, DerivedUnit>(multiplyDerivedUnitByBaseUnits),
         new Multiplication <BaseUnit, MetricPrefix, DerivedUnit>(multiplyBaseUnitByPrefix),
         new Multiplication <MetricPrefix, BaseUnit, DerivedUnit>(multiplyPrefixByBaseUnit),
         new Exponentiation <BaseUnit, double, DerivedUnit>(exponentiateBaseUnit),
         new Exponentiation <DerivedUnit, double, DerivedUnit>(exponentiateDerivedUnit)
     });
 }
示例#8
0
 public NeuralLayer(MathOperationManager mathManager, float[] biasData, int neuronCount, int batchSize, NeuronActivationType activation, bool isInputLayer = false, bool isOutputLayer = false)
 {
     this.mathManager   = mathManager;
     this.IsInputLayer  = isInputLayer;
     this.IsOutputLayer = isOutputLayer;
     this.NeuralLink    = null;
     this.LayerIn       = null;
     this.LayerOut      = null;
     this.Activation    = activation;
     this.Data          = mathManager.CreateMatrix(neuronCount, batchSize);
     this.Bias          = mathManager.CreateMatrix(biasData, neuronCount);
     this.ErrorGradient = mathManager.CreateMatrix(neuronCount, batchSize);
 }
        public static void BellmanLossAndDerivative(MathOperationManager mathManager, Matrix predictedQValues, Matrix QHatValues, Matrix chosenActionIndices, Matrix currentRewards,
                                                    ref float errorAvg, Matrix errorDerivative, float discountFactor, Matrix isLastEpisode)
        {
            using (Matrix maxQHatValues = mathManager.CreateMatrix(predictedQValues.Column), errorMatrix = mathManager.CreateMatrix(predictedQValues.Column))
            {
                // Calculate column-wise max of the QHat values
                mathManager.GlobalInstance.ColumnWiseMax(QHatValues, maxQHatValues);

                // Calculate error & derivative
                mathManager.GlobalInstance.MatrixBellmanErrorAndDerivative(predictedQValues, maxQHatValues, chosenActionIndices, currentRewards, errorMatrix, errorDerivative,
                                                                           discountFactor, isLastEpisode);
                errorAvg = errorMatrix.GetValue().Sum() / predictedQValues.Column;
            }
        }
 public static void CrossEntropyErrorAndDerivative(MathOperationManager mathManager, Matrix preSigmoidScores, Matrix trueLabels, Matrix outputErrorDerivative,
                                                   ref float errorAvg)
 {
     using (Matrix sigmoidScores = mathManager.CreateMatrix(preSigmoidScores.Row, preSigmoidScores.Column), outputError = mathManager.CreateMatrix(preSigmoidScores.Row, preSigmoidScores.Column))
     {
         // Calculate sigmoid
         mathManager.GlobalInstance.MatrixSigmoid(preSigmoidScores, sigmoidScores);
         // Get cross entropy error
         mathManager.GlobalInstance.MatrixCrossEntropyError(sigmoidScores, trueLabels, outputError);
         errorAvg = outputError.GetValue().Sum() / preSigmoidScores.Column;
         // calculate error derivative = sigmoidScores - trueLabels + (regularizationParameter * sigmoidScores)
         mathManager.GlobalInstance.MatrixAddition(sigmoidScores, MatrixTransformation.None, 1, trueLabels, MatrixTransformation.None, -1, outputErrorDerivative);
     }
 }
示例#11
0
        static Quantity()
        {
            //Multiplication Operators
            Func <DerivedQuantity, DerivedQuantity, DerivedQuantity> multiplyDerivedQuantities = (a, b) =>
            {
                var expression = new DerivedQuantityDimension(a.Dimension.Members
                                                              .Concat(b.Dimension.Members).ToArray());
                return(new DerivedQuantity(expression));
            };

            Func <BaseQuantity, BaseQuantity, DerivedQuantity> multiplyBaseQuantities = (a, b) =>
            {
                var derivedA = new DerivedQuantity(a, 1);
                var derivedB = new DerivedQuantity(b, 1);
                return(multiplyDerivedQuantities(derivedA, derivedB));
            };

            Func <BaseQuantity, DerivedQuantity, DerivedQuantity> multiplyBaseByDerivedQuantities = (a, b) =>
            {
                var derivedA = new DerivedQuantity(a, 1);
                return(multiplyDerivedQuantities(derivedA, b));
            };

            Func <DerivedQuantity, BaseQuantity, DerivedQuantity> multiplyDerivedByBaseQuantities = (a, b) =>
            {
                var derivedB = new DerivedQuantity(b, 1);
                return(multiplyDerivedQuantities(a, derivedB));
            };

            //Exponentiation Operators
            Func <BaseQuantity, double, DerivedQuantity> exponentiateBaseQuantity = (a, b) => new DerivedQuantity(a, b);

            Func <DerivedQuantity, double, DerivedQuantity> exponentiateDerivedQuantity = (a, b) =>
            {
                var members = a.Dimension.Members.Select(m => new DerivedQuantityDimensionMember(m.BaseQuantity,
                                                                                                 m.Exponent * b)).ToArray();
                var expression = new DerivedQuantityDimension(members);
                return(new DerivedQuantity(expression));
            };

            MathOperationManager.RegisterMany(new MathOperation[] {
                new Multiplication <BaseQuantity, BaseQuantity, DerivedQuantity>(multiplyBaseQuantities),
                new Multiplication <DerivedQuantity, DerivedQuantity, DerivedQuantity>(multiplyDerivedQuantities),
                new Multiplication <BaseQuantity, DerivedQuantity, DerivedQuantity>(multiplyBaseByDerivedQuantities),
                new Multiplication <DerivedQuantity, BaseQuantity, DerivedQuantity>(multiplyDerivedByBaseQuantities),
                new Exponentiation <BaseQuantity, double, DerivedQuantity>(exponentiateBaseQuantity),
                new Exponentiation <DerivedQuantity, double, DerivedQuantity>(exponentiateDerivedQuantity)
            });
        }
示例#12
0
        public DQN(MathOperationManager mathManager, DQNNeuralNetworkConfiguration configuration)
        {
            if (configuration.LossFunction != LossFunctionType.BellmanError)
            {
                throw new ArgumentException("DQN only supports Bellman error. Pls check the configuration passed in.");
            }

            base.CreateNeuralNetwork(mathManager, configuration);

            this.DQNConfiguration  = configuration;
            this.gradientStepCount = 0;

            var nnStore = this.CreateNeuralNetworkStore();

            this.QHat = new NeuralNetwork(this.mathManager, nnStore);
        }
        private static void DNN(IEnumerable <BatchInputWrapper> trainData, IEnumerable <BatchInputWrapper> cvData)
        {
            using (MathOperationManager mathManager = new MathOperationManager(MathType.GPU))
            {
                var hiddenLayers = new List <int>();
                hiddenLayers.Add(100);
                hiddenLayers.Add(100);
                NeuralNetworkConfiguration config = new NeuralNetworkConfiguration(784, hiddenLayers, 10);
                config.Epochs   = 100;
                config.StepSize = (float)1.5;
                //config.Activation = NeuronActivationType.ReLu;

                using (NeuralNetwork dnn = new NeuralNetwork(mathManager, config))
                {
                    dnn.MiniBatchStochasticGradientDescent(trainData, cvData);
                }
            }
        }
        private static void DQN(IEnumerable <BatchInputWrapper> trainData, IEnumerable <BatchInputWrapper> cvData)
        {
            using (MathOperationManager mathManager = new MathOperationManager(MathType.GPU))
            {
                var hiddenLayers = new List <int>();
                hiddenLayers.Add(10);
                hiddenLayers.Add(5);
                DQNNeuralNetworkConfiguration config = new DQNNeuralNetworkConfiguration(5, hiddenLayers, 12);
                config.LossFunction = LossFunctionType.BellmanError;
                config.Epochs       = 20;
                config.StepSize     = (float)0.1;

                using (DQN nn = new DQN(mathManager, config))
                {
                    nn.MiniBatchStochasticGradientDescent(trainData, cvData);
                }
            }
        }
        public static void DqnStanfordEvaluation(MathOperationManager mathManager, Matrix predictedQValues, Matrix chosenActionIndices, Matrix currentRewards,
                                                 ref float matchPredictRewardSum, ref int matchPredictRewardCount, ref float nonMatchPredictRewardSum, ref int nonMatchPredictRewardCount)
        {
            float emptyIndicatorValue = float.Epsilon;

            using (Matrix predictedActionIndices = mathManager.CreateMatrix(predictedQValues.Column),
                   matchPredictrewardMatrix = mathManager.CreateMatrix(Enumerable.Repeat <float>(emptyIndicatorValue, predictedQValues.Column).ToArray(), predictedQValues.Column),
                   nonMatchPredictrewardMatrix = mathManager.CreateMatrix(Enumerable.Repeat <float>(emptyIndicatorValue, predictedQValues.Column).ToArray(), predictedQValues.Column))
            {
                mathManager.GlobalInstance.ColumnWiseMaxIndex(predictedQValues, predictedActionIndices);
                mathManager.GlobalInstance.DqnStanfordEvaluation(predictedActionIndices, chosenActionIndices, currentRewards, matchPredictrewardMatrix, nonMatchPredictrewardMatrix);

                var matchPredictRewards = matchPredictrewardMatrix.GetValue();
                matchPredictRewardCount = matchPredictRewards.Count(r => r != emptyIndicatorValue);
                matchPredictRewardSum   = matchPredictRewards.Sum();

                var nonMatchPredictRewards = nonMatchPredictrewardMatrix.GetValue();
                nonMatchPredictRewardCount = nonMatchPredictRewards.Count(r => r != emptyIndicatorValue);
                nonMatchPredictRewardSum   = nonMatchPredictRewards.Sum();
            }
        }
 public NeuralNetwork(MathOperationManager mathManager, NeuralNetworkStore nnStore)
 {
     this.CreateNeuralNetwork(mathManager, nnStore.Configuration);
     this.LoadFromNeuralNetworkStore(nnStore);
 }
 public NeuralLink(MathOperationManager mathManager, float[] matrixData, NeuralLayer layerIn, NeuralLayer layerOut)
 {
     this.Weights       = mathManager.CreateMatrix(matrixData, layerOut.Data.Row, layerIn.Data.Row);
     this.ErrorGradient = mathManager.CreateMatrix(layerOut.Data.Row, layerIn.Data.Row);
 }
 public NeuralNetwork(MathOperationManager mathManager, NeuralNetworkConfiguration configuration)
 {
     this.CreateNeuralNetwork(mathManager, configuration);
 }
示例#19
0
 public static object Negate(object a) =>
 MathOperationManager.Get(MathOperationType.Negation, a.GetType())?.Execute(a);
示例#20
0
 public static object Power(object a, object b) =>
 MathOperationManager.Get(MathOperationType.Exponentiation, a.GetType(), b.GetType())?.Execute(a, b);
示例#21
0
 public static object Multiply(object a, object b) =>
 MathOperationManager.Get(MathOperationType.Multiplication, a.GetType(), b.GetType())?.Execute(a, b);
示例#22
0
 public static object Add(object a, object b) =>
 MathOperationManager.Get(MathOperationType.Addition, a.GetType(), b.GetType())?.Execute(a, b);
 public NeuralLink(MathOperationManager mathManager, NeuralLayer layerIn, NeuralLayer layerOut)
 {
     this.Weights       = mathManager.CreateMatrix(ArrayUtilities.GetArrayWithRandomValues(layerOut.Data.Row * layerIn.Data.Row), layerOut.Data.Row, layerIn.Data.Row);
     this.ErrorGradient = mathManager.CreateMatrix(layerOut.Data.Row, layerIn.Data.Row);
 }