public void ElementwiseSubtractDataOnDifferent()
        {
            int[] shape1  = { 2, 3 };
            var   tensor1 = new FloatTensor(shape1, true);

            int[] shape2  = { 2, 3 };
            var   tensor2 = new FloatTensor(shape2, false);

            Assert.That(() => tensor1.ElementwiseSubtract(tensor2),
                        Throws.TypeOf <InvalidOperationException>());
        }
        public void ElementwiseSubtractUnequalShapes()
        {
            float[] data1   = { 1, 2, 3, 4, 5, 6 };
            int[]   shape1  = { 2, 3 };
            var     tensor1 = new FloatTensor(data1, shape1);

            float[] data2   = { 1, 2, 3, 4, 5, 6 };
            int[]   shape2  = { 3, 2 };
            var     tensor2 = new FloatTensor(data2, shape2);

            Assert.That(() => tensor1.ElementwiseSubtract(tensor2),
                        Throws.TypeOf <InvalidOperationException>());
        }
        public void ElementwiseSubtract()
        {
            float[] data1   = { float.MinValue, -10, -1.5f, 0, 1.5f, 10, 20, float.MaxValue };
            int[]   shape1  = { 2, 4 };
            var     tensor1 = new FloatTensor(data1, shape1);

            float[] data2   = { float.MaxValue, 10, 1.5f, 0, -1.5f, -10, -20, float.MinValue };
            int[]   shape2  = { 2, 4 };
            var     tensor2 = new FloatTensor(data2, shape2);

            var tensor = new FloatTensor(data1, shape1);

            tensor.ElementwiseSubtract(tensor2);

            for (int i = 0; i < tensor.Size; i++)
            {
                float current = tensor1.Data [i] - tensor2.Data [i];
                Assert.AreEqual(current, tensor.Data [i]);
            }
        }
Esempio n. 4
0
        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.");
        }