Class Tensor
Syntax
public class Tensor : IDisposable
Constructors
Tensor(ITensorAllocator)
Create an uninitialized Tensor with a shape of [1,1,1,1] and ITensorAllocator a
Declaration
public Tensor(ITensorAllocator a)
Parameters
Tensor(TensorShape, ITensorAllocator)
Create an uninitialized Tensor of shape s
and ITensorAllocator a
.
Declaration
public Tensor(TensorShape s, ITensorAllocator a)
Parameters
Tensor(TensorShape, ITensorData, ITensorAllocator)
Create a Tensor of shape s
, a ITensorData d
and a ITensorAllocator a
Declaration
public Tensor(TensorShape s, ITensorData d, ITensorAllocator a)
Parameters
Tensor(TensorShape, ITensorData, String)
Create a Tensor of shape s
, a ITensorData d
and an optional name n
Declaration
public Tensor(TensorShape s, ITensorData d, string n = "")
Parameters
Tensor(TensorShape, Single[], String)
Create a Tensor of shape s
, an array of data srcData
and an optional name n
srcData
should be of size s.length
.
Declaration
public Tensor(TensorShape s, float[] srcData, string n = "")
Parameters
Tensor(TensorShape, Single[][], String)
Create a Tensor of shape s
, an array of data srcData
and an optional name n
srcData
should be of size s.length
.
Declaration
public Tensor(TensorShape s, float[][] srcData, string n = "")
Parameters
Tensor(TensorShape, String)
Create an uninitialized Tensor of shape s
.
Declaration
public Tensor(TensorShape s, string n = "")
Parameters
Tensor(TensorShape, UnityEngine.ComputeBuffer, String)
Create a Tensor of shape s
, associated ComputeBuffer srcBuffer
filled with tensor values, and an optional name n
srcBuffer
should be larger than s.length
.
Declaration
public Tensor(TensorShape s, UnityEngine.ComputeBuffer srcBuffer, string n = "")
Parameters
Tensor(Int32, Int32, ITensorAllocator)
Create an uninitialized Tensor of shape [b,1,1,ch] and ITensorAllocator a
.
Declaration
public Tensor(int b, int ch, ITensorAllocator a)
Parameters
Tensor(Int32, Int32, ITensorData, ITensorAllocator)
Create a Tensor of shape [b,1,1,ch], a ITensorData d
and a ITensorAllocator a
srcData
should be of size b*ch
Declaration
public Tensor(int b, int ch, ITensorData d, ITensorAllocator a)
Parameters
Tensor(Int32, Int32, ITensorData, String)
Create a Tensor of shape [b,1,1,ch], a ITensorData d
and an optional name n
srcData
should be of size b*ch
Declaration
public Tensor(int b, int ch, ITensorData d, string n = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, ITensorAllocator)
Create an uninitialized Tensor of shape [b,h,w,ch] and ITensorAllocator a
.
Declaration
public Tensor(int b, int h, int w, int ch, ITensorAllocator a)
Parameters
Tensor(Int32, Int32, Int32, Int32, ITensorData, ITensorAllocator)
Create a Tensor of shape [b,h,w,ch], a ITensorData d
and a ITensorAllocator a
Declaration
public Tensor(int b, int h, int w, int ch, ITensorData d, ITensorAllocator a)
Parameters
Tensor(Int32, Int32, Int32, Int32, ITensorData, String)
Create a Tensor of shape [b,h,w,ch], a ITensorData d
and an optional name n
srcData
should be of size bhw*ch
Declaration
public Tensor(int b, int h, int w, int ch, ITensorData d, string n = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, Single[], String)
Create a Tensor of shape [b,h,w,ch], an array of data srcData
and an optional name n
srcData
should be of size bhw*ch
Declaration
public Tensor(int b, int h, int w, int ch, float[] srcData, string n = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, Single[][], String)
Create a Tensor of shape [b,h,w,ch], an array of data srcData
and an optional name n
srcData
should be of size bhw*ch
Declaration
public Tensor(int b, int h, int w, int ch, float[][] srcData, string n = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, String)
Create an uninitialized Tensor of shape [b,h,w,ch].
Declaration
public Tensor(int b, int h, int w, int ch, string n = "")
Parameters
Tensor(Int32, Int32, Int32, Int32, UnityEngine.ComputeBuffer, String)
Create a Tensor of shape [b,h,w,ch], associated ComputeBuffer srcBuffer
filled with tensor values, and an optional name n
srcBuffer
should be larger than bhw*ch
Declaration
public Tensor(int b, int h, int w, int ch, UnityEngine.ComputeBuffer srcBuffer, string n = "")
Parameters
Tensor(Int32, Int32, Single[], String)
Create a Tensor of shape [b,1,1,ch], an array of data srcData
and an optional name n
srcData
should be of size b*ch
Declaration
public Tensor(int b, int ch, float[] srcData, string n = "")
Parameters
Tensor(Int32, Int32, Single[][], String)
Create a Tensor of shape [b,1,1,ch], an array of data srcData
and an optional name n
srcData
should be of size b*ch
Declaration
public Tensor(int b, int ch, float[][] srcData, string n = "")
Parameters
Tensor(Int32, Int32, String)
Create an uninitialized Tensor of shape [b,1,1,ch].
Declaration
public Tensor(int b, int ch, string n = "")
Parameters
Tensor(Int32, Int32, UnityEngine.ComputeBuffer, String)
Create a Tensor of shape [b,1,1,ch], associated ComputeBuffer srcBuffer
filled with tensor values, and an optional name n
srcBuffer
should be larger than b*ch
Declaration
public Tensor(int b, int ch, UnityEngine.ComputeBuffer srcBuffer, string n = "")
Parameters
Type |
Name |
Description |
Int32 |
b |
|
Int32 |
ch |
|
UnityEngine.ComputeBuffer |
srcBuffer |
|
String |
n |
|
Tensor(Int32[], ITensorAllocator)
Create an uninitialized Tensor from a shape s
and ITensorAllocator a
s
should be of size 4, order is [b,h,w,ch].
Declaration
public Tensor(int[] s, ITensorAllocator a)
Parameters
Tensor(Int32[], ITensorData, ITensorAllocator)
Create a Tensor from a shape s
, a ITensorData d
and a ITensorAllocator a
s
should be of size 4, order is [b,h,w,ch].
Declaration
public Tensor(int[] s, ITensorData d, ITensorAllocator a)
Parameters
Tensor(Int32[], ITensorData, String)
Create a Tensor from a shape s
, a ITensorData d
and an optional name n
s
should be of size 4, order is [b,h,w,ch].
Declaration
public Tensor(int[] s, ITensorData d, string n = "")
Parameters
Tensor(Int32[], Single[], String)
Create a Tensor from a shape s
, an array of data srcData
and an optional name n
s
should be of size 4, order is [b,h,w,ch].
srcData
should be of size s[0]s[1]s[2]*s[3].
Declaration
public Tensor(int[] s, float[] srcData, string n = "")
Parameters
Tensor(Int32[], Single[][], String)
Create a Tensor from a shape s
, an array of data srcData
and an optional name n
s
should be of size 4, order is [b,h,w,ch].
srcData
should be of size s[0]s[1]s[2]*s[3].
Declaration
public Tensor(int[] s, float[][] srcData, string n = "")
Parameters
Tensor(Int32[], String)
Create an uninitialized Tensor from a shape s
. s
should be of size 4, order is [b,h,w,ch]
Declaration
public Tensor(int[] s, string n = "")
Parameters
Tensor(Int32[], UnityEngine.ComputeBuffer, String)
Create a Tensor from a shape s
, associated ComputeBuffer srcBuffer
filled with tensor values, and an optional name n
s
should be of size 4, order is [b,h,w,ch].
srcBuffer
should be larger than s[0]s[1]s[2]*s[3].
Declaration
public Tensor(int[] s, UnityEngine.ComputeBuffer srcBuffer, string n = "")
Parameters
Type |
Name |
Description |
Int32[] |
s |
|
UnityEngine.ComputeBuffer |
srcBuffer |
|
String |
n |
|
Tensor(String)
Create an uninitialized Tensor with a shape of [1,1,1,1].
Declaration
public Tensor(string n = "")
Parameters
Type |
Name |
Description |
String |
n |
|
Tensor(UnityEngine.Texture, Int32, String)
Create a Tensor from a texture, shape is [1, texture.height, texture.width, channels=3
]
Declaration
public Tensor(UnityEngine.Texture srcTexture, int channels = 3, string n = "")
Parameters
Type |
Name |
Description |
UnityEngine.Texture |
srcTexture |
|
Int32 |
channels |
|
String |
n |
|
Tensor(UnityEngine.Texture[], Int32, String)
Create a Tensor from multiple texture, shape is [srcTextures.length, texture.height, texture.width, channels=3
]
All textures must be of the same size and dimension.
Declaration
public Tensor(UnityEngine.Texture[] srcTextures, int channels = 3, string n = "")
Parameters
Type |
Name |
Description |
UnityEngine.Texture[] |
srcTextures |
|
Int32 |
channels |
|
String |
n |
|
Fields
name
Declaration
Field Value
shape
Return this tensor shape as [B,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 [B,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 cache is dirty.
Declaration
public float this[int index] { get; set; }
Parameters
Type |
Name |
Description |
Int32 |
index |
|
Property Value
Item[Int32, Int32]
Access element at index [b,0,0,ch] in this Tensor.
This will create a blocking read if cache is dirty.
Declaration
public float this[int b, int ch] { get; set; }
Parameters
Property Value
Item[Int32, Int32, Int32, Int32]
Access element at index [b,h,w,ch] in this Tensor.
This will create a blocking read if cache is dirty.
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
Return the cached linear memory representation of this tensor data.
This will create a blocking read if cache is dirty.
see also readonlyArrayOffset
.
IMPORTANT: This data should not be modified.
Declaration
public float[] readonlyArray { get; }
Property Value
readonlyArrayOffset
Return the offset to use when accessing readonlyArray
Always 0 at the moment.
Declaration
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
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
CastOnDevice(ITensorData)
Cast a tensorData to this tensor, transferring ownership of on tensorData device memory to this tensor.
Declaration
public void CastOnDevice(ITensorData onDevice)
Parameters
DeepCopy()
Create a copy of the current Tensor, actively syncing there data in a blocking way.
Declaration
Returns
Dispose()
Dispose Tensor and associated memories.
Declaration
public virtual void Dispose()
Finalize()
Destructor will also dispose associated memories.
Declaration
protected void Finalize()
Flatten()
Create a flattened copy of the current Tensor ie of shape [B,1,1,HWCH]
Declaration
Returns
FlushCache()
Upload cache to device memory and delete it.
Declaration
Index(Int32, Int32)
Given an element dimensions indices [b,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 [b,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 [b,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
PinToDeviceAndDownloadFromIt(ITensorData)
Allocate tensor on device if needed and download data to cache.
See also PrepareCacheForAccess()
.
Declaration
public void PinToDeviceAndDownloadFromIt(ITensorData onDevice)
Parameters
PinToDeviceAndUploadToIt(ITensorData, Boolean)
Allocate tensor on device if needed and update data.
By default cached copy of the data will be discarded when doing so, set forceInvalidateCache
to false to keep the cache.
Declaration
public void PinToDeviceAndUploadToIt(ITensorData onDevice, bool forceInvalidateCache = true)
Parameters
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)
Create a reshaped copy of the current Tensor.
newShape
.length must be equal to this.shape.length.
Declaration
public Tensor Reshape(TensorShape newShape)
Parameters
Returns
ShallowCopy()
Create a copy of the current Tensor, sharing data storage with original tensor.
Declaration
public Tensor ShallowCopy()
Returns
TakeOwnership()
Remove system reference to this tensor, caller assume ownership.
Declaration
public void TakeOwnership()
ToRenderTexture(Int32, Int32, Single, Single)
Create a new RenderTexture from a slice/batch of a tensor.
Declaration
public UnityEngine.RenderTexture ToRenderTexture(int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F)
Parameters
Returns
Type |
Description |
UnityEngine.RenderTexture |
|
ToRenderTexture(UnityEngine.RenderTexture, Int32, Int32, Single, Single)
Fill a RenderTexture with a slice/batch of a tensor.
Declaration
public void ToRenderTexture(UnityEngine.RenderTexture target, int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F)
Parameters
ToString()
Declaration
public override string ToString()
Returns
Overrides
Unpin(Boolean)
Remove tensor from device, will first sync the cache with device data.
Declaration
public ITensorData Unpin(bool disposeUnpinned = true)
Parameters
Type |
Name |
Description |
Boolean |
disposeUnpinned |
|
Returns
UnpinAndDisposeTensor()
Remove tensor from device, and dispose it.
Declaration
public ITensorData UnpinAndDisposeTensor()
Returns
Extension Methods