public void Learn() { List <FloatTensor> rewards_list = new List <FloatTensor>(); List <FloatTensor> losses_list = new List <FloatTensor>(); for (int i = 0; i < history.Count; i++) { if (history[i][1] != null) { rewards_list.Add(history[i][1]); } if (history[i][0] != null) { losses_list.Add(history[i][0]); } } FloatTensor rewards = Functional.Concatenate(this.controller.floatTensorFactory, rewards_list, 0); FloatTensor losses = Functional.Concatenate(this.controller.floatTensorFactory, losses_list, 0); var norm_rewards = rewards.Sub(rewards.Mean()).Div(rewards.Std().Add(0.000001f)); norm_rewards.Autograd = true; var policy_loss = norm_rewards.Mul(losses.Neg()).Sum(0); policy_loss.Backward(); optimizer.Step(rewards.Shape[0], 0); history = new List <FloatTensor[]>(); }
public void Neg() { float[] data1 = { -1, 0, 1, float.MaxValue, float.MinValue }; int[] shape1 = { 5 }; var tensor1 = new FloatTensor(data1, shape1); float[] data2 = { 1, 0, -1, -float.MaxValue, -float.MinValue }; int[] shape2 = { 5 }; var tensorNeg = new FloatTensor(data2, shape2); tensor1.Neg(); for (int i = 0; i < tensor1.Size; i++) { Assert.AreEqual(tensor1.Data[i], tensorNeg.Data[i]); } }
public string processMessage(string json_message) { //Debug.LogFormat("<color=green>SyftController.processMessage {0}</color>", json_message); Command msgObj = JsonUtility.FromJson <Command>(json_message); if (msgObj.functionCall == "createTensor") { FloatTensor tensor = new FloatTensor(msgObj.data, msgObj.shape); tensor.Shader = shader; tensors.Add(tensor.Id, tensor); Debug.LogFormat("<color=magenta>createTensor:</color> {0}", string.Join(", ", tensor.Data)); string id = tensor.Id.ToString(); return(id); } else { if (msgObj.objectType == "tensor") { //Below check needs additions/fix. bool success = true; if (msgObj.objectIndex > FloatTensor.CreatedObjectCount) { return("Invalid objectIndex: " + msgObj.objectIndex); } FloatTensor tensor = tensors[msgObj.objectIndex]; if (msgObj.functionCall == "init_add_matrix_multiply") { FloatTensor tensor_1 = tensors [msgObj.tensorIndexParams [0]]; tensor.ElementwiseMultiplication(tensor_1); } else if (msgObj.functionCall == "inline_elementwise_subtract") { FloatTensor tensor_1 = tensors [msgObj.tensorIndexParams [0]]; tensor.ElementwiseSubtract(tensor_1); } else if (msgObj.functionCall == "multiply_derivative") { FloatTensor tensor_1 = tensors [msgObj.tensorIndexParams [0]]; tensor.MultiplyDerivative(tensor_1); } else if (msgObj.functionCall == "add_matrix_multiply") { FloatTensor tensor_1 = tensors [msgObj.tensorIndexParams [0]]; FloatTensor tensor_2 = tensors [msgObj.tensorIndexParams [1]]; tensor.AddMatrixMultiply(tensor_1, tensor_2); } else if (msgObj.functionCall == "print") { return(tensor.Print()); } else if (msgObj.functionCall == "abs") { // calls the function on our tensor object tensor.Abs(); } else if (msgObj.functionCall == "neg") { tensor.Neg(); } else if (msgObj.functionCall == "add") { FloatTensor tensor_1 = tensors [msgObj.tensorIndexParams [0]]; FloatTensor output = tensor_1.Add(tensor_1); tensors.Add(output.Id, output); string id = output.Id.ToString(); return(id); } else if (msgObj.functionCall == "scalar_multiply") { //get the scalar, cast it and multiply tensor.ScalarMultiplication((float)msgObj.tensorIndexParams[0]); } else { success = false; } if (success) { return(msgObj.functionCall + ": OK"); } } } return("SyftController.processMessage: Command not found."); }