public void Abs() { 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 tensorAbs = new FloatTensor(data2, shape2); tensor1.Abs(); for (int i = 0; i < tensor1.Size; i++) { Assert.AreEqual(tensor1.Data[i], tensorAbs.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."); }