docs.unity3d.com
    Show / Hide Table of Contents

    Create and modify tensors

    Tensor methods in Sentis are similar to methods in frameworks like numpy, TensorFlow, and PyTorch.

    Create a basic tensor

    Create a basic tensor using the methods in the TensorFloat or TensorInt APIs. For example use TensorFloat.Zeros to create a tensor filled with 0.

    Refer to Create input for a model for more information.

    Reshape a tensor

    You can't change the shape of a tensor, but you can use ShallowReshape to create a reshaped copy of a tensor.

    For example:

    // Create a 2 × 2 × 2 tensor
    TensorShape shape = new TensorShape(2, 2, 2);
    int[] data = new int[] { 1, 2, 3, 4, 10, 20, 30, 40 };
    TensorInt tensor = new TensorInt(shape, data);
    
    // Create a copy of the tensor with the new shape 2 × 1 × 4
    TensorShape newShape = new TensorShape(2, 1, 4);
    Tensor reshapedTensor = tensor.ShallowReshape(newShape);
    

    The shape of the tensor and the new shape must have the same number of elements. You can use the length property of a tensor to check the number of elements.

    The result of ShallowReshape is a shallow copy that points to the same memory as the original output.

    Note: Don't use ShallowReshapeif you use BackendType.GPUPixel, because ShallowReshape isn't compatible with the way Sentis stores tensor data in textures.

    Additional resources

    • Tensor fundamentals
    • Do operations on a tensor
    • Model inputs
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023