Class Tensor
Syntax
public class Tensor : IDisposable
Constructors
Tensor(Int32, Int32, Int32, Int32, Single[], String)
Create a Tensor of shape [N,H,W,C], an array of data srcData
and an optional debug name
.
srcData
must be of size n*h*w*c
.
Declaration
public Tensor(int n, int h, int w, int c, float[] srcData, string name = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, Single[][], String)
Create a Tensor of shape [N,H,W,C], an array of data srcData
and an optional debug name
.
srcData
must be of size n*h*w*c
.
Declaration
public Tensor(int n, int h, int w, int c, float[][] srcData, string name = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, String)
Create an uninitialized Tensor of shape [N,H,W,C] and an optional debug name
.
Declaration
public Tensor(int n, int h, int w, int c, string name = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, ITensorAllocator)
Create an uninitialized Tensor of shape [N,H,W,C] and an ITensorAllocator allocator
.
Declaration
public Tensor(int n, int h, int w, int c, ITensorAllocator allocator)
Parameters
Tensor(Int32, Int32, Int32, Int32, ITensorData, String)
Create a Tensor of shape [N,H,W,C], an ITensorData data
and an optional debug name
.
srcData
must be of size n*h*w*c
.
Declaration
public Tensor(int n, int h, int w, int c, ITensorData data, string name = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, ITensorData, ITensorAllocator)
Create a Tensor of shape [N,H,W,C], an ITensorData data
and an ITensorAllocator allocator
.
data
must be of size n*h*w*c
.
Declaration
public Tensor(int n, int h, int w, int c, ITensorData data, ITensorAllocator allocator)
Parameters
Tensor(Int32, Int32, Int32, Int32, ComputeBuffer, String)
Create a Tensor of shape [N,H,W,C], associated ComputeBuffer srcBuffer
filled with tensor values, and an optional debug name
.
srcBuffer
must be larger than n*h*w*c
.
Declaration
public Tensor(int n, int h, int w, int c, ComputeBuffer srcBuffer, string name = "")
Parameters
Tensor(Int32, Int32, Single[], String)
Create a Tensor of shape [N,1,1,C], an array of data srcData
and an optional debug name
.
srcData
must be of size n*c
.
Declaration
public Tensor(int n, int c, float[] srcData, string name = "")
Parameters
Tensor(Int32, Int32, Single[][], String)
Create a Tensor of shape [N,1,1,C], an array of data srcData
and an optional debug name
.
srcData
must be of size n*c
.
Declaration
public Tensor(int n, int c, float[][] srcData, string name = "")
Parameters
Tensor(Int32, Int32, String)
Create an uninitialized Tensor of shape [N,1,1,C] and an optional debug name
.
Declaration
public Tensor(int n, int c, string name = "")
Parameters
Tensor(Int32, Int32, ITensorAllocator)
Create an uninitialized Tensor of shape [N,1,1,C] and an ITensorAllocator allocator
.
Declaration
public Tensor(int n, int c, ITensorAllocator allocator)
Parameters
Tensor(Int32, Int32, ITensorData, String)
Create a Tensor of shape [N,1,1,C], an ITensorData data
and an optional debug name
.
srcData
must be of size n*c
.
Declaration
public Tensor(int n, int c, ITensorData data, string name = "")
Parameters
Tensor(Int32, Int32, ITensorData, ITensorAllocator)
Create a Tensor of shape [N,1,1,C], an ITensorData data
and an ITensorAllocator allocator
.
srcData
must be of size n*c
.
Declaration
public Tensor(int n, int c, ITensorData data, ITensorAllocator allocator)
Parameters
Tensor(Int32, Int32, ComputeBuffer, String)
Create a Tensor of shape [N,1,1,C], associated ComputeBuffer srcBuffer
filled with tensor values, and an optional debug name
.
srcBuffer
must be larger than n*c
.
Declaration
public Tensor(int n, int c, ComputeBuffer srcBuffer, string name = "")
Parameters
Tensor(Int32[], Single[], String)
Create a Tensor from a shape
, an array of data srcData
and an optional debug name
.
shape
must be of size 4, the order is [N,H,W,C].
srcData
must be of size s[0]*s[1]*s[2]*s[3]
.
Declaration
public Tensor(int[] shape, float[] srcData, string name = "")
Parameters
Tensor(Int32[], Single[][], String)
Create a Tensor from a shape
, an array of data srcData
and an optional name debug name
.
shape
must be of size 4, the order is [N,H,W,C].
srcData
must be of size s[0]*s[1]*s[2]*s[3]
.
Declaration
public Tensor(int[] shape, float[][] srcData, string name = "")
Parameters
Tensor(Int32[], String)
Create an uninitialized Tensor from a shape
and an optional debug name
.
shape
must be of size 4, the order is [N,H,W,C]
Declaration
public Tensor(int[] shape, string name = "")
Parameters
Tensor(Int32[], ITensorAllocator)
Create an uninitialized Tensor from a shape
and an ITensorAllocator allocator
.
shape
must be of size 4, the order is [N,H,W,C].
Declaration
public Tensor(int[] shape, ITensorAllocator allocator)
Parameters
Tensor(Int32[], ITensorData, String)
Create a Tensor from a shape
, an ITensorData data
and an optional debug name
.
shape
must be of size 4, the order is [N,H,W,C].
Declaration
public Tensor(int[] shape, ITensorData data, string name = "")
Parameters
Tensor(Int32[], ITensorData, ITensorAllocator)
Create a Tensor from a shape
, an ITensorData data
and an ITensorAllocator allocator
.
shape
must be of size 4, the order is [N,H,W,C].
Declaration
public Tensor(int[] shape, ITensorData data, ITensorAllocator allocator)
Parameters
Tensor(Int32[], ComputeBuffer, String)
Create a Tensor from a shape
, associated ComputeBuffer srcBuffer
filled with tensor values, and an optional debug name
.
shape
must be of size 4, the order is [N,H,W,C].
srcBuffer
must be larger than s[0]*s[1]*s[2]*s[3]
.
Declaration
public Tensor(int[] shape, ComputeBuffer srcBuffer, string name = "")
Parameters
Tensor(String)
Create an uninitialized Tensor with a shape of [1,1,1,1] and an optional debug name
.
Declaration
public Tensor(string name = "")
Parameters
Type |
Name |
Description |
String |
name |
|
Tensor(ITensorAllocator)
Create an uninitialized Tensor with a shape of [1,1,1,1] and an ITensorAllocator allocator
.
Declaration
public Tensor(ITensorAllocator allocator)
Parameters
Tensor(TensorShape, Single[], String)
Create a Tensor with specified shape
, an array of data srcData
and an optional debug name
.
srcData
must be of size shape.length
.
Declaration
public Tensor(TensorShape shape, float[] srcData, string name = "")
Parameters
Tensor(TensorShape, Single[][], String)
Create a Tensor with specified shape
, an array of data srcData
and an optional debug name
.
srcData
must be of size shape.length
.
Declaration
public Tensor(TensorShape shape, float[][] srcData, string name = "")
Parameters
Tensor(TensorShape, String)
Create an uninitialized Tensor with specified shape
and an optional debug name
.
Declaration
public Tensor(TensorShape shape, string name = "")
Parameters
Tensor(TensorShape, ITensorAllocator)
Create an uninitialized Tensor with specified shape
and ITensorAllocator allocator
.
Declaration
public Tensor(TensorShape shape, ITensorAllocator allocator)
Parameters
Tensor(TensorShape, ITensorData, String)
Create a Tensor with specified shape
, an ITensorData data
and an optional debug name
.
Declaration
public Tensor(TensorShape shape, ITensorData data, string name = "")
Parameters
Tensor(TensorShape, ITensorData, ITensorAllocator)
Create a Tensor with specified shape
, an ITensorData data
and an ITensorAllocator allocator
Declaration
public Tensor(TensorShape shape, ITensorData data, ITensorAllocator allocator)
Parameters
Tensor(TensorShape, ComputeBuffer, String)
Create a Tensor with specified shape
, associated ComputeBuffer srcBuffer
filled with tensor values, and an optional debug name
.
srcBuffer
must be larger than shape.length
.
Declaration
public Tensor(TensorShape shape, ComputeBuffer srcBuffer, string name = "")
Parameters
Tensor(Texture, Int32, String)
Create a Tensor from a texture, shape is [1, texture.height
, texture.width
, channels
].
If channels
is set to -1 (default value), then number of channels in the new Tensor will match the number of channels in the texture.
Just like Texture2D.GetPixels
when reading from LDR texture (RGBA32, ARGB32, RGB24, Alpha8, RG16, R8, etc) this function will remap pixel values from byte values to the range of [0.0 .. 1.0]. Pixel values from HDR textures (such as ARGBFloat or ARGBHalf) will be left unchanged.
Declaration
public Tensor(Texture srcTexture, int channels = -1, string name = "")
Parameters
Tensor(Texture[], Int32, String)
Create a Tensor from multiple texture, shape is [srcTextures.length
, texture.height
, texture.width
, channels
].
If channels
is set to -1 (default value), then number of channels in the new Tensor will match the number of channels in the texture.
All textures must be of the same size and dimension.
Just like Texture2D.GetPixels
when reading from LDR texture (RGBA32, ARGB32, RGB24, Alpha8, RG16, R8, etc) this function will remap pixel values from byte values to the range of [0.0 .. 1.0]. Pixel values from HDR textures (such as ARGBFloat or ARGBHalf) will be left unchanged.
Declaration
public Tensor(Texture[] srcTextures, int channels = -1, string name = "")
Parameters
Fields
name
Declaration
Field Value
shape
Return this tensor shape as [N,H,W,C].
Declaration
public readonly TensorShape shape
Field Value
Properties
allocator
Return this tensor allocator, see interface ITensorAllocator
.
Declaration
public ITensorAllocator allocator { get; }
Property Value
batch
Return the number of batch.
Declaration
public int batch { get; }
Property Value
channels
Return the number of channels.
Declaration
public int channels { get; }
Property Value
data
Declaration
public ITensorData data { get; }
Property Value
dimensions
Return the count of non-unit dimension of this tensor shape.
For example [N,1,1,C] dimensions is 2.
Declaration
public int dimensions { get; }
Property Value
flatHeight
Return the number of batch.
Declaration
public int flatHeight { get; }
Property Value
flatWidth
Declaration
public int flatWidth { get; }
Property Value
height
Return the spatial height.
Declaration
public int height { get; }
Property Value
Item[Int32]
Access element at offset index
in this Tensor.
This will create a blocking read, if this Tensor is a result of a computation on a different device (GPU).
Declaration
public float this[int index] { get; set; }
Parameters
Type |
Name |
Description |
Int32 |
index |
|
Property Value
Item[Int32, Int32]
Access element at index [N,0,0,C] in this Tensor.
This will create a blocking read, if this Tensor is a result of a computation on a different device (GPU).
Declaration
public float this[int b, int ch] { get; set; }
Parameters
Property Value
Item[Int32, Int32, Int32, Int32]
Access element at index [N,H,W,C] in this Tensor.
This will create a blocking read, if this Tensor is a result of a computation on a different device (GPU).
Declaration
public float this[int b, int h, int w, int ch] { get; set; }
Parameters
Property Value
kernelCount
Kernel dimension ordering is [H,W,C,K] for efficiency purpose.
Return kernel count (aka the number of output channels of the associated operator).
Declaration
public int kernelCount { get; }
Property Value
kernelDepth
Kernel dimension ordering is [H,W,C,K] for efficiency purpose.
Return kernel depth (aka the number of input channels of the associated operator).
Declaration
public int kernelDepth { get; }
Property Value
kernelHeight
Kernel dimension ordering is [H,W,C,K] for efficiency purpose.
Return kernel height.
Declaration
public int kernelHeight { get; }
Property Value
kernelWidth
Kernel dimension ordering is [H,W,C,K] for efficiency purpose.
Return kernel width.
Declaration
public int kernelWidth { get; }
Property Value
length
Return the total number of elements in this tensor.
Declaration
public int length { get; }
Property Value
readonlyArray
Declaration
[Obsolete("Use ToReadOnlyArray instead.", false)]
public float[] readonlyArray { get; }
Property Value
readonlyArrayOffset
Declaration
[Obsolete("Use ToReadOnlyArray instead.", false)]
public int readonlyArrayOffset { get; }
Property Value
tensorOnDevice
Declaration
public ITensorData tensorOnDevice { get; }
Property Value
width
Return the spatial width.
Declaration
public int width { get; }
Property Value
Methods
AttachToDevice(ITensorData)
Associates tensor with the block of data residing on a device.
Tensor values will be downloaded from the source
upon the first access.
source
should contain initialized and valid data representing tensor values.
See also PrepareCacheForAccess()
to schedule download as soon as possible.
Declaration
public void AttachToDevice(ITensorData source)
Parameters
Axis(Int32)
Allow to use negative axis to access tensorShape backward.
axis
should be from -rank to rank (exclusive).
Declaration
public int Axis(int axis)
Parameters
Type |
Name |
Description |
Int32 |
axis |
|
Returns
DeepCopy()
Create a copy of the current Tensor.
Declaration
Returns
DetachFromDevice(Boolean)
Remove tensor from device, will first sync the cache with device data.
Declaration
public ITensorData DetachFromDevice(bool disposeDeviceData = true)
Parameters
Type |
Name |
Description |
Boolean |
disposeDeviceData |
|
Returns
Dispose()
Dispose Tensor and associated memories.
Declaration
public virtual void Dispose()
Implements
Finalize()
Destructor will also dispose associated memories.
Declaration
protected void Finalize()
Flatten(String)
Create a flattened copy of the current Tensor ie of shape [N,1,1,HWC]
Declaration
public Tensor Flatten(string newName = null)
Parameters
Type |
Name |
Description |
String |
newName |
|
Returns
FlushCache()
Upload cache to device memory and delete it.
Declaration
Index(Int32, Int32)
Given an element dimensions indices [N,0,0,C] return this element offset in memory.
Declaration
public int Index(int y, int x)
Parameters
Returns
Index(Int32, Int32, Int32, Int32)
Given an element dimensions indices [N,H,W,C] return this element offset in memory.
Declaration
public int Index(int b, int h, int w, int ch)
Parameters
Returns
IndexWithBroadcast(Int32, Int32, Int32, Int32)
Given an element dimensions indices [N,H,W,C] with broadcast support, return this element offset in memory.
Declaration
public int IndexWithBroadcast(int b, int h, int w, int ch)
Parameters
Returns
IndexWithClamp(Int32, Int32, Int32, Int32)
Given an element dimensions indices [N,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
Declaration
public int IndexWithClamp(int b, int h, int w, int ch)
Parameters
Returns
PrepareCacheForAccess(Boolean)
Populate the cache with on device data.
Blocking read if blocking
is true (default)
Declaration
public bool PrepareCacheForAccess(bool blocking = true)
Parameters
Type |
Name |
Description |
Boolean |
blocking |
|
Returns
Reshape(TensorShape, String)
Create a reshaped copy of the current Tensor.
newShape
.length must be equal to this.shape.length.
Declaration
public Tensor Reshape(TensorShape newShape, string newName = null)
Parameters
Returns
ShallowCopy(String)
Create a copy of the current Tensor, sharing data storage with original tensor.
Declaration
public Tensor ShallowCopy(string newName = null)
Parameters
Type |
Name |
Description |
String |
newName |
|
Returns
TakeOwnership()
Remove system reference to this tensor, caller assume ownership.
Declaration
public void TakeOwnership()
ToReadOnlyArray()
Return the cached linear memory representation of this tensor data.
This will create a blocking read, if this Tensor is a result of a computation on a different device (GPU).
IMPORTANT: Modifying contents of the returned array will have undefined behavior.
Declaration
public float[] ToReadOnlyArray()
Returns
ToRenderTexture(Int32, Int32, Single, Single, Texture3D)
Create new RenderTexture and fill it with a portion of the tensor applying scale
and bias
. Portion of the target is specified by batch
and fromChannel
.
batch
specifies the tensor batch to read values from.
fromChannel
specifies the first tensor channel to start reading values from.
Number of channels in the target
texture specifies how many channels to read from the tensor, starting from index fromChannel
.
Resolution of the target
must match the spatial dimensions of the tensor.
scale
multiplier and bias
addition is applied to the values read from the tensor and clamped to the range from 0.0 to 1.0.
Declaration
public RenderTexture ToRenderTexture(int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F, Texture3D lut = null)
Parameters
Returns
ToRenderTexture(RenderTexture, Int32, Int32, Single, Single, Texture3D)
Fill a target
RenderTexture with a portion of the tensor applying scale
and bias
. Portion of the target is specified by batch
and fromChannel
.
batch
specifies the tensor batch to read values from.
fromChannel
specifies the first tensor channel to start reading values from.
Number of channels in the target
texture specifies how many channels to read from the tensor, starting from index fromChannel
.
Resolution of the target
must match the spatial dimensions of the tensor.
scale
multiplier and bias
addition is applied to the values read from the tensor and, if target
is LDR texture (RGBA32, ARGB32, RGB24, Alpha8, RG16, R8, etc), clamped to the range from 0.0 to 1.0.
Declaration
public void ToRenderTexture(RenderTexture target, int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F, Texture3D lut = null)
Parameters
ToRenderTexture(RenderTexture, Int32, Int32, Vector4, Vector4, Texture3D)
Fill a target
RenderTexture with a portion of the tensor applying scale
and bias
. Portion of the target is specified by batch
and fromChannel
.
batch
specifies the tensor batch to read values from.
fromChannel
specifies the first tensor channel to start reading values from.
Number of channels in the target
texture specifies how many channels to read from the tensor, starting from index fromChannel
.
Resolution of the target
must match the spatial dimensions of the tensor.
scale
multiplier and bias
addition is applied to the values read from the tensor and, if target
is LDR texture (RGBA32, ARGB32, RGB24, Alpha8, RG16, R8, etc), clamped to the range from 0.0 to 1.0.
Declaration
public void ToRenderTexture(RenderTexture target, int batch, int fromChannel, Vector4 scale, Vector4 bias, Texture3D lut = null)
Parameters
ToRenderTexture(RenderTextureFormat, Int32, Int32, Single, Single, Texture3D)
Create new RenderTexture and fill it with a portion of the tensor applying scale
and bias
. Portion of the target is specified by batch
and fromChannel
.
format
specifies the type of the new RenderTexture.
batch
specifies the tensor batch to read values from.
fromChannel
specifies the first tensor channel to start reading values from.
Number of channels in the target
texture specifies how many channels to read from the tensor, starting from index fromChannel
.
scale
multiplier and bias
addition is applied to the values read from the tensor and, if format
is LDR (RGBA32, ARGB32, RGB24, Alpha8, RG16, R8, etc), clamped to the range from 0.0 to 1.0.
Declaration
public RenderTexture ToRenderTexture(RenderTextureFormat format, int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F, Texture3D lut = null)
Parameters
Returns
ToString()
Declaration
public override string ToString()
Returns
Overrides
UnpinAndDisposeTensor()
Declaration
[Obsolete("Use Dispose instead.", false)]
public ITensorData UnpinAndDisposeTensor()
Returns
UploadToDevice(ITensorData, Boolean)
Upload tensor values to the device.
This call associates tensor with the uninitialized block of data residing on a device.
destination
should be allocated on a target device. Previous contents of destination
will be overwritten after this call.
By default local cache will be discarded after this call, set invalidateCacheAfterUpload
to false to keep the cache.
Declaration
public void UploadToDevice(ITensorData destination, bool invalidateCacheAfterUpload = true)
Parameters
Extension Methods