Beispiel #1
0
    public static void Main(string [] args)
    {
        var x = new FloatTensor(10);
        var b = new FloatTensor(10);

        b.Fill(30);
        Dump(b);
        x.Random(new RandomGenerator(), 10);
        FloatTensor.Add(x, 100, b);
        Dump(x);
        Dump(b);
#if false
        Dump(x);
        var y = x.Add(100);
        Dump(y);
#endif
        for (int i = 0; i < 1000; i++)
        {
            using (var a = new FloatTensor(1000)){
                using (var c = new FloatTensor(1000)) {
                    var d = a.Add(10);
                    a.CAdd(0, d);
                    d.Dispose();
                }
            }
        }
    }
        public FloatTensor createOnesTensorLike(FloatTensor tensor)
        {
            FloatTensor new_tensor = tensor.Copy();

            new_tensor.Zero_();
            new_tensor.Add((float)1, true);
            return(new_tensor);
        }
Beispiel #3
0
        public override FloatTensor Forward(FloatTensor input)
        {
            FloatTensor unbiased_output = input.MM(_weights);
            FloatTensor output          = unbiased_output.Add(_bias.Expand(unbiased_output.Shape).Contiguous());

            activation = output.Id;

            return(output);
        }
        public void AddUnequalShapes()
        {
            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.Add(tensor2),
                        Throws.TypeOf <InvalidOperationException>());
        }
        public void Add()
        {
            float[] data1   = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[]   shape1  = { 2, 5 };
            var     tensor1 = new FloatTensor(data1, shape1);

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

            var tensorSum = tensor1.Add(tensor2);

            for (int i = 0; i < tensorSum.Size; i++)
            {
                Assert.AreEqual(tensor1.Data [i] + tensor2.Data [i], tensorSum.Data [i]);
            }
        }
Beispiel #6
0
        public void Sigmoid_()
        {
            float[] data1   = { 0.0f };
            int[]   shape1  = { 1 };
            var     tensor1 = new FloatTensor(data1, shape1);

            tensor1.Sigmoid_();
            Assert.AreEqual(tensor1.Data[0], 0.5f);

            float[] data2   = { 0.1f, 0.5f, 1.0f, 2.0f };
            float[] data3   = { -0.1f, -0.5f, -1.0f, -2.0f };
            int[]   shape2  = { 4 };
            var     tensor2 = new FloatTensor(data2, shape2);
            var     tensor3 = new FloatTensor(data3, shape2);

            tensor2.Sigmoid_();
            tensor3.Sigmoid_();
            var sum = tensor2.Add(tensor3);

            for (int i = 0; i < sum.Size; i++)
            {
                Assert.AreEqual(sum.Data[i], 1.0f);
            }
        }
Beispiel #7
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.");
        }