internal Training(NeuralNetwork Network, TrainingMode mode) : base(Network, DetermineIterationRepeatPars(Network)) { Contract.Requires(Network != null); Mode = mode; if (Mode == TrainingMode.Streamed && (GCAlgo != GradientComputingAlgorithm.RTLR || GCAlgo != GradientComputingAlgorithm.None)) { throw new InvalidOperationException("Only RTLR allowed for Streamed training. You have to use Recurrent NN with RTLR Algorithm in RecurrentOptions."); } if ((Network.StructuralElementFlags & NNStructuralElement.GradientInformation) != 0) { if (Network.IsRecurrent) { GCAlgo = Network.RecurrentOptions.Algorithm == RLAlgorithm.BPTT ? GradientComputingAlgorithm.BPTT : GradientComputingAlgorithm.RTLR; } else { GCAlgo = GradientComputingAlgorithm.BP; } } else { GCAlgo = GradientComputingAlgorithm.None; } }
public SynapsesControl(NeuralNetwork NN, Layer from, Layer to) { Initialize(); this.NeuralNetwork = NN; From = from; To = to; }
internal Training(NeuralNetwork network, TrainingMode mode) : base(network, DetermineIterationRepeatPars(network)) { Contract.Requires(network != null); Mode = mode; }
public AdjustedNeuralNetworkPopulation Create(NeuralNetwork network, NeuralNetworkTest test, out AdjustedNeuralNetworkBodyFactory bodyFactory) { Contract.Requires(network != null); Contract.Requires(test != null); return DoCreate(network, test, out bodyFactory); }
public override void InitPlayer() { if (!isInited) { id++; teamGoally = transform.parent.gameObject.GetComponentInChildren<GoallyPlayer>(); if (teamGoally.transform.position.x < 0) { isRedTeam = false; } else { isRedTeam = true; } AttackPlayer[] list = oponentTeam.GetComponentsInChildren<AttackPlayer>(); foreach (AttackPlayer a in list) { if (a.NameType == GameConsts.ATTACK_PLAYER) { oponentAttacker = a; break; } } rgBody = GetComponent<Rigidbody2D>(); ballScript = FindObjectOfType<BallScript>(); brain = new NeuralNetwork(NeuralNetworkConst.DEFENSE_INPUT_COUNT, NeuralNetworkConst.DEFENSE_OUTPUT_COUNT, NeuralNetworkConst.DEFENSE_HID_LAYER_COUNT, NeuralNetworkConst.DEFENSE_NEURONS_PER_HID_LAY); isInited = true; } }
protected AdjusterLearningEpoch(NeuralNetwork network, NeuralNetworkTest test = null) : base(test) { Contract.Requires(network != null); Network = network; }
public static NeuralNetwork CreateFullConnected( int inputInterfaceLength, int outputInterfaceLength, int neuronCount, Func<Synapse> synapseFactoryMethod, Func<Neuron> neuronFactoryMethod, bool feedForward = true) { Contract.Requires(inputInterfaceLength > 0); Contract.Requires(outputInterfaceLength > 0); Contract.Requires(synapseFactoryMethod != null); Contract.Requires(neuronFactoryMethod != null); Contract.Requires(neuronCount > 0); Contract.Requires(feedForward == true); // TODO: Non feed-forward var network = new NeuralNetwork(inputInterfaceLength, outputInterfaceLength); ComputationalArchitecture.InitializeFullConnected( network, neuronCount, synapseFactoryMethod, neuronFactoryMethod, feedForward); return network; }
public static NeuralNetwork CreateLayered( int inputInterfaceLength, int outputInterfaceLength, Func<Synapse> synapseFactoryMethod, Func<Neuron> neuronFactoryMethod, bool feedForward, params int[] neuronCounts) { Contract.Requires(inputInterfaceLength > 0); Contract.Requires(outputInterfaceLength > 0); Contract.Requires(synapseFactoryMethod != null); Contract.Requires(neuronFactoryMethod != null); Contract.Requires(feedForward == true); // TODO: Non feed-forward Contract.Requires(!neuronCounts.IsNullOrEmpty()); Contract.Requires(Contract.ForAll(neuronCounts, (c) => c > 0)); var network = new NeuralNetwork(inputInterfaceLength, outputInterfaceLength); ComputationalArchitecture.InitializeLayered( network, synapseFactoryMethod, neuronFactoryMethod, feedForward, neuronCounts); return network; }
void Start() { EntityBrain = new NeuralNetwork(); EntityBrain.Initialize(15, 50, 4); EntityBrain.SetLearningRate(0.2f); EntityBrain.SetMomentum(true, 0.9f); }
internal Training(NeuralNetwork network, BufferAllocator allocator, TrainingMode mode) : base(network, DetermineIterationRepeat(network), allocator) { Contract.Requires(network != null); Contract.Requires(allocator != null); Mode = mode; if (Mode == TrainingMode.Streamed && (GCAlgo != GradientComputingAlgorithm.RTLR || GCAlgo != GradientComputingAlgorithm.None)) { throw new InvalidOperationException("Only RTLR allowed for Streamed training. You have to use Recurrent NN with RTLR Algorithm in RecurrentOptions."); } if ((network.StructuralElementFlags & NNStructuralElement.GradientInformation) != 0) { if (network.IsRecurrent) { GCAlgo = network.RecurrentOptions.Algorithm == RLAlgorithm.BPTT ? GradientComputingAlgorithm.BPTT : GradientComputingAlgorithm.RTLR; } else { GCAlgo = GradientComputingAlgorithm.BP; } } else { GCAlgo = GradientComputingAlgorithm.None; } if (GCAlgo == GradientComputingAlgorithm.BPTT) { savedErrorVectors = new ErrorVectorStack(network, allocator); } }
public Recognition(string fileName,bool isDBN) { if (isDBN) dbn = DBN.Load(fileName); else ann = NeuralNetwork.Load(fileName); }
public SynapsesSettings(NeuralNetwork nn, Layer from, Layer to) { InitializeComponent(); NeuralNetwork = nn; From = from; To = to; SetUI(); }
/// <param name="N">ANN to train</param> public LearningAlgorithm(NeuralNetwork N) { ANN = N; ErrorTreshold = 0.005f; MaximumOfEpochs = 10000; CurrentEpoch = 0; MeanSquareError = -1.0f; }
public CompetitiveMSEAQALearningStrategy(NeuralNetwork network, int competitorCount = 10) : base(network) { Contract.Requires(network != null); Contract.Requires(competitorCount > 0); this.competitorCount = competitorCount; }
public void AddInboundConnection_ValidatesArgs() { // Setup var network = new NeuralNetwork(); // Execute/Verify network.AddInboundConnection(null); }
public BackPropagationLearningEpoch(BackPropagationRule rule, NeuralNetwork network, NeuralNetworkTest test = null) : base(network, test) { Contract.Requires(network != null); Contract.Requires(rule != null); Rule = rule; }
public void AddInputNode_ValidatesArgs() { // Setup var network = new NeuralNetwork(); // Execute/Verify network.AddInputNode(null); }
public QSALearningStrategy(NeuralNetwork network, double strength = 1.0) : base(network) { Contract.Requires(network != null); Contract.Requires(strength > 0.0); this.strength = strength; }
// Use this for initialization public Neuron(NeuralNetwork parent, int numInputs, int outputType) { //This is called in the first generation to set it up randomly this.parent = parent; weights = new float[numInputs + 1];//The last weight will be the threshold for (int i = 0; i < weights.Length; i++) { weights [i] = Random.value * 2 - 1; } this.outputType = outputType; this.numInputs = numInputs; }
public AdjustedNeuralNetworkPopulation Create(NeuralNetwork network, NeuralNetworkTest test) { Contract.Requires(network != null); Contract.Requires(test != null); AdjustedNeuralNetworkBodyFactory temp; var pop = DoCreate(network, test, out temp); return pop; }
public static double CalculateError(ref Example[] dataset, ref NeuralNetwork network) { double error = 0.0; foreach (Example example in dataset) { network.classify(example.ToNetworkInput()); error += network.squaredError(example.ToNetworkOutput()); } return error * 0.5; }
void Start() { inputs = new float[1]; neuralNetwork = gameObject.AddComponent<NeuralNetwork>(); neuralNetwork.inputs = inputs; neuralNetwork.nbOutputs = 1; trainer = gameObject.AddComponent<RPropTrainer>(); mover = GetComponent<Mover>(); errors = new float[neuralNetwork.nbOutputs]; }
// Use this for initialization void Start () { pos = transform.position; player = GameObject.Find ("Commit"); outputs = new double[2]; net = new NeuralNetwork (9, 2, 6); net.readWeights (); ctrl = player.GetComponent<CommitController> (); wall = player.GetComponent<WallSensor> (); adj = player.GetComponent<AdjacentSensor> (); pie = player.GetComponent<PieSliceSensor> (); }
public GALearningStrategy( NeuralNetwork network, AdjustedNeuralNetworkPopulationFactory populationFactory = null) : base(network) { Contract.Requires(network != null); this.populationFactory = populationFactory == null ? new AdjustedNeuralNetworkPopulationFactory() : populationFactory; }
// Use this for initialization public void Start() { Stopper s = new Stopper(); Console.WriteLine("How many hidden nodes?"); hidden = int.Parse(Console.ReadLine()); Console.WriteLine("Epoch counter?"); int epochCtr = int.Parse(Console.ReadLine()); Console.WriteLine("Error threshold?"); s.errorThresh = float.Parse(Console.ReadLine()); Thread oThread = new Thread(new ThreadStart(s.run)); oThread.Start(); String outPath = "C:/Users/SwoodGrommet/Desktop/game-ai/Assets/Scripts/Datas/"; outputFile = "weights.txt"; sets = new ArrayList(); rec = new Recorder(); nn = new NeuralNetwork (input, output, hidden); numWeights = (input * hidden) + (hidden * output) + (hidden + output); weights = getRandomWeights (numWeights); nn = new NeuralNetwork (input, output, hidden); nn.setWeights (weights); Console.WriteLine("chutzpah"); rec.read (this.sets); read = true; double changeDel = 0; while (/*!trained &&*/ read) { double errors = 0; //do { foreach (TrainingSet set in this.sets) { setInputs(set.getInputs()); setTargets(set.getTargets()); errors += train(); } //Debug.Log("Current error : " + errors); //} while (error > 0.0001); error = errors / sets.Count; if (s.output) { Console.WriteLine("Outputting weights to file"); this.weights = nn.getWeights(); nn.outputWeights(weights); s.output = false; } if (s.errorThresh > error || s.stop) { Console.WriteLine("All trainings are done!!!"); break; } //Console.WriteLine(ctr + ": " + errors); if (ctr % epochCtr == 0) { Console.WriteLine("epoch: " + ctr / epochCtr + "; error: " + error + "; Delta: " + (changeDel - error)); changeDel = error; } ctr++; } }
// Use this for initialization void Start () { this.outputFile = Application.dataPath + "/Datas/weights.txt"; this.numWeights = (input * hidden) + (hidden * output) + (hidden + output); this.weights = NeuralNetworkHelpers.getRandomWeights (numWeights); this.nn = new NeuralNetwork (input, output, hidden); this.nn.setWeights (weights); this.error = double.MaxValue; this.trained = false; this.rec = new Recorder(); this.trainingSets = this.rec.read(input, output); }
public GenderComputation(NeuralNetwork network, int numberOfIterations = 1) { Contract.Requires(network != null); Contract.Requires(numberOfIterations > 0); Network = network; this.binaryOutput = network.OutputInterface.Length == 2; NumberOfIterations = numberOfIterations; VerifyNetwork(); if (numberOfIterations != 1) resetHandler = new ResetHandler(Network, false); }
public int Train(DataSet dataSet) { var data = dataSet.Signatures.Select(i => i.Data.Cast<double>().ToArray()).ToArray(); var dict = new Dictionary<string, double[]>(); for (int i = 0; i < data.Count(); i++) { dict.Add((i+1).ToString(), data[i]); } network = new NeuralNetwork<string>(new Layer3<string>(8*21, (int)((double)((8*21 + 4)*FirstLayerParameter)), (int)((double)((8 * 21 + 4) * SecondLayerParameter)), 4), dict); return 1; }
private static int DetermineIterationRepeat(NeuralNetwork Network) { return 5; // TODO: Fix this int max = 1; foreach (var bwRule in Network.Layers.OfType<IHasLearningRules>().SelectMany(l => l.LearningRules).OfType<ErrorBasedLearningRule>()) { if (bwRule.IterationRepeat > max) max = bwRule.IterationRepeat; } return max; }
public NeuralNetworkTrainer(GameObject player) { this.player = player; target = GameObject.Find ("timmoc"); wall = this.player.GetComponent<WallSensor> (); //pie = GetComponent<PieSliceSensor> (); adj = this.player.GetComponent<AdjacentSensor> (); ctrl = this.player.GetComponent<CommitController> (); numWeights = (input * hidden) + (hidden * output) + (hidden + output); readWeights (); nn = new NeuralNetwork (input, output, hidden); nn.setWeights (weights); generation = 0; }
/// <summary> /// Initializes a new instance of the <see cref="Visualizer" /> class. /// </summary> /// <param name="neuralNetwork">The neural network.</param> /// <param name="visualizationOptions">The visualization options.</param> public Visualizer(NeuralNetwork neuralNetwork, VisualizationOptions visualizationOptions) { this.neuralNetwork = neuralNetwork; this.visualizationOptions = visualizationOptions; CalculateValues(); }
public Synapse(Neuron inputNeuron, Neuron outputNeuron) { this.inputNeuron = inputNeuron; this.outputNeuron = outputNeuron; this.weight = NeuralNetwork.NextRandom(); }
private void Button_Click_3(object sender, RoutedEventArgs e) { tests.ItemsSource = NeuralNetwork.visualize() .Select(BitmapToImageSource); }
public NeuralNetwork getIndividualController() { network = new NeuralNetwork(topology); network.map_from_linear(genotype); return(network); }
// Update is called once per frame void Update() { if (!isStarted) { return; } var sb = new StringBuilder(); int alive = 0; sb.Append($"Time left: {System.Math.Round(Countdown, 1)}\n"); sb.Append($"Generation {generationNumber}\n"); Countdown -= Time.deltaTime; if (nets != null && nets.Count > 0 && humanList.Count > 0) { nextGen = true; foreach (var human in humanList) { if (!human.dontReward) { nextGen = false; alive++; } } if (nextGen) { Countdown = -1; } } if (Countdown < 0f) { lateCameraFollow.TrackingObject = null; if (generationNumber == 0) { sb.Append($"Best current fitness: 0\n"); InitHumanNeuralNetworks(); } else { if (nextGen) { cause = CauseOfLastGenDeath.EveryoneDied; } else { cause = CauseOfLastGenDeath.Timeout; } for (int i = 0; i < humanList.Count; i++) { AverageScores.Add(nets[i].GetFitness()); } nets.Sort(); float lastMaxFitness = nets[nets.Count - 1].GetFitness(); bestLastFitness = lastMaxFitness; bestLastTimeAlive = nets[nets.Count - 1].GetTimeAlive(); while (FitGraph.Count >= 20) { FitGraph.RemoveAt(0); } while (TimeGraph.Count >= 20) { TimeGraph.RemoveAt(0); } FitGraph.Add(bestLastFitness); TimeGraph.Add(bestLastTimeAlive); DrawGraph(TextGraph, FitGraph); DrawGraph(TextGraphTime, TimeGraph); if (bestOverallFitness < lastMaxFitness) { bestOverallFitness = lastMaxFitness; bestOverallGenFitness = generationNumber; netBestScore = nets[nets.Count - 1]; } if (bestOverallTimeAlive < bestLastTimeAlive) { bestOverallTimeAlive = bestLastTimeAlive; bestOverallGenTimeAlive = generationNumber; netBestTime = nets[nets.Count - 1]; } for (int i = 0; i < populationSize / 2; i++) { nets[i] = new NeuralNetwork(nets[i + (populationSize / 2)]); nets[i].Mutate(i == 0); nets[i + (populationSize / 2)] = new NeuralNetwork(nets[i + (populationSize / 2)]); } for (int i = 0; i < populationSize; i++) { nets[i].SetFitness(0); } } generationNumber++; sb.Append($"Best current fitness: {generationNumber}\n"); wod.Restart(); Countdown = LearningTime; CreateHumanBodies(); lateCameraFollow.TrackingObject = humanList[0].gameObject; } if (nets.Count > 0 && humanList.Count > 0 && Countdown > 0) { float bestScore = float.MinValue; int bestI = 0; for (int i = 0; i < nets.Count; i++) { var netScore = nets[i].GetFitness(); if (netScore > bestScore && !humanList[i].dontReward) { bestScore = netScore; bestI = i; } } for (int i = 0; i < humanList.Count; i++) { if (i == bestI && bestScore >= 0) { humanList[i].MakeVisible(); } else { humanList[i].Shade(); } } if (bestScore >= 0) { lateCameraFollow.TrackingObject = humanList[bestI].gameObject; } else { lateCameraFollow.TrackingObject = null; } sb.Append($"Best current fitness: {System.Math.Round(bestScore, 2)}\n"); } if (bestLastFitness >= 0) { sb.Append($"Best last fitness: {System.Math.Round(bestLastFitness, 2)}\n"); sb.Append($"Best last time alive: {System.Math.Round(bestLastTimeAlive, 2)}\n"); } if (bestOverallFitness >= 0 && bestOverallGenFitness > 0) { sb.Append($"Best overall fitness was {System.Math.Round(bestOverallFitness, 2)} in Gen {bestOverallGenFitness}\n"); sb.Append($"Best overall time alive was {System.Math.Round(bestOverallTimeAlive, 2)}s in Gen {bestOverallGenTimeAlive}\n"); } if (AverageScores.Count > 0) { float score = 0f; for (int i = 0; i < AverageScores.Count; i++) { score += AverageScores[i]; } score /= AverageScores.Count; sb.Append($"Average overall was {score}\n"); } if (generationNumber > 1) { sb.Append($"Cause of last gen death: {(cause == CauseOfLastGenDeath.Timeout ? "Timeout" : "Everyone died")}\n"); } sb.Append($"Alive: {alive}/{populationSize}\n"); TextInfo.text = sb.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="Chromosome"/> class with given <paramref name="id"/> and <paramref name="network"/>. /// </summary> /// <param name="id">The new Chromosome's id.</param> /// <param name="network">The new Chromosome's neural network.</param> public Chromosome(string id, NeuralNetwork network) { Id = id; Network = network; }
static void Main(string[] args) { Tensor.SetOpMode(Tensor.OpMode.MultiCPU); var input1 = new Dense(2, 2, Activation.Sigmoid) { Name = "input1" };; var upperStream1 = new Dense(input1, 2, Activation.Linear) { Name = "upperStream1" };; var lowerStream1 = new Dense(input1, 2, Activation.Linear) { Name = "lowerStream1" }; var net = new NeuralNetwork("test"); net.Model = new Flow(new[] { input1 }, new[] { upperStream1, lowerStream1 }); net.Optimize(new SGD(0.05f), Loss.MeanSquareError); var input = new Tensor(new float[] { 0, 1 }, new Shape(1, 2)); var outputs = new [] { new Tensor(new float[] { 0, 1 }, new Shape(1, 2)), new Tensor(new float[] { 1, 2 }, new Shape(1, 2)) }; var trainingData = new List <Data> { new Data(new[] { input }, outputs) }; var netClone = net.Clone(); netClone.Fit(trainingData, 1, 60, null, 2, Track.Nothing, false); //var input1 = new Dense(2, 2, Activation.Sigmoid); //var upperStream1 = new Dense(input1, 2, Activation.Sigmoid); //var upperStream2 = new Dense(upperStream1, 2, Activation.Sigmoid) { Name = "upperStream2" }; //var lowerStream1 = new Dense(input1, 2, Activation.Sigmoid) { Name = "lowerStream1" }; //var merge = new Merge(new[] {upperStream2, lowerStream1}, Merge.Mode.Sum) { Name = "merge1" }; //var net = new NeuralNetwork("test"); //net.Model = new Flow(new[] { input1 }, new[] { merge }); //net.Optimize(new SGD(), new Dictionary<string, LossFunc>{ {"upperStream2", Loss.MeanSquareError}, { "lowerStream1", Loss.Huber1 } }); /*var inputs = new Tensor(new float[] { 1,1,2,2,3,3,4,4,5,5,6,6,2,3,4,5,6,7,8,9,0,1 }, new Shape(1, 2, 1, 11)); * var outputs = new Tensor(new float[] { 2,2,3,3,4,4,5,5,6,6,7,7,3,4,5,6,7,8,9,10,1,2 }, new Shape(1, 2, 1, 11)); * * var net = new NeuralNetwork("test"); * net.AddLayer(new Dense(2, 5, Activation.Sigmoid)); * net.AddLayer(new Dense(net.LastLayer, 4, Activation.Sigmoid)); * net.AddLayer(new Dense(net.LastLayer, 2, Activation.Linear)); * * var l0 = net.Layer(0) as Dense; * l0.Weights = new Tensor(new[] {-0.5790837f , 0.79525125f, -0.6933877f , -0.3692013f , 0.1810553f, * 0.03039712f, 0.91264546f, 0.11529088f, 0.33134186f, -0.46221718f }, new Shape(l0.Weights.Height, l0.Weights.Width)).Transposed(); * * var l1 = net.Layer(1) as Dense; * l1.Weights = new Tensor(new[] { 0.08085728f, -0.10262775f, 0.38443696f, -0.23273587f, * 0.33498216f, -0.7566199f , -0.814561f , -0.08565235f, * -0.55490625f, 0.6140275f , 0.34785295f, -0.3431782f, * 0.47427893f, -0.41688982f, 0.59143007f, 0.00616223f, * 0.60304165f, 0.6548513f , -0.78456855f, 0.4640578f }, new Shape(l1.Weights.Height, l1.Weights.Width)).Transposed(); * * var l2 = net.Layer(2) as Dense; * l2.Weights = new Tensor(new[] { 0.32492328f, 0.6930735f, * -0.7263415f , 0.4574399f, * 0.5422747f , 0.19008946f, * 0.911242f , -0.24971604f }, new Shape(l2.Weights.Height, l2.Weights.Width)).Transposed(); * * Trace.WriteLine(net.Predict(inputs.GetBatch(0))); * * //net.Optimize(new SGD(0.01f), Loss.MeanSquareError); * net.Optimize(new Adam(0.01f), Loss.MeanSquareError); * * net.Fit(inputs, outputs, 1, 100, 2, Track.Nothing, false);*/ /*var inShape = new Shape(20); * var outShape = new Shape(20); * * List<Data> trainingData = new List<Data>(); * * for (int i = 0; i < 32; ++i) * { * var input = new Tensor(inShape); * input.FillWithRand(3 * i); * var output = new Tensor(outShape); * output.FillWithRand(3 * i); * trainingData.Add(new Data(input, output)); * } * * var model = new Sequential(); * model.AddLayer(new Flatten(inShape)); * model.AddLayer(new Dense(model.LastLayer, 128, Activation.ReLU)); * model.AddLayer(new Dense(model.LastLayer, 64, Activation.ReLU)); * model.AddLayer(new Dense(model.LastLayer, outShape.Length, Activation.Linear)); * * var net = new NeuralNetwork("simple_net_perf_test"); * net.Model = model; * net.Optimize(new Adam(), Loss.MeanSquareError);*/ var timer = new Stopwatch(); timer.Start(); net.Fit(trainingData, -1, 500, null, 0, Track.Nothing); timer.Stop(); Trace.WriteLine($"{Math.Round(timer.ElapsedMilliseconds / 1000.0, 2)} seconds"); return; }
private void CreateNetworkButton_Click(object sender, RoutedEventArgs e) { //check if learning rate is set double learningRate; if (!Double.TryParse(LearningRateTextBox.Text, out learningRate)) { consoleTextBox.AppendText("Learning rate must be set and must be in a correct format (x,xxx) ! \n"); return; } for (int lay = 1; lay < layerDisplay.Items.Count; lay++) { if ((layerDisplay.Items[lay] as NetworkLayerDescriptor).neurons == 0) { consoleTextBox.AppendText("Network´s layer cant have 0 neurons ! \n"); return; } } neuralNetwork = window.neuralNetwork; SaveWarning saveWarning; //if there allready is a network, ask user to save it if (neuralNetwork != null) { saveWarning = new SaveWarning(); saveWarning.ShowDialog(); if (saveWarning.OverrideNetwork) { List <int> layers = new List <int>(); List <ActivationFunctions> activationFunctions = new List <ActivationFunctions>(); for (int lay = 1; lay < layerDisplay.Items.Count; lay++) { layers.Add((layerDisplay.Items[lay] as NetworkLayerDescriptor).neurons); activationFunctions.Add((ActivationFunctions)Enum.Parse(typeof(ActivationFunctions), (layerDisplay.Items[lay] as NetworkLayerDescriptor).selectedIndex.ToString())); } window.neuralNetwork = new NeuralNetwork(layers.ToArray(), learningRate, activationFunctions); this.neuralNetwork = window.neuralNetwork; consoleTextBox.AppendText("Network succesfully created ! \n"); } else { consoleTextBox.AppendText("Creation canceled \n"); } } //if there is no network, create a new one else { List <int> layers = new List <int>(); List <ActivationFunctions> activationFunctions = new List <ActivationFunctions>(); for (int lay = 1; lay < layerDisplay.Items.Count; lay++) { layers.Add((layerDisplay.Items[lay] as NetworkLayerDescriptor).neurons); activationFunctions.Add((ActivationFunctions)Enum.Parse(typeof(ActivationFunctions), (layerDisplay.Items[lay] as NetworkLayerDescriptor).selectedIndex.ToString())); } window.neuralNetwork = new NeuralNetwork(layers.ToArray(), learningRate, activationFunctions); this.neuralNetwork = window.neuralNetwork; consoleTextBox.AppendText("Network succesfully created ! \n"); } }
private void StartButton_Click(object sender, RoutedEventArgs e) { this.neuralNetwork = window.neuralNetwork; if (selectedTrainingFile == null || selectedTrainingFile == string.Empty) { consoleTextBox.AppendText("No training data selected !\n"); return; } if (window.neuralNetwork == null) { consoleTextBox.AppendText("No network loaded !\n"); return; } double buffer; TimeSpan timespanBuffer; if (errorTargetTextBox.Text == string.Empty && !TimeSpan.TryParse(LearningTimeTextBox.Text, out timespanBuffer) || errorTargetTextBox.Text == string.Empty && TimeSpan.Parse(LearningTimeTextBox.Text) == TimeSpan.Zero || !Double.TryParse(errorTargetTextBox.Text, out buffer) && TimeSpan.Parse(LearningTimeTextBox.Text) == TimeSpan.Zero || !Double.TryParse(errorTargetTextBox.Text, out buffer) && !TimeSpan.TryParse(LearningTimeTextBox.Text, out timespanBuffer)) { consoleTextBox.AppendText("Invalid error target !\n"); return; } List <double> inputValues = new List <double>(); List <double> outputValues = new List <double>(); try { List <string> readData = CSVReader.ReadCSVFile(selectedTrainingFile); for (int line = 0; line < readData.Count; line++) { string[] splitData = readData[line].Split(';'); for (int data = 0; data < splitData.Length; data++) { if (data < neuralNetwork.Layers[0].Neurons.Count) { inputValues.Add(double.Parse(splitData[data])); } else { outputValues.Add(double.Parse(splitData[data])); } } } } catch (Exception exc) { consoleTextBox.AppendText("Invalid training data !\n"); return; } Task networkTrainer; TimeSpan span = TimeSpan.Parse(LearningTimeTextBox.Text); if (errorTargetTextBox.Text == string.Empty && span.TotalMilliseconds >= 0) { networkTrainer = neuralNetwork.TrainAsync(inputValues, outputValues, span); } else if (span.TotalMilliseconds >= 0) { networkTrainer = neuralNetwork.TrainAsync(inputValues, outputValues, double.Parse(errorTargetTextBox.Text), span); } else { networkTrainer = neuralNetwork.TrainAsync(inputValues, outputValues, double.Parse(errorTargetTextBox.Text)); } Task errorDisplayer = Task.Factory.StartNew(async() => { while (networkTrainer.Status == TaskStatus.WaitingToRun || networkTrainer.Status == TaskStatus.WaitingForActivation || networkTrainer.Status == TaskStatus.WaitingForChildrenToComplete || networkTrainer.Status == TaskStatus.Running || networkTrainer.Status == TaskStatus.Created) { if (neuralNetwork.AbsoluteError != 0) { this.Dispatcher.Invoke(() => { consoleTextBox.AppendText("Training... current error: " + neuralNetwork.AbsoluteError.ToString() + " Elapsed: " + neuralNetwork.elapsed.TotalMilliseconds + " milliseconds" + Environment.NewLine); consoleTextBox.ScrollToEnd(); }); } await Task.Delay(300); } this.Dispatcher.Invoke(() => { consoleTextBox.AppendText("Training finished.\n"); }); }); }
//NN copy init, creates neuron connections based on a parent NN (from breeding) public void copyInit(NeuralNetwork n) { net = new NeuralNetwork(n); net.Mutate(); }
static IEnumerator Train(int numGenerations, int modelsPerGen) { Dictionary <Vector2Int, GameTile> gameMap = GameController.instance.gameMap; int inputNodeCount = gameMap.Keys.Count * 3; int outputNodeCount = 1 + gameMap.Keys.Count * 2; int nodeCountPerLayer = inputNodeCount; int[] perceptronArray = new int[HIDDEN_LAYER_COUNT]; for (int i = 0; i < perceptronArray.Length; i++) { // Have the number of perceptrons in each layer equal to number of inputs perceptronArray[i] = nodeCountPerLayer; } if (modelsPerGen % 2 != 0) { // Make sure the number of models per generation is even so we always // have two models to pit against each other Debug.LogWarning("Number of models per gen is odd. Fixing."); modelsPerGen++; } NeuralNetwork nn = new NeuralNetwork(inputNodeCount, outputNodeCount, perceptronArray); float[][][][] genWeights = new float[modelsPerGen][][][]; // Randomize weights for (int m = 0; m < modelsPerGen; m++) { genWeights[m] = new float[HIDDEN_LAYER_COUNT][][]; for (int l = 0; l < HIDDEN_LAYER_COUNT; l++) { genWeights[m][l] = new float[nodeCountPerLayer][]; for (int p = 0; p < nodeCountPerLayer; p++) { int weightCount = nn.GetInputCount(l, p); genWeights[m][l][p] = new float[weightCount]; for (int w = 0; w < weightCount; w++) { genWeights[m][l][p][w] = Random.Range(WEIGHT_INIT_MIN, WEIGHT_INIT_MAX); } } } } // TRAINING PROCESS // We'll be testing two models at a time, pitted against each other. for (int gen = 0; gen < numGenerations; gen++) { // One bool for each model in the current generation, to track whether it won bool[] wins = new bool[modelsPerGen]; for (int model = 0; model < modelsPerGen; model += 2) { SceneManager.UnloadSceneAsync(MAIN_SCENE_INDEX); while (SceneManager.GetSceneByBuildIndex(MAIN_SCENE_INDEX).isLoaded) { yield return(null); } SceneManager.LoadScene(MAIN_SCENE_INDEX, LoadSceneMode.Additive); yield return(null); SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(MAIN_SCENE_INDEX)); if (!GameController.instance.setupHasFinished) { Debug.Log("Waiting for setup"); bool setupFinished = false; GameController.instance.OnSetupComplete += (() => setupFinished = true); while (setupFinished == false) { // TODO timeout yield return(null); } } Debug.Log("Setup finished."); // Wait a frame for scene to load yield return(null); float[] model1LastInput = new float[inputNodeCount]; float[] model2LastInput = new float[inputNodeCount]; // Play through a game for every two models while (GameController.instance.gameEnded == false) { if (TurnHandler.CurrentTurn > MAX_TURN_COUNT) { Debug.Log("Turn limit reached."); // Hit max turn count; end the game GameController.instance.gameEnded = true; break; } // Set weights depending on whose turn it is if (TurnHandler.CurrentTurn % 2 == 1) // Turn is odd (turns start on 1) { // Set NN to have weights for first model nn.SetWeights(genWeights[model]); } else // Turn is even { // Set NN to have weights for second model nn.SetWeights(genWeights[model + 1]); } float[] input = GatherInputs(); float[] output = nn.Calculate(input); while (true) // Repeat until turn end { // Find best starting tile int bestStart = 0; for (int i = 0; i < GameController.WORLD_X * GameController.WORLD_Y; i++) { int y = (i) / GameController.WORLD_X; int x = (i) % GameController.WORLD_X; // If this would not be a valid start tile, make it highly undesirable GameTile tile = GameController.instance.gameMap[new Vector2Int(x, y)]; if (tile.owner != TurnHandler.GetCurrentPlayer() || (!InputHandler.instance.placingArmies && tile.armies == tile.expendedArmies)) { output[i + 1] -= 1000; } if (output[i + 1] > output[bestStart + 1]) { bestStart = i; } } // Find best target tile int offset = 1 + GameController.WORLD_X * GameController.WORLD_Y; int bestTarget = 0; for (int i = 0; i < GameController.WORLD_X * GameController.WORLD_Y; i++) { int y = (i) / GameController.WORLD_X; int x = (i) % GameController.WORLD_X; GameTile tile = GameController.instance.gameMap[new Vector2Int(x, y)]; // Check if this tile is next to the start tile, and if not make it highly undesirable bool nextToStartTile = false; foreach (Vector2Int adjTile in GameController.instance.GetAdjacentTiles(new Vector2Int(x, y))) { if (adjTile.x == bestStart % GameController.WORLD_X && adjTile.y == bestStart / GameController.WORLD_X) { nextToStartTile = true; } } if (!nextToStartTile) { output[i + offset] -= 1000; } if (output[i + offset] > output[bestTarget + offset]) { bestTarget = i; } } if (output[0] > output[bestStart + 1]) { // Agent decided end turn is best move break; } else { // Input move int startY = bestStart / GameController.WORLD_X; int startX = bestStart % GameController.WORLD_X; int targetY = bestTarget / GameController.WORLD_X; int targetX = bestTarget % GameController.WORLD_X; InputHandler.instance.ClearTileSelection(); InputHandler.instance.RegisterClickOnTile(new Vector2Int(startX, startY)); if (DoShowMoves) { yield return(null); } if (!InputHandler.instance.placingArmies) // Only input the second move if we're not placing armies { InputHandler.instance.RegisterClickOnTile(new Vector2Int(targetX, targetY)); if (DoShowMoves) { yield return(null); } } } if (GameController.instance.gameEnded) { break; } input = GatherInputs(); if (!Enumerable.SequenceEqual(input, GatherInputs())) { Debug.Log(input[1]); Debug.Log(GatherInputs()[1]); Debug.LogError("input gathering is not deterministic"); } output = nn.Calculate(input); } InputHandler.instance.OnEndTurnButton(); } // The game is over. // Evaluate which player won: if (ScorePosition(GameController.instance.startingPlayers[0]) > ScorePosition(GameController.instance.startingPlayers[1])) { Debug.Log("Win goes to " + GameController.instance.startingPlayers[0].nationName); wins[model] = true; wins[model + 1] = false; } else if (ScorePosition(GameController.instance.startingPlayers[0]) < ScorePosition(GameController.instance.startingPlayers[1])) { Debug.Log("Win goes to " + GameController.instance.startingPlayers[1].nationName); wins[model] = false; wins[model + 1] = true; } else { Debug.Log("A perfect draw."); wins[model] = false; wins[model + 1] = false; } } // All the models in this generation have been tested. Time to breed. Debug.Log("Breeding winners of generation " + gen); List <float[][][]> breedingPool = new List <float[][][]>(); for (int m = 0; m < modelsPerGen; m++) { if (wins[m] == true) { breedingPool.Add(genWeights[m]); } } if (breedingPool.Count == 0) { breedingPool.Add(genWeights[0]); Debug.LogError("No models in gen " + gen + " survived to breed!"); } for (int m = 0; m < modelsPerGen; m++) { for (int l = 0; l < HIDDEN_LAYER_COUNT; l++) { for (int p = 0; p < nodeCountPerLayer; p++) { for (int w = 0; w < genWeights[m][l][p].Length; w++) { if (Random.value < MUTATION_CHANCE) { genWeights[m][l][p][w] = Random.Range(WEIGHT_INIT_MIN, WEIGHT_INIT_MAX); } else { int i = Random.Range(0, breedingPool.Count); genWeights[m][l][p][w] = breedingPool[i][l][p][w]; } } } } } } }
//NN normal init, creates with random neuron connections public void normalInit() { net = new NeuralNetwork(layers); net.Mutate(); }
public virtual void SetInfo(NeuralNetwork nn, Genome ge) { this.nn = nn; this.ge = ge; }
public void networkScore(NeuralNetwork neuralNetwork, int score) { generations.addGenome(new Genome(neuralNetwork, score)); }
void Awake() { NN = GetComponent <NeuralNetwork>(); }
public void DisplayConnections(int neuronIndex, Layer currentLayer, UI_Network_Layer nextLayer, NeuralNetwork network, float scaleFactor) { Image node = connections[0]; for (int i = connections.Count; i < nextLayer.nodes.Count; i++) { Image newNode = Instantiate(node); newNode.transform.SetParent(this.transform, false); connections.Add(newNode); } // Position Connections for (int i = 0; i < connections.Count; i++) { PositionConnections(connections[i], nextLayer.nodes[i], neuronIndex, i, currentLayer.GetWeights(network), scaleFactor); } }
void Parser() { Console.WriteLine("Command:"); String input = Console.ReadLine(); if (input.ToLower() == "neural network init") // Neural Network { Console.WriteLine("Certain:"); input = Console.ReadLine(); if (input.ToLower() == "yes") // Neural Network { //New Neural Network List <int> NNLayers = new List <int>(); NNLayers.Add(52); NNLayers.Add(52); NNLayers.Add(52); NeuralNetwork NewNeuralNetwork = new NeuralNetwork(NNLayers.ToArray()); NewNeuralNetwork.Save(NewNeuralNetwork); } } else if (input.ToLower() == "test random") // Neural Network { TotalWins = 0; TotalGames = 0; int NumberOfRuns = 20; //Existing Neural Network Test NeuralNetwork neuralNetwork = new NeuralNetwork(); neuralNetwork = neuralNetwork.Load(); Program prog = new Program(); for (int i = 0; i < NumberOfRuns; i++) { List <Card> deck = Deal.CreateDeck(); TotalWins += prog.GameNNTrainToWin(neuralNetwork, deck); TotalGames += 10; } ; Console.WriteLine(""); Console.WriteLine("Total Wins: " + TotalWins + " Total Games: " + TotalGames); neuralNetwork.fitness = (float)(TotalWins / TotalGames); } else if (input.ToLower() == "test neural network" || input.ToLower() == "test nn") // Neural Network { TotalWins = 0; TotalGames = 0; int NumberOfRuns = 20; //Random Neural Network Test List <int> NNLayers = new List <int>(); NNLayers.Add(52); NeuralNetwork NewNeuralNetwork = new NeuralNetwork(NNLayers.ToArray()); Program prog = new Program(); for (int i = 0; i < NumberOfRuns; i++) { List <Card> deck = Deal.CreateDeck(); TotalWins += prog.GameNNTrainToWin(NewNeuralNetwork, deck); TotalGames += 10; } ; Console.WriteLine(""); Console.WriteLine("Total Wins: " + TotalWins + " Total Games: " + TotalGames); } else if (input.ToLower() == "neural network engage" || input.ToLower() == "run nn") // Neural Network { Console.WriteLine("Number of Generations"); input = Console.ReadLine(); int NumberOfGenerations = 0; Console.WriteLine("Number of Runs"); string inputTwo = Console.ReadLine(); int NumberOfRuns = 0; if (int.TryParse(input, out NumberOfGenerations) && int.TryParse(inputTwo, out NumberOfRuns)) { for (int j = 0; j < NumberOfGenerations; j++) { List <List <Card> > decks = new List <List <Card> >(); List <List <Card> > decksTwo = new List <List <Card> >(); //Create Deck List for NN to compete with for (int i = 0; i < NumberOfRuns; i++) { List <Card> deck = Deal.CreateDeckNN(); decks.Add(deck); List <Card> deckCopy = new List <Card>(); foreach (Card card in deck) { deckCopy.Add(card.Clone()); } decksTwo.Add(deckCopy); } TotalWins = 0; TotalGames = 0; //New Neural Network List <int> NNLayers = new List <int>(); NNLayers.Add(52); NNLayers.Add(52); NNLayers.Add(52); NNLayers.Add(52); NeuralNetwork NewNeuralNetwork = new NeuralNetwork(NNLayers.ToArray()); //Existing Neural Network Test NeuralNetwork neuralNetwork = new NeuralNetwork(); neuralNetwork = neuralNetwork.Load(); CompareNN(neuralNetwork, NewNeuralNetwork, NumberOfRuns, decks, decksTwo); if (NewNeuralNetwork.fitness > neuralNetwork.fitness) { Console.WriteLine("Old Fitness: " + neuralNetwork.fitness + " New Fitness: " + NewNeuralNetwork.fitness); Console.WriteLine("Comparing"); NewNeuralNetwork.Save(NewNeuralNetwork); Console.WriteLine("OVERWRITE"); } } } } if (input.ToLower() == "run") { TotalWins = 0; TotalGames = 0; Console.WriteLine("How many runs:"); input = Console.ReadLine(); int NumberOfRuns = 0; if (int.TryParse(input, out NumberOfRuns)) { for (int i = 0; i < NumberOfRuns; i++) { Program prog = new Program(); TotalWins += prog.Game(); TotalGames++; } } else { Console.WriteLine("Invalid input"); } Console.WriteLine(""); Console.WriteLine("Win Rate:" + Math.Round(((TotalWins / TotalGames) * 100), 2).ToString() + "%"); } else if (input.ToLower() == "exit" || input.ToLower() == "quit") { System.Environment.Exit(1); } Parser(); }
public void Create(NeuralNetwork neuralNetwork) { _networks.Add(neuralNetwork.Guid, neuralNetwork); }
public void SetNeuralNetwork(NeuralNetwork nn) { myNN = nn; StartCar(); }
static void Main(string[] args) { NeuralNetwork nn = new NeuralNetwork(6, 6, 1); }
public void TrainNetwork() //When user presses train button, this function will be run and network will be trained and saved in file. { // Read in training values: string path = "Assets/Resources/TrainingValues.txt"; // TestValues: float[][] input = new float[256][]; float[][] output = new float[256][]; //Read the text from directly from the test.txt file StreamReader reader = new StreamReader(path); string line; int inputNr = 0; int outputNr = 0; int lineNr = 0; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("#")) // Don't read comments in the txt file: { continue; } if (lineNr % 2 == 0) { string[] bits = line.Split(' '); float[] array = new float[bits.Length]; for (int i = 0; i < bits.Length; i++) { array[i] = float.Parse(bits[i]); } input[inputNr] = array; inputNr++; } else { string[] bits = line.Split(' '); float[] array = new float[bits.Length]; for (int i = 0; i < bits.Length; i++) { array[i] = float.Parse(bits[i]); } output[outputNr] = array; outputNr++; } lineNr++; } NeuralNetwork net = new NeuralNetwork(new int[] { 10, 25, 25, 4 }); // initialize network for (int i = 0; i < trainGenerations; i++) { for (int j = 0; j < (lineNr / 2); j++) { net.FeedForward(input[j]); net.BackProp(output[j]); } } // Used to see the accuracy of the network: float accuracy = 0; for (int i = 0; i < trainGenerations; i++) { for (int j = 0; j < (lineNr / 2); j++) { float[] outputTest = net.FeedForward(input[j]); for (int k = 0; k < 4; k++) { if (output[j][k] == 1) // Supposed to be 1. { float newValue = outputTest[k] * 2 - 1; // Making value from 0.5-1 to 0-1. accuracy = (accuracy + newValue) / 2; } else // Supposed to be 0. { float newValue = 1 - (outputTest[k] * 2); // Making value from 0.5-0 to 0-1. accuracy = (accuracy + newValue) / 2; } } } } Debug.Log("Accuracy: " + accuracy); // Save the training values: net.SaveBrain(); net.LoadBrain(); }
private async void Button_Click_2(object sender, RoutedEventArgs e) { await Task.Run(() => NeuralNetwork.train(ReportProgress)); }
void ResetGame() { for (int i = 0; i < Matches.Count; i++) { Matches[i].Reset(); } CurrentGeneration++; TimeStart = Time.time; MatchText.text = "Gen #" + CurrentGeneration; Networks.Sort(); for (int i = 0; i < NetworkCount; i++) { if (!LifetimeStats.ContainsKey(Networks[i].Name)) { LifetimeStats.Add(Networks[i].Name, Networks[i].GetFitness()); } else { LifetimeStats[Networks[i].Name] += Networks[i].GetFitness(); } } for (int i = 0; i < NetworkCount / 2; i++) { LifetimeStats.Remove(Networks[i].Name); Networks[i] = new NeuralNetwork(Networks[i + NetworkCount / 2]); Networks[i].Mutate(); Networks[i].Name = "#" + tempValue; tempValue++; LifetimeStats.Add(Networks[i].Name, Networks[i].GetFitness()); Networks[i + NetworkCount / 2] = new NeuralNetwork(Networks[i + NetworkCount / 2]); } for (int i = 0; i < NetworkCount; i++) { Networks[i].SetFitness(0); } string PlayersTextString = "Players:\n"; foreach (string Key in LifetimeStats.Keys) { PlayersTextString += Key + ": " + LifetimeStats[Key] + "\n"; } PlayersText.text = PlayersTextString; int matchId = 0; for (int i = 0; i < NetworkCount - 1; i++) { for (int j = i + 1; j < NetworkCount; j++) { Matches[matchId].NetworkForRobot1 = Networks[i]; Matches[matchId].NetworkForRobot2 = Networks[j]; Matches[matchId].MatchFinished = false; matchId++; } } }
private void Button_Click_1(object sender, RoutedEventArgs e) { testsResult.Text = NeuralNetwork.test(); }
public void setNeuralNet(NeuralNetwork NeuralNetwork) { this.NeuralNetwork = NeuralNetwork; }
void Update() { if (Time.timeScale != TimeRatio) { Time.timeScale = TimeRatio; //timerBack = TimeRatio * timer; } //Changement de generation if (isTraning == false) { // Si c'est la premiere generation, instancie les objets if (generationNumber == 0) { InitCatNeuralNetworks(); CreateObjectBodies(); } else { //Transfer le score de fitness du controleur vers le reseau de neurones for (int i = 0; i < populationSize; i++) { NNController script = objectList[i].GetComponent <NNController>(); float fitness = script.fitness; nets[i].SetFitness(fitness); } //Trie les agents pour ne garder que les plus performants nets.Sort(); nets.Reverse(); //Affiche la moyenne de fitness de la generation fit = 0; for (int i = 0; i < populationSize; i++) { fit += nets[i].GetFitness(); } fit /= populationSize; Debug.Log("Average fitness: " + fit + "Generation: " + generationNumber); //Instancie la liste de la generation suivante List <NeuralNetwork> newNets = new List <NeuralNetwork>(); //Recupere les plus intelligent de nos objets for (int i = 0; i < populationSize * RatioGenie && popAct < populationSize; i++) { NeuralNetwork net = new NeuralNetwork(nets[i]); newNets.Add(net); popAct++; } //Recupere les plus intelligent de nos objets et les fait muter for (int i = 0; i < populationSize * RatioeNorm && popAct < populationSize; i++) { NeuralNetwork net = new NeuralNetwork(nets[i]); net.Mutate(0.25f); newNets.Add(net); popAct++; } //Recupere les plus intelligent de nos objetset les fait plus muter for (int i = 0; i < populationSize * RatioBad && popAct < populationSize; i++) { NeuralNetwork net = new NeuralNetwork(nets[i]); net.Mutate(0.5f); newNets.Add(net); popAct++; } //Recupere les plus intelligent de nos objets et leurs defonce le cerveau for (int i = 0; i < populationSize * RatioNoBrain && popAct < populationSize; i++) { NeuralNetwork net = new NeuralNetwork(nets[i]); net.Mutate(4f); newNets.Add(net); popAct++; } for (int i = 0; popAct < populationSize; i++) { NeuralNetwork net = new NeuralNetwork(nets[i]); net.Mutate(8f); newNets.Add(net); popAct++; } //Changement d'agents entre les deux generation nets = newNets; } //A la fin du decompte du timer, passe a la generation suivante generationNumber++; Invoke("Timer", timerBack); CreateObjectBodies(); isTraning = true; } //------------------FEEDFORWARD //Transfer les informations du NNController vers l'input layer du reseau de neurones for (int i = 0; i < populationSize; i++) { NNController script = objectList[i].GetComponent <NNController>(); float[] result; float vel = script.GetVelocity(); float distForward = script.distForward; float distBackward = script.distBackward; float distLeft = script.distLeft; float distRight = script.distRight; float distDiagLeft = script.distDiagLeft; float distDiagRight = script.distDiagRight; float distBackDiagLeft = script.distBackDiagLeft; float distBackDiagRight = script.distBackDiagRight; float[] tInputs = new float[] { vel, distForward, distBackward, distLeft, distRight, distDiagLeft, distDiagRight, distBackDiagLeft, distBackDiagRight }; result = nets[i].FeedForward(tInputs); script.results = result;//Envoie le resultat au controleur } //Les generation ne meurent pas assez vite bool end = true; for (int i = 0; i < populationSize; i++) { if (objectList[i].GetComponent <NNController>().active) { end = false; } } if (end || Input.GetKeyDown(KeyCode.Space)) { CancelInvoke("Timer"); Timer(); } }
static void Main(string[] args) { Console.WriteLine("\nBegin neural network regression demo\n"); Console.WriteLine("Goal is to predict the sin(x)"); // artificial; in realistic scenarios you'd read from a text file int numItems = 80; Console.WriteLine("\nProgrammatically generating " + numItems + " training data items"); double[][] trainData = new double[numItems][]; Random rnd = new Random(); for (int i = 0; i < numItems; ++i) { double x = 6.4 * rnd.NextDouble(); // [0 to 2PI] double sx = Math.Sin(x); trainData[i] = new double[] { x, sx }; //產生一個周期內的80個sin取樣點 } Console.WriteLine("\nTraining data:\n"); Show.ShowMatrix(trainData, 3, 4, true); //呈現視覺化資料 視覺化.ShowPlot(trainData); //CvInvoke.WaitKey(1000); CvInvoke.WaitKey(); int numInput = 1; // usually more int numHidden = 4; int numOutput = 1; // usual for regression int rndSeed = 0; Console.WriteLine("\nCreating a " + numInput + "-" + numHidden + "-" + numOutput + " regression neural network"); Console.WriteLine("Using tanh hidden layer activation"); NeuralNetwork nn = new NeuralNetwork(numInput, numHidden, numOutput, rndSeed); int maxEpochs = 1000; double learnRate = 0.05; double momentum = 0.001; Console.WriteLine("\nSetting maxEpochs = " + maxEpochs); Console.WriteLine("Setting learnRate = " + learnRate.ToString("F4")); Console.WriteLine("Setting momentum = " + momentum.ToString("F4")); Console.WriteLine("\nStarting training (using stochastic back-propagation)"); double[] weights = nn.Train(trainData, maxEpochs, learnRate, momentum); Console.WriteLine("Finished training"); Console.WriteLine("\nFinal neural network model weights:\n"); Show.ShowVector(weights, 4, 8, true); double[] y = nn.ComputeOutputs(new double[] { Math.PI }); Console.WriteLine("\nActual sin(PI) = 0.0 Predicted = " + y[0].ToString("F6")); y = nn.ComputeOutputs(new double[] { Math.PI / 2 }); Console.WriteLine("\nActual sin(PI / 2) = 1.0 Predicted = " + y[0].ToString("F6")); y = nn.ComputeOutputs(new double[] { 3 * Math.PI / 2.0 }); Console.WriteLine("\nActual sin(3*PI / 2) = -1.0 Predicted = " + y[0].ToString("F6")); y = nn.ComputeOutputs(new double[] { 6 * Math.PI }); Console.WriteLine("\nActual sin(6*PI) = 0.0 Predicted = " + y[0].ToString("F6")); Console.WriteLine("\nEnd demo\n"); Console.ReadLine(); } // Main
// Constructor (makes a random DNA) public void InitCar(int inputNodes, int hiddenNodes, int outputNodes) { neuralNetwork = new NeuralNetwork(inputNodes, hiddenNodes, outputNodes); }
public void SetNetwork(NeuralNetwork net) { this.net = net; }
/** * Does crossover and mutates the generation */ public void crossover() { //Debug.Log("Should be sorted " + fits[0]); for (int l = 0; l < parentsNum; l++) { newNeuralNets[l] = oldNeuralNetworks[l].clone(); } for (int i = parentsNum; i < parentsNum + randomParents; i++) { newNeuralNets[i] = oldNeuralNetworks[Utils.randInt(parentsNum, genomes_per_generation - 1)].clone(); } //newNeuralNets[0].printWeights(); for (int l = parentsNum + randomParents; l < genomes_per_generation; l++) { double[][][] hiddenWeight = new double[neuralNetworkProp.Length - 1][][]; for (int i = 0; i < hiddenWeight.Length; i++) { hiddenWeight[i] = new double[neuralNetworkProp[i] + 1][]; for (int j = 0; j < hiddenWeight[i].Length; j++) { hiddenWeight[i][j] = new double[neuralNetworkProp[i + 1]]; } } //2 4 1 int mixChoice = Utils.randInt(0, parentsNum + randomParents - 1); for (int i = 0; i < hiddenWeight.Length; i++) { Debug.Log("New is " + hiddenWeight[i].Length + " Old " + newNeuralNets[0].hiddenWeight[i].Length); for (int k = 0; k < hiddenWeight[i].Length; k++) { for (int j = 0; j < hiddenWeight[i][k].Length; j++) { if (Utils.randDouble(0, 1) < mutation_probability) { Debug.Log("Entered here"); // hiddenWeight[i][k][j] = randDouble(min_weight, max_weight); // if(randDouble(0,1) < random_mutation_probability) hiddenWeight[i][k][j] = Utils.randDouble(min_weight, max_weight); //hiddenWeight[i][k][j] = 0.5f; // else*/ // hiddenWeight[i][k][j] *= randDouble(minChange, maxChange); } else { if (Utils.randDouble(0, 1) < 0.8f) { hiddenWeight[i][k][j] = newNeuralNets[0].hiddenWeight[i][k][j]; //hiddenWeight[i][k][j] = -0.4d; } else { //hiddenWeight[i][k][j] = 0.4d; hiddenWeight[i][k][j] = newNeuralNets[mixChoice].hiddenWeight[i][k][j]; } } } } } newNeuralNets[l] = new NeuralNetwork(hiddenWeight); } /*for(int i = 0; i < genomes_per_generation; i++){ * NeuralNetwork neuralNet = new NeuralNetwork(neuralNetworkProp); * newNeuralNets[i] = neuralNet; * }*/ }