Package deepboof
Class Tensor<T extends Tensor>
java.lang.Object
deepboof.BaseTensor
deepboof.Tensor<T>
- All Implemented Interfaces:
ITensor
- Direct Known Subclasses:
Tensor_F32,Tensor_F64,Tensor_S32,Tensor_S64,Tensor_U8
Base class for Tensors. A tensor is an N-dimensional array. Array elements are stored in a 1-D array
in row-major ordering. This class and its direct children provide only a light weight wrapper around the
array.
Sub-tensors are tensors which are wrapped around an external array which it doesn't own. Most of the time,
sub-tensors will have a non-zero startIndex. They can't be reshaped if the change
in shape requires the data array to grow.
-
Field Summary
FieldsModifier and TypeFieldDescriptionintThe index in the input array that this tensor starts at.int[]Stride for each axisbooleanIf this tensor is wrapped around another array which it doesn't own then it is a sub-tensor.Fields inherited from class deepboof.BaseTensor
shape -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidComputes how many indexes must be steped over to increment a value in each axiscopy()Returns a copy of this tensorvoidcopyShape(int[] shape) Copies the shape of this tensor into the provided int[]abstract Tcreate(int... shape) Creates a tensor of the same type with the specified shapeint[]Creates a new coordinate for this tensor.Creates a tensor of the same type and same shape as this oneabstract ObjectgetData()Returns internal array used to store tensor data.abstract doublegetDouble(int... coordinate) Accessor function which allows any tensor's element to be read as a double.intidx(int axis0) Specialized version ofidx(int...)for 1-D tensors.intidx(int... coordinate) Returns the index of the coordinate.intidx(int axis1, int axis0) Specialized version ofidx(int...)for 2-D tensors.intidx(int axis2, int axis1, int axis0) Specialized version ofidx(int...)for 3-D tensors.intidx(int axis3, int axis2, int axis1, int axis0) Specialized version ofidx(int...)for 4-D tensors.intidx(int axis4, int axis3, int axis2, int axis1, int axis0) Specialized version ofidx(int...)for 5-D tensors.int[]indexToCoor(int index, int[] storage) Converts an array index into a coordinateprotected abstract voidinnerArrayGrow(int N) Re-declare inner array so that it is at least of length Nprotected abstract intLength of inner array as returned by "data.length"booleanisSub()Returns true if it is a sub-tensor.intlength()Length of used elements in Tensor's data array.intlength(int dimension) Returns the length of a dimension/axis.voidreshape()Reshape for when the inner shape variable has already been adjusted.voidreshape(int length0) Reshapefor 1-D tensors.voidreshape(int... shape) Reshape for an arbitrary number of dimensionsvoidreshape(int length1, int length0) Reshapefor 2-D tensors.voidreshape(int length2, int length1, int length0) Reshapefor 3-D tensors.voidreshape(int length3, int length2, int length1, int length0) Reshapefor 4-D tensors.voidreshape(int length4, int length3, int length2, int length1, int length0) Reshapefor 5-D tensors.abstract voidUsed to change the internal array in an abstract wayvoidTurns 'this' tensor into a copy of the provided tensor.intstride(int index) Returns the stride at the specified dimension.subtensor(int startIndex, int[] shape) Creates a subtensor from this tensor.abstract voidzero()Sets all elements in the tensor to the value of zeroMethods inherited from class deepboof.BaseTensor
getDimension, getShape, isShapeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface deepboof.ITensor
getDataType
-
Field Details
-
startIndex
public int startIndexThe index in the input array that this tensor starts at. This allows for a tensor to be inside a much larger array. -
subtensor
public boolean subtensorIf this tensor is wrapped around another array which it doesn't own then it is a sub-tensor. Most of the time, sub-tensors will have a non-zero startIndex. They can't be resized. -
strides
public int[] stridesStride for each axis
-
-
Constructor Details
-
Tensor
public Tensor()
-
-
Method Details
-
getDouble
public abstract double getDouble(int... coordinate) Accessor function which allows any tensor's element to be read as a double.- Parameters:
coordinate- Coordinate of the element which is to be read- Returns:
- Tensor elements's value as a double
-
getData
Returns internal array used to store tensor data. Data is stored in a row-major order in a single array.- Returns:
- tensor data.
-
setData
Used to change the internal array in an abstract way -
reshape
public void reshape(int... shape) Reshape for an arbitrary number of dimensions- Parameters:
shape- New shape. Length must be the same as the number of dimensions. Array reference not saved internally. Highest to lowest dimension
-
reshape
public void reshape(int length0) Reshapefor 1-D tensors. Convenience function, but can be used to avoid declaring a new array- Parameters:
length0- Length of axis-0
-
reshape
public void reshape(int length1, int length0) Reshapefor 2-D tensors. Convenience function, but can be used to avoid declaring a new array- Parameters:
length1- Length of axis-1length0- Length of axis-0
-
reshape
public void reshape(int length2, int length1, int length0) Reshapefor 3-D tensors. Convenience function, but can be used to avoid declaring a new array- Parameters:
length2- Length of axis-2length1- Length of axis-1length0- Length of axis-0
-
reshape
public void reshape(int length3, int length2, int length1, int length0) Reshapefor 4-D tensors. Convenience function, but can be used to avoid declaring a new array- Parameters:
length3- Length of axis-3length2- Length of axis-2length1- Length of axis-1length0- Length of axis-0
-
reshape
public void reshape(int length4, int length3, int length2, int length1, int length0) Reshapefor 5-D tensors. Convenience function, but can be used to avoid declaring a new array- Parameters:
length4- Length of axis-4length3- Length of axis-3length2- Length of axis-2length1- Length of axis-1length0- Length of axis-0
-
reshape
public void reshape()Reshape for when the inner shape variable has already been adjusted. Useful for when calls to new are being minimized. -
innerArrayGrow
protected abstract void innerArrayGrow(int N) Re-declare inner array so that it is at least of length N- Parameters:
N- Desired minimum length of inner array
-
innerArrayLength
protected abstract int innerArrayLength()Length of inner array as returned by "data.length"- Returns:
- Length of inner array
-
idx
public int idx(int... coordinate) Returns the index of the coordinate. Data array is encoded in a row-major format- Parameters:
coordinate- Coordinate from highest to lowest axis number/dimension- Returns:
- index of the index in internal data array
-
idx
public int idx(int axis0) Specialized version ofidx(int...)for 1-D tensors.- Parameters:
axis0- axis-0 of coordinate- Returns:
- index in internal data array
-
idx
public int idx(int axis1, int axis0) Specialized version ofidx(int...)for 2-D tensors.- Parameters:
axis1- axis-1 of coordinateaxis0- axis-0 of coordinate- Returns:
- index in internal data array
-
idx
public int idx(int axis2, int axis1, int axis0) Specialized version ofidx(int...)for 3-D tensors.- Parameters:
axis2- axis-2 of coordinateaxis1- axis-1 of coordinateaxis0- axis-0 of coordinate- Returns:
- index in internal data array
-
idx
public int idx(int axis3, int axis2, int axis1, int axis0) Specialized version ofidx(int...)for 4-D tensors.- Parameters:
axis3- axis-3 of coordinateaxis2- axis-2 of coordinateaxis1- axis-1 of coordinateaxis0- axis-0 of coordinate- Returns:
- index in internal data array
-
idx
public int idx(int axis4, int axis3, int axis2, int axis1, int axis0) Specialized version ofidx(int...)for 5-D tensors.- Parameters:
axis4- axis-4 of coordinateaxis3- axis-3 of coordinateaxis2- axis-2 of coordinateaxis1- axis-1 of coordinateaxis0- axis-0 of coordinate- Returns:
- index in internal data array
-
computeStrides
public void computeStrides()Computes how many indexes must be steped over to increment a value in each axis -
stride
public int stride(int index) Returns the stride at the specified dimension. If negative it will start counting from the tail -
copyShape
public void copyShape(int[] shape) Copies the shape of this tensor into the provided int[]- Parameters:
shape- Where the shape is to be written to. Must be the correct size
-
length
public int length(int dimension) Returns the length of a dimension/axis. If a negative number is passed in it will return the distance relative to the end. E.g. -1 = length-1, -2 = length-2- Specified by:
lengthin interfaceITensor- Overrides:
lengthin classBaseTensor- Parameters:
dimension- The dimension/axis- Returns:
- length
-
length
public int length()Length of used elements in Tensor's data array. Note that the actual data array can be larger- Returns:
- length of used region in data array
-
create
Creates a tensor of the same type with the specified shape- Parameters:
shape- Shape of the new tensor- Returns:
- New tensor with the specified shape
-
createLike
Creates a tensor of the same type and same shape as this one- Returns:
- New tensor
-
createCoor
public int[] createCoor()Creates a new coordinate for this tensor.- Specified by:
createCoorin interfaceITensor- Overrides:
createCoorin classBaseTensor- Returns:
- coordinate/int array of appropriate length
-
indexToCoor
public int[] indexToCoor(int index, int[] storage) Converts an array index into a coordinate- Parameters:
index- internal array index offset from startIndexstorage- (Optional) storage for coordinate. If null a new instance is created- Returns:
- coordinate
-
setTo
Turns 'this' tensor into a copy of the provided tensor.- Parameters:
original- Original tensor that's to be copied into this one. Not modified.
-
zero
public abstract void zero()Sets all elements in the tensor to the value of zero -
copy
Returns a copy of this tensor -
isSub
public boolean isSub()Returns true if it is a sub-tensor.- Returns:
- true if sub-tensor or false if not
-
subtensor
Creates a subtensor from this tensor.- Parameters:
startIndex- The start index in this tensor's internal data arrayshape- Shape of the output tensor- Returns:
- The new sub-tensor.
-