コード例 #1
0
ファイル: Neuron.cs プロジェクト: mroohian/ai-car-control
        public Neuron(NeuronParam neuronParam)
        {
            this._metadata = new ExpandoObject();

            this.Bias = neuronParam.Bias;

            this.Dendrites = new Dendrite[neuronParam.DendriteCount];

            for (int d = 0; d < neuronParam.DendriteCount; d++)
            {
                this.Dendrites[d] = new Dendrite(neuronParam.Weights[d]);
            }
        }
コード例 #2
0
        public NeuralNetwork(int[] layers, double[] weights = null)
        {
            // set to true if needs to load data
            var hasWeight = weights != null;
            var readIndex = 0;

            // At least we need input and output layers
            if (layers.Length < 2)
            {
                throw new ArgumentException(nameof(layers));
            }

            // if we need to load weights check the number of them are correct.
            if (hasWeight)
            {
                if (weights.Length != CalculateWeightsCount(layers))
                {
                    throw new ArgumentException(nameof(weights));
                }
            }

            this.Layers = new Layer[layers.Length];

            for (int l = 0; l < layers.Length; l++)
            {
                Layer layer = new Layer(layers[l]);
                this.Layers[l] = layer;

                for (int n = 0; n < layers[l]; n++)
                {
                    var nDandrites = l > 0 ? layers[l - 1] : 0;
                    var param      = new NeuronParam(nDandrites);

                    if (l == 0)
                    {
                        param.Bias = 0;
                    }
                    else
                    {
                        if (hasWeight)
                        {
                            // Load data from weights
                            param.Bias = weights[readIndex++];

                            for (int d = 0; d < nDandrites; d++)
                            {
                                param.Weights[d] = weights[readIndex++];
                            }
                        }
                        else
                        {
                            // Set random data
                            param.Bias = new CryptoRandom().RandomValue;

                            for (int d = 0; d < nDandrites; d++)
                            {
                                param.Weights[d] = new CryptoRandom().RandomValue;
                            }
                        }
                    }

                    layer.Neurons[n] = new Neuron(param);
                }
            }
        }