/// <summary> /// Sample 1D neural network from xMin to xMax building array of 2D points. /// </summary> /// <param name="numSamples"></param> /// <param name="nn"></param> /// <param name="xMin"></param> /// <param name="xMax"></param> /// <returns></returns> public static float[][] SampleNeuralNetwork(int numSamples, NeuralNetwork nn, float xMin, float xMax) { NeuralNetworkProgram nnp = new NeuralNetworkProgram(nn); nnp.context.Reset(true); float[][] res = new float[numSamples][]; for (int i = 0; i < numSamples; i++) { float sx = (i / (float)(numSamples - 1)) * (xMax - xMin) + xMin; nnp.context.inputData[0] = sx; nnp.Execute(); res[i] = new float[] { sx, nnp.context.outputData[0] }; } return(res); }
private void evolverThread(int id) { NeuralNetworkProgram subject = subjects[id]; while (running) { float loss = -1.0f; if (subject.state == -1) { bool reset = true; if (onStreamNextData != null) { readyNextData[id] = true; if (id == 0) { while (true) { bool ready = true; for (int i = 0; i < numThreads; i++) { ready &= readyNextData[i]; } if (ready) { break; } Thread.Sleep(1); } reset = onStreamNextData(ref inputData, ref targetData); for (int i = 0; i < numThreads; i++) { readyNextData[i] = false; } } else { while (readyNextData[id]) { Thread.Sleep(1); } } } if (reset) { if (subject.total > 0) { loss = subject.loss; if (lossType == NeuralNetworkTrainer.LOSS_TYPE_AVERAGE) { loss /= (float)subject.total; } } subject.loss = 0.0f; } } if (loss > -1.0f) { subject.neuralNetwork = NextGeneration(subject.neuralNetwork, loss); //reset state subject.context.Reset(false); if (loss <= desiredLoss) { //hit performance goal, done! if (onReachedGoal != null) { onReachedGoal(); } //clean up running = false; return; } } inputOutputLossFunction(subject); subject.Execute(); if (evolverDelay != 0) { Thread.Sleep(evolverDelay); } } }