/// <summary> /// Stimulate the specified neuron by the specified percent. This is used to /// randomize the weights and bias values for weak neurons. /// </summary> /// <param name="percent">The percent to randomize by.</param> /// <param name="layer">The layer that the neuron is on.</param> /// <param name="neuron">The neuron to randomize.</param> public void StimulateNeuron(double percent, ILayer layer, int neuron) { Distort d = new Distort(percent); if (layer.HasBias) { layer.BiasWeights[neuron] = d.Randomize(layer.BiasWeights[neuron]); } // calculate the outbound significance foreach (ISynapse synapse in layer.Next) { for (int i = 0; i < synapse.ToNeuronCount; i++) { double v = synapse.WeightMatrix[neuron, i]; synapse.WeightMatrix[neuron, i] = d.Randomize(v); } } ICollection <ISynapse> inboundSynapses = this.network.Structure .GetPreviousSynapses(layer); foreach (ISynapse synapse in inboundSynapses) { for (int i = 0; i < synapse.FromNeuronCount; i++) { double v = synapse.WeightMatrix[i, neuron]; synapse.WeightMatrix[i, neuron] = d.Randomize(v); } } }
//Methods for displaying public override void Init() { base.Init(); distort = transform.Find("Distort").GetComponent <Distort>(); underline = transform.Find("Underline").GetComponent <Image>(); distort.TurnOff(); currentWidth = widthNormal; underline.GetComponent <RectTransform>().sizeDelta = new Vector2(currentWidth, height); underline.gameObject.SetActive(false); textName.text = string.Empty; }
/// <summary> /// Used internally to randomize a neuron. Usually called from /// randomizeNeuron or stimulateNeuron. /// </summary> /// /// <param name="targetLayer">The target layer.</param> /// <param name="neuron">The target neuron.</param> /// <param name="useRange">True if range randomization should be used.</param> /// <param name="low">The low-end of the range.</param> /// <param name="high">The high-end of the range.</param> /// <param name="usePercent">True if percent stimulation should be used.</param> /// <param name="percent">The percent to stimulate by.</param> private void RandomizeNeuron(int targetLayer, int neuron, bool useRange, double low, double high, bool usePercent, double percent) { IRandomizer d; if (useRange) { d = new RangeRandomizer(low, high); } else { d = new Distort(percent); } // check for errors _network.ValidateNeuron(targetLayer, neuron); // access the flat network FlatNetwork flat = _network.Structure.Flat; // allocate new weights now that we know how big the new weights will be var newWeights = new double[flat.Weights.Length]; // construct the new weights int weightsIndex = 0; for (int fromLayer = flat.LayerCounts.Length - 2; fromLayer >= 0; fromLayer--) { int fromNeuronCount = _network .GetLayerTotalNeuronCount(fromLayer); int toNeuronCount = _network .GetLayerNeuronCount(fromLayer + 1); int toLayer = fromLayer + 1; for (int toNeuron = 0; toNeuron < toNeuronCount; toNeuron++) { for (int fromNeuron = 0; fromNeuron < fromNeuronCount; fromNeuron++) { bool randomize = false; if ((toLayer == targetLayer) && (toNeuron == neuron)) { randomize = true; } else if ((fromLayer == targetLayer) && (fromNeuron == neuron)) { randomize = true; } double weight = _network.GetWeight(fromLayer, fromNeuron, toNeuron); if (randomize) { weight = d.Randomize(weight); } newWeights[weightsIndex++] = weight; } } } // swap in the new weights flat.Weights = newWeights; }