//LEGACY TESTS: //A simple 1-layer back-propigation test. (Does it improve? (everything approches 1?)) void Simple_Back() { Matrix v1 = new Matrix(1, 1, new double[, ] { { 1 } }); Matrix r1 = new Matrix(1, 3, new double[, ] { { 1, 1, 1 } }); //Matrix fun = new Matrix(3,3, new double[,]{{1,1,1},{2,3,4},{5,6,7}}); //Debug.Log(fun); //r1 = Matrix.Transpose(r1); l1 = new Layer(1, 3, Misc.sigmoidNF()); Matrix a = l1.Forward(v1); Debug.Log(a.ToString()); l1.Backwards(r1); //Debug.Log(r1.ToString()); a = l1.Forward(v1); Debug.Log(a.ToString()); }
void not_testcase(bool prnt = false) { //Matrix TestData = new Matrix(2,2, new double[,] { {1,0}{0,1}}); Matrix v1 = new Matrix(1, 1, new double[, ] { { 1 } }); Matrix v0 = new Matrix(1, 1, new double[, ] { { 0 } }); Matrix a = l2.Forward(l1.Forward(v1)); //Forward if (prnt) { Debug.Log("1: forward." + a.ToString()); } l1.Backwards(l2.Backwards(v0, true)); //Debug.Log("ffs: "+a.ToString()); //l1.Backwards(a,false); a = l2.Forward(l1.Forward(v0)); //Forward if (prnt) { Debug.Log("0: forward." + a.ToString()); } l1.Backwards(l2.Backwards(v1, true)); //} }
void network_train1(bool prnt = false) { for (int i = 0; i < ndset.Rows; i++) { Matrix a = new Matrix(1, 4, new double[, ] { { ndset.Elements[i, 0], ndset.Elements[i, 1], ndset.Elements[i, 2], ndset.Elements[i, 3] } }); //Gets the arguments. Matrix b = net1.Forward(a); if (prnt) { Debug.Log("Training...: Input " + a.ToString() + " Target: " + ndset.Elements[i, 4] + " Output: " + b.ToString()); } net1.Backward(new Matrix(1, 1, new double[, ] { { ndset.Elements[i, 4] } })); } }
void train(Matrix dataset, Layer layer, bool prnt = false) { if (prnt) { Debug.Log("Training sofar:"); } for (int i = 0; i < dataset.Rows; i++) //For each row. { Matrix a = null; if (dataset.Columns == 3) { a = new Matrix(1, 2, new double[, ] { { dataset.Elements[i, 0], dataset.Elements[i, 1] } }); //Gets the arguments. } if (dataset.Columns == 2) { a = new Matrix(1, 2, new double[, ] { { dataset.Elements[i, 0], dataset.Elements[i, 1] } }); //Gets the arguments. } Matrix b = layer.Forward(a); if (prnt) { Debug.Log("Training...: Input " + a.ToString() + " Target: " + dataset.Elements[i, 2] + " Output: " + b.ToString()); } layer.Backwards(new Matrix(1, 1, new double[, ] { { dataset.Elements[i, 2] } })); } }
//this one is a prototype void brain() { NeuralNet.Matrix input = eye.gameObject.GetComponent <Eye_script> ().look(); //Dirty hack to get the input from the eye... isn't great.. //NeuralNet.Matrix input2 = NeuralNet.Matrix.AddBias (NeuralNet.Matrix.AddBias (input)); /* * NeuralNet.Matrix input2 = new NeuralNet.Matrix(1,input.Columns+4); * for (int i = 0; i < input.Columns; i++) { //Only 1 row. * input2.Elements [0,i] = input.Elements[0,i]; * }*/ //input2 [0,0] = 0; //input2 [0,1] = 1; NeuralNet.Matrix brainRet = mind.Forward(input); if (getLastBrainStr) { lastBrainStr = brainRet.ToString(); } //Hardcoding ret to test: //ret[0,0] = input[0,0] / Mathf.PI + 0.5 ; //Abstract the values out of the NN. double rotate_value = ((brainRet[0, 0] - 0.5) * 180.0) / 10.0; double accel_value = (brainRet[0, 1] - 0.5) * 1; //ROTATION// transform.rotation = Quaternion.AngleAxis((float)rotate_value, Vector3.up) * transform.rotation; //VELOCITY// body.velocity = body.velocity * 0.9f; //Dampen vel body.velocity += ( float)accel_value * body.transform.forward; //Velocity = (brainoutput 1*45)*Velocity + accel //Rotates the velocity and applies the body/etc //body.velocity = Quaternion.AngleAxis((float)((ret[0,0]-0.5)*180.0/100), Vector3.up)*body.velocity + (body.transform.forward * (float)ret[0,1] * 0); //The rotation is based off the result. //food -= (float)(ret[0,1]); //Accel costs. //Rotate // transform.rotation = Quaternion.AngleAxis((float)((ret[0,1]-ret[0,2])*10), Vector3.up)*transform.rotation ; //transform.rotation = Quaternion.AngleAxis((float)((input[0,0])), Vector3.up)*transform.rotation ; //TRY IT RAW! //transform.rotation = new Quaternion (((float)(ret [0, 1]+ret [0, 2]-ret [0, 3]-ret [0, 4])*2) , Vector3.up[0],Vector3.up[1],Vector3.up[2]); //transform.rotation = new Quaternion (((float)(ret [0, 1]-ret [0, 2])) , Vector3.up[0],Vector3.up[1],Vector3.up[2]); //Move // if(ret[0,0] > 0) // body.velocity = (float)(ret[0,0]-0.3)*5f*body.transform.forward; //if (body.velocity.magnitude < 0.1) { //food+=2; // train_bad(); //} //food -= (float)(ret[0,0]); /* * if (Random.Range(0, 100) < 10) * { * body.velocity = Quaternion.AngleAxis(Random.Range(-45,45), Vector3.up)*body.velocity; * // body.velocity = new Vector3(0,0,10); * } * //Face velocity dir. * if (body.velocity != new Vector3 (0, 0, 0)) { * transform.rotation = Quaternion.LookRotation (body.velocity); * }//*/ }