public void UpdateWeights(NeuronLayer nextLayer, double alpha) { for (int i = 0; i < neurons.Length; i++) { neurons [i].UpdateErrorGradient(nextLayer.neurons, i, alpha); } }
public NeuralNet(NeuronMode mode, bool evolveActivationThreshold, int inputCount, int outputCount, int middleCount, int hiddenLayerCount) { this.mode = mode; inputLayer = new InputLayer(inputCount); outputLayer = new NeuronLayer(mode, outputCount, defaultActivationThreshold, evolveActivationThreshold); NeuronLayer prevLayer = null; for (int i = 0; i < hiddenLayerCount; ++i) { NeuronLayer newLayer = new NeuronLayer(mode, middleCount, defaultActivationThreshold, evolveActivationThreshold); hiddenLayers.Add(newLayer); if (prevLayer != null) { prevLayer.setOutputLayer(newLayer); } prevLayer = newLayer; } if (prevLayer != null) { prevLayer.setOutputLayer(outputLayer); inputLayer.setOutputLayer(hiddenLayers[0]); } else { inputLayer.setOutputLayer(outputLayer); } }
public static void Run() { var neuron1 = new Neuron() { Value = 2.3f }; var neuron2 = new Neuron() { Value = 0.3f }; var neuronLayer1 = new NeuronLayer(); var neuronLayer2 = new NeuronLayer(); neuronLayer1.Add(new Neuron() { Value = 0.54f }); neuronLayer2.Add(new Neuron() { Value = 1.3f }); neuronLayer2.Add(new Neuron() { Value = 4.6f }); // 1 neuron1.ConnectTo(neuron2); // 2 neuronLayer1.ConnectTo(neuron1); // 3 neuron2.ConnectTo(neuronLayer1); // 4 neuronLayer1.ConnectTo(neuronLayer2); }
public NeuralNetwork(int inputNb, int outputNb, int hiddenLayerNb = 1, int neuronsPerHiddenLayer = 2) { if (inputNb < 1 || outputNb < 1 || hiddenLayerNb < 1 || neuronsPerHiddenLayer < 1 /*2*/) { throw new ArgumentException(); } Gain = 1.0f; InputNb = inputNb; InputLayer = new NeuronLayer(this, InputNb); OutputNb = outputNb; OutputLayer = new NeuronLayer(this, OutputNb); HiddenLayerNb = hiddenLayerNb; NeuronsPerHiddenLayer = neuronsPerHiddenLayer; HiddenLayers = new NeuronLayer[HiddenLayerNb]; for (int i = 0; i < hiddenLayerNb; ++i) { HiddenLayers[i] = new NeuronLayer(this, NeuronsPerHiddenLayer); } CreateLinks(); }
public NeuralNetwork(NeuralNetwork other) { neuronLayers = new NeuronLayer[other.neuronLayers.Length]; // copy layers for (int iLayer = 0; iLayer < other.neuronLayers.Length; ++iLayer) { var otherNeuronLayer = other.neuronLayers[iLayer]; var currNeuronLayer = new NeuronLayer(); currNeuronLayer.InitNeurons(otherNeuronLayer.neurons.Length); // copy neurons for (int iNeuron = 0; iNeuron < currNeuronLayer.neurons.Length; ++iNeuron) { var otherNeuron = otherNeuronLayer.neurons[iNeuron]; // copy other neurons if (otherNeuron.weights != null) { var currNeuron = currNeuronLayer.neurons[iNeuron]; currNeuron.InitWeights(otherNeuron.weights.Length); // copy weights for (int iWeight = 0; iWeight < otherNeuron.weights.Length; ++iWeight) { currNeuron.weights[iWeight] = otherNeuron.weights[iWeight]; currNeuron.val = 0.0f; } } } neuronLayers[iLayer] = currNeuronLayer; } }
public void InitNeurons(NeuronLayer aInputLayer, NeuronLayer aSourceLayer) { for (int i = 0; i < neurons.Count; i++) { neurons[i].Link(aInputLayer.neurons, aSourceLayer.neurons[i].inputs); } }
public NeuralNetwork() { hidden = new NeuronLayer(20); output = new NeuronLayer(26); inHidden = new double[256, 20]; hiddenOut = new double[20, 26]; string[] lines = File.ReadAllLines(textFile); for (int i = 0; i < 256; i++) { string[] numbers = lines[i].Split(';'); for (int j = 0; j < 20; j++) { inHidden[i, j] = Convert.ToDouble(numbers[j]); } } for (int i = 0; i < 20; i++) { string[] numbers = lines[256 + i].Split(';'); for (int j = 0; j < 26; j++) { hiddenOut[i, j] = Convert.ToDouble(numbers[j]); } } UnityEngine.Debug.Log(inHidden[25, 15]); learningSpeed = 0.5; }
/// <summary> Creates, loads, and returns a <see cref="NeuralNetwork"/> using the given <paramref name="networkLoader"/>. </summary> /// <param name="networkLoader"> The <see cref="INetworkLoader"/> used to load the network. </param> /// <returns> The loaded <see cref="NeuralNetwork"/>. </returns> public static NeuralNetwork Load(INetworkLoader networkLoader) { // Get the number of neuron layers in the file. int neuronLayerCount = networkLoader.GetNetworkNeuronLayerCount(); // Create an empty neural network. NeuralNetwork neuralNetwork = new NeuralNetwork(networkLoader.GetNetworkLearningRate(), neuronLayerCount); // Create the neuron layers for the neural network. for (uint i = 0; i < neuronLayerCount; i++) { neuralNetwork.neuronLayers[i] = NeuronLayer.Load(neuralNetwork, networkLoader, i); } // Create the weight layers for the neural network. for (uint i = 0; i < neuronLayerCount - 1; i++) { neuralNetwork.weightLayers[i] = WeightLayer.Load(neuralNetwork, networkLoader, i, neuralNetwork.neuronLayers[i], neuralNetwork.neuronLayers[i + 1]); } // Set the highest neuron count. neuralNetwork.HighestNeuronCount = calculateHighestNeuronCount(neuralNetwork); // Return the neural network. return(neuralNetwork); }
public void BasicTest() { var neuron1 = new Neuron(0.1f); var neuron2 = new Neuron(0.2f); var neuron3 = new Neuron(0.3f); var neuron4 = new Neuron(0.4f); var neuron5 = new Neuron(0.5f); var neuron6 = new Neuron(0.6f); neuron1.ConnectTo(neuron2); var layer1 = new NeuronLayer(); layer1.Add(neuron3); layer1.Add(neuron4); var layer2 = new NeuronLayer(); layer2.Add(neuron5); layer2.Add(neuron6); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); Assert.IsTrue(neuron1.Out.Count == 3); Assert.IsTrue(layer1.ToList()[0].In.Count == 1); Assert.IsTrue(layer2.ToList()[0].In.Count == 2); }
public void SetUp() { layer2 = new NeuronLayer(); layer1 = new NeuronLayer(layer2); double[,] weights1 = new double[, ] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } }; double[,] weights2 = new double[, ] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } }; for (int i = 0; i < NEURON_SIZE; i++) { Neuron neuron1 = new Neuron(LinearFunction.Instance); for (int j = 0; j <= weights1.GetUpperBound(0); j++) { neuron1.Weights.Add(weights1[i, j]); } layer1.AddNeuron(neuron1); Neuron neuron2 = new Neuron(LinearFunction.Instance); for (int j = 0; j <= weights2.GetUpperBound(0); j++) { neuron2.Weights.Add(weights2[i, j]); } layer2.AddNeuron(neuron2); } }
static void Main(string[] args) { var drawing = new GraphicObject { Name = "My Drawing" }; drawing.Children.Add(new Square { Color = "Red" }); drawing.Children.Add(new Circle { Color = "Yellow" }); var group = new GraphicObject(); group.Children.Add(new Circle { Color = "Blue" }); group.Children.Add(new Square { Color = "Blue" }); drawing.Children.Add(group); WriteLine(drawing); var neuron1 = new Neuron(); var neuron2 = new Neuron(); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(neuron2); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); }
public NeuralNetwork(int[] layerSizes) { //UnityEngine.Random.InitState(505); neuronLayers = new NeuronLayer[layerSizes.Length]; for (int iLayer = 0; iLayer < layerSizes.Length; ++iLayer) { var numNeuronsInLayer = layerSizes[iLayer]; // create neurons var currNeuronLayer = new NeuronLayer(); currNeuronLayer.InitNeurons(numNeuronsInLayer); // connect to previous neurons if (iLayer > 0) { var prevNeuronLayer = neuronLayers[iLayer - 1]; for (int iNeuron = 0; iNeuron < numNeuronsInLayer; ++iNeuron) { var neuron = currNeuronLayer.neurons[iNeuron]; neuron.InitWeights(prevNeuronLayer.neurons.Length); } } neuronLayers[iLayer] = currNeuronLayer; } }
public NeuralNetwork(int layerCount, int neuronCount, int inputCount, int outputCount) { graph = new NeuronLayer[layerCount]; for (int i = 0; i < layerCount; ++i) { graph[i] = new NeuronLayer(); graph[i].neurons = new Neuron[neuronCount]; for (int j = 0; j < neuronCount; ++j) { var weightCount = neuronCount; if (i == 0) { weightCount = inputCount; } graph[i].neurons[j] = new Neuron(); graph[i].neurons[j].weights = new float[weightCount]; for (int k = 0; k < weightCount; ++k) { graph[i].neurons[j].weights[k] = InitializeWeight(); } } } }
static void Main(string[] args) { //var drawing = new GraphicObjects(); //drawing.Name = "My Drawing"; ////single addition //drawing.Children.Add(new Square{Color = "Red"}); //drawing.Children.Add(new Circle{Color = "Blue"}); ////collection addition //var group = new GraphicObjects(); //group.Children.Add(new Circle{Color = "Green"}); //group.Children.Add(new Square{Color = "Yelllow"}); //drawing.Children.Add(group); //Console.WriteLine(drawing); var neuron1 = new Neuron(); var neuron2 = new Neuron(); neuron1.ConnectTo(neuron2); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); }
/// <summary> /// Inicializa a rede neural /// </summary> /// <param name="id">Identificador da rede neural</param> /// <param name="inputLayerSize">Tamanho da camada de entrada da rede neural</param> /// <param name="hiddenLayerSize">Tamanho da camada oculta da rede neural</param> /// <param name="outputLayerSize">Tamanho da camada de saída da rede neural</param> public MultiLayerPerceptron(int id, int inputLayerSize, int hiddenLayerSize, int outputLayerSize) { this.id = id; this.inputLayerSize = inputLayerSize; this.hiddenLayer1 = new NeuronLayer(1, hiddenLayerSize, inputLayerSize); this.hiddenLayer2 = new NeuronLayer(2, hiddenLayerSize, hiddenLayerSize); this.outputLayer = new NeuronLayer(3, outputLayerSize, hiddenLayerSize); }
public static void PrintNeuronLayerWeights(string name, NeuronLayer layer) { Console.WriteLine(name); foreach (Neuron neuron in layer) { ArraysUtil<double>.Print(neuron.Weights.ToArray()); } }
void Update() { if (testInputs.Count != testOutputs.Count) { Debug.Log("Skipping test, please ensure that there are the same number of test input sets as test output sets"); return; } if (useBackPropagation && neuralNetwork == null) { neuralNetwork = new NeuralNetwork(bestNeuralNetwork); } if (shouldMutate) { neuralNetwork = new NeuralNetwork(bestNeuralNetwork); if (lowestError != 0.0f) { neuralNetwork.MutateWeights(weightMutationChance, -learningRate, learningRate); } } error = 0; outputText.text = ""; for (int i = 0; i < testInputs.Count && testInputs[i].Count > 1; i++) { neuralNetwork.FeedForward(testInputs[i].ToArray()); NeuronLayer outputLayer = neuralNetwork.OutputLayer; if (outputLayer != null && outputLayer.Neurons.Count > 0) { outputText.text += outputLayer.Neurons[0].Output + System.Environment.NewLine; if (useBackPropagation) { neuralNetwork.BackPropagate(new float[] { testOutputs[i] }); } if (shouldMutate) { error += Mathf.Abs(testOutputs[i] - outputLayer.Neurons[0].Output); } } } if (shouldMutate) { if (error < lowestError) { lowestError = error; bestNeuralNetwork = new NeuralNetwork(neuralNetwork); } } }
public Neuron(NeuronLayer nLayer, int inputNr) { parent = nLayer; for (int i = 0; i <= inputNr; i++) { weights.Add(MathUtility.getRandomWeight()); //Console.WriteLine("Initialized weights {0}", weights[i]); } }
public TestSeer() { NeuronLayer[] nL = new NeuronLayer[4]; nL[0] = new NeuronLayer(3, 5); nL[1] = new NeuronLayer(5, 5); nL[2] = new NeuronLayer(5, 5); nL[3] = new NeuronLayer(5, 3, new Logistic()); //nL[2] = new NeuronLayer(2, 1); nlg = new NeuronLayerGroup(nL); }
public void SetPreviousLayer(NeuronLayer previousLayer) { PreviousNeuronsNb = previousLayer.NeuronNb; PreviousNeurons = new Neuron[PreviousNeuronsNb]; for (int i = 0; i < PreviousNeuronsNb; ++i) { PreviousNeurons[i] = previousLayer.Neurons[i]; } }
static void Composite() { var neuron1 = new Neuron(); var neuron2 = new Neuron(); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(neuron2); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); }
public void setOutputLayer(NeuronLayer layer) { System.Random random = GlobalRandom.getInstance().getRandom(); foreach (IOutputValue input in inputs) { foreach (AbstractNeuron neuron in layer.getNeuronList()) { neuron.addInput(input, random.NextDouble() - random.NextDouble()); } } }
public void Run() { var neuron1 = new Neuron(); var neuron2 = new Neuron(); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(neuron2); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); }
public void setOutputLayer(NeuronLayer outputLayer) { System.Random random = GlobalRandom.getInstance().getRandom(); foreach (AbstractNeuron neuron in neuronList) { foreach (AbstractNeuron output in outputLayer.neuronList) { output.addInput(neuron, random.NextDouble() - random.NextDouble()); } } }
static void Main(string[] args) { var neuron1 = new Neuron(); var neuron2 = new Neuron(); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(neuron2); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); }
public void ConnectTo_ConnectLayerToNeuron_ValidateIn() { var neuron = new Neuron(); var layer = new NeuronLayer(); var neuronInLayer = new Neuron(); layer.Add(neuronInLayer); layer.ConnectTo(neuron); Assert.That(neuron.In[0], Is.InstanceOf <Neuron>()); }
public void ClassTest() { var neuron = new Neuron(0.1f); Assert.IsTrue(neuron.Value == 0.1f); Assert.IsTrue(neuron.In.Count == 0); Assert.IsTrue(neuron.Out.Count == 0); Assert.IsTrue(neuron is IEnumerable); var layer = new NeuronLayer(); Assert.IsTrue(layer is Collection <Neuron>); }
public NeuralNetwork(int num_inputs, int num_hidden, int num_outputs, List <float> hidden_layer_weights = null, float hidden_layer_bias = float.NaN, List <float> output_layer_weights = null, float output_layer_bias = float.NaN) { this.num_inputs = num_inputs; //this.input_layer = new NeuronLayer(num_inputs, 0.5f); //Añadido this.hidden_layer = new NeuronLayer(num_hidden, hidden_layer_bias); this.output_layer = new NeuronLayer(num_outputs, output_layer_bias); //this.init_weights_from_inputs_to_input_layer_neurons(); this.init_weights_from_inputs_to_hidden_layer_neurons(hidden_layer_weights); this.init_weights_from_hidden_layer_neurons_to_output_layer_neurons(output_layer_weights); }
static void RunCompositeDesignPattern2() { Neuron neuron1 = new Neuron(); Neuron neuron2 = new Neuron(); neuron1.ConnectTo(neuron2); NeuronLayer neuronLayer1 = new NeuronLayer(); NeuronLayer neuronLayer2 = new NeuronLayer(); neuronLayer1.ConnectTo(neuronLayer2); }
public static void CommonInterfaceDemo() { var neuron1 = new Neuron(); var neuron2 = new Neuron(); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(neuron2); neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); WriteLine("This example does not provide any output, please check the code."); }
public void Start() { var neuron = new Neuron(); var neuron2 = new Neuron(); neuron.ConnectTo(neuron2); var layer = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron.ConnectTo(layer); layer.ConnectTo(layer2); }
public SimplifiedFuzzyARTMap(int inputSizeA, int inputSizeB, double artABasedVigilance, double artAChoiceValue, double artABeta) { this.map = new NeuronLayer(); this.Beta = 1.0; this.artA = new SimplifiedFuzzyART(inputSizeA, 0.5, artAChoiceValue, false); this.artB = new SimplifiedFuzzyART(inputSizeB, 1.0, 0.0001, false); this.Vigilance = 1.0; this.BasedVigilanceA = artABasedVigilance; this.ArtA.Beta = artABeta; this.BasedVigilanceB = 1.0; this.ArtB.Beta = 1.0; }
public SimplifiedFuzzyART(int inputSize, double vigilance, double choice, bool autoAdjustWeight) { this.inputSize = inputSize; Vigilance = vigilance; Choice = choice; AutoAdjustWeight = autoAdjustWeight; this.f2 = new NeuronLayer(); this.f1 = new NeuronLayer(this.f2); choiceValues = new List<double>(); Initialize(); }
public static void CreateCompositeNueral() { var neuron1 = new Neuron(); var neuron2 = new Neuron(); var layer1 = new NeuronLayer(); var layer2 = new NeuronLayer(); neuron1.ConnectTo(neuron2); // this is possible for a single neutron is seen as an element // in a collection (a monoelement collection) neuron1.ConnectTo(layer1); layer1.ConnectTo(layer2); }
public void SetUp() { layer2 = new NeuronLayer(); layer1 = new NeuronLayer(layer2); double[,] weights1 = new double[,] { { 1, 1,1 ,1 }, { 1, 1, 1, 1 }, {1, 1, 1, 1}, { 1, 1, 1, 1}}; double[,] weights2 = new double[,]{ { 1, 1, 1,1 }, { 1, 1, 1, 1 }, {1, 1, 1, 1}, { 1, 1, 1, 1}}; for (int i = 0; i < NEURON_SIZE; i++) { Neuron neuron1 = new Neuron(LinearFunction.Instance); for(int j = 0; j <= weights1.GetUpperBound(0); j++) neuron1.Weights.Add(weights1[i,j]); layer1.AddNeuron(neuron1); Neuron neuron2 = new Neuron(LinearFunction.Instance); for (int j = 0; j <= weights2.GetUpperBound(0); j++) neuron2.Weights.Add(weights2[i,j]); layer2.AddNeuron(neuron2); } }
//Creates a network of neurons based on the initialized values of the neural network. private void constructNetwork() { layers = new NeuronLayer[numLayers]; //Create first layer layers[0] = new NeuronLayer((numLayers == 1? numOutputs: numNeuronsPerLayer), numInputs); //If more than one layer, create inner layers. if (numLayers > 1) { //Create all inner layers for (int currLayer = 1; currLayer < numLayers-1; ++currLayer) { layers[currLayer] = new NeuronLayer(numNeuronsPerLayer, numNeuronsPerLayer); } //Create output layer layers[numLayers-1] = new NeuronLayer(numOutputs, numNeuronsPerLayer); } }
public NeuronNet(int[] layerSizes, float[] initialWeights) { // TODO: Clear distinction between the layers. DebugAux.Assert(layerSizes.Length >= 2, "Must have at least 2 layers"); layers = new NeuronLayer[layerSizes.Length-1]; int counter = 0; for (int i = 0; i < layers.Length; i++) { int size = (layerSizes[i]+1) * layerSizes[i+1]; float[] dest = new float[size]; Array.Copy(initialWeights, counter, dest, 0, size); counter += size; layers[i] = new NeuronLayer(layerSizes[i], layerSizes[i+1], dest); } }