private static void Main(string[] args) { // species a 2-layer neural network with one hidden layer of 20 neurons var net = new Net(); // input layer declares size of input. here: 2-D data // ConvNetJS works on 3-Dimensional volumes (width, height, depth), but if you're not dealing with images // then the first two dimensions (width, height) will always be kept at size 1 net.AddLayer(new InputLayer(1, 1, 2)); // declare 20 neurons, followed by ReLU (rectified linear unit non-linearity) net.AddLayer(new FullyConnLayer(20, Activation.Relu)); // declare the linear classifier on top of the previous hidden layer net.AddLayer(new SoftmaxLayer(10)); // forward a random data point through the network var x = new Volume(new[] {0.3, -0.5}); var prob = net.Forward(x); // prob is a Volume. Volumes have a property Weights that stores the raw data, and WeightGradients that stores gradients Console.WriteLine("probability that x is class 0: " + prob.Weights[0]); // prints e.g. 0.50101 var trainer = new Trainer(net) {LearningRate = 0.01, L2Decay = 0.001}; trainer.Train(x, 0); // train the network, specifying that x is class zero var prob2 = net.Forward(x); Console.WriteLine("probability that x is class 0: " + prob2.Weights[0]); // now prints 0.50374, slightly higher than previous 0.50101: the networks // weights have been adjusted by the Trainer to give a higher probability to // the class we trained the network with (zero) }
private static void Regression1DDemo() { var net = new Net(); net.AddLayer(new InputLayer(1, 1, 1)); net.AddLayer(new FullyConnLayer(20, Activation.Relu)); net.AddLayer(new FullyConnLayer(20, Activation.Sigmoid)); net.AddLayer(new RegressionLayer(1)); var trainer = new Trainer(net) { LearningRate = 0.01, Momentum = 0.0, BatchSize = 1, L2Decay = 0.001 }; // Function we want to learn double[] x = { 0.0, 0.5, 1.0 }; double[] y = { 0.0, 0.1, 0.2 }; var n = x.Length; // Training do { RegressionUpdate(n, x, trainer, y); } while (!Console.KeyAvailable); // Testing var netx = new Volume(1, 1, 1); for (var ix = 0; ix < n; ix++) { netx.Weights = new[] { x[ix] }; var result = net.Forward(netx); } }
private static void Classify2DDemo() { var net = new Net(); net.AddLayer(new InputLayer(1, 1, 2)); net.AddLayer(new FullyConnLayer(6, Activation.Tanh)); net.AddLayer(new FullyConnLayer(2, Activation.Tanh)); net.AddLayer(new SoftmaxLayer(2)); var trainer = new Trainer(net) { LearningRate = 0.01, Momentum = 0.0, BatchSize = 10, L2Decay = 0.001 }; // Data var data = new List<double[]>(); var labels = new List<int>(); data.Add(new[] { -0.4326, 1.1909 }); labels.Add(1); data.Add(new[] { 3.0, 4.0 }); labels.Add(1); data.Add(new[] { 0.1253, -0.0376 }); labels.Add(1); data.Add(new[] { 0.2877, 0.3273 }); labels.Add(1); data.Add(new[] { -1.1465, 0.1746 }); labels.Add(1); data.Add(new[] { 1.8133, 1.0139 }); labels.Add(0); data.Add(new[] { 2.7258, 1.0668 }); labels.Add(0); data.Add(new[] { 1.4117, 0.5593 }); labels.Add(0); data.Add(new[] { 4.1832, 0.3044 }); labels.Add(0); data.Add(new[] { 1.8636, 0.1677 }); labels.Add(0); data.Add(new[] { 0.5, 3.2 }); labels.Add(1); data.Add(new[] { 0.8, 3.2 }); labels.Add(1); data.Add(new[] { 1.0, -2.2 }); labels.Add(1); var n = labels.Count; // Training do { Classify2DUpdate(n, data, trainer, labels); } while (!Console.KeyAvailable); // Testing var netx = new Volume(1, 1, 1); for (var ix = 0; ix < n; ix++) { netx.Weights = data[ix]; var result = net.Forward(netx); var c = net.GetPrediction(); bool accurate = c == labels[ix]; } }
void Update() { for (var i = 0; i < iteration; ++i) { foreach (Vector3 v in data) { trainer.Train(new Volume(new double[] { v.x, v.y }), v.z); } } if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); var pos = ray.origin; pos /= 10.0f; pos.z = 0; data.Add(pos); } if (Input.GetMouseButtonDown(1)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); var pos = ray.origin; pos /= 10.0f; pos.z = 1; data.Add(pos); } for (var i = 0; i < pixels.GetLength(0); ++i) { for (var j = 0; j < pixels.GetLength(1); ++j) { Volume action = net.Forward(new Volume(new double[] { i / 10f, j / 10f })); double output = action.Weights[0]; if (output > 0.5) { pixels [i, j].GetComponent <MeshRenderer> ().material.color = new Color(1, 0, 0); } else { pixels [i, j].GetComponent <MeshRenderer> ().material.color = new Color(0, 0, 1); } } } }
public void learn(float r1, bool food) { // perform an update on Q function if (r0 != -99f) { // learn from this tuple to get a sense of how "surprising" it is to the agent double tderror = learnFromTuple(s0, a0, r0, s1, a1); entropyNum++; /* * * entropyAverage = entropyAverage + (1 / (double)entropyNum) * (entropy - entropyAverage); * * * double entropyDifference = entropy - entropyAverage; * * if (entropyDifference < 0) * entropyDifference = 0; * */ /* * * if( entropyDifference * alpha > 0.5f ) * Debug.Log( entropyDifference * alpha ); * * * * * if (entropyNum >= 500) * { * * entropyAverage = entropyNum = 0; * } * */ double[] state = new double[2]; state[0] = e.getEntropySingle(s0); state[1] = e.getEntropySingle(s1); /* printArrayDouble(s0); * * printArrayDouble(state); * * Debug.Log(s0.Length + " " + state.Length); * */ double resultClass = 1f; //bool sample = true; if (t > 2) { Volume resultClassify = netClassify.Forward(new Volume(state)); resultClass = resultClassify.Weights[0]; //sample = resultClassify.Weights[0] <= resultClassify.Weights[1]; } //Debug.Log(resultClass); // decide if we should keep this experience in the replay //if (food || Random.Range(0f, 1f) < resultClass) //if (entropy > 0.7 || r0 != 0 || t == 0) //if(sample) if (t % experienceAddEvery == 0) { exp[expi] = new Experience(s0, a0, r0, s1, a1); expi += 1; if (expn < experienceSize) { expn += 1; } if (expi >= experienceSize) { expi = 0; } // roll over when we run out } t += 1; //Debug.Log("length: " + exp.Length); // sample some additional experience from replay memory and learn from it int k = 0; while (k < learningStepsPerIteration) { Experience experience = exp[Random.Range(0, expn)]; double[] s = new double[2]; s[0] = e.getEntropySingle(experience.s0); s[1] = e.getEntropySingle(experience.s1); Volume resultClassify = netClassify.Forward(new Volume(s)); double result = 1f; result = resultClassify.Weights[0]; if (Random.Range(0f, 1f) < result) { learnFromTuple(experience.s0, experience.a0, experience.r0, experience.s1, experience.a1); k++; } } } r0 = r1; // store for next update }
double[] forwardQ(double[] s) { Volume result = net.Forward(new Volume(s)); return(result.Weights); }