Module org.tensorflow.ndarray
Class SparseNdArray<T,U extends NdArray<T>>
- java.lang.Object
-
- org.tensorflow.ndarray.impl.AbstractNdArray<T,U>
-
- org.tensorflow.ndarray.impl.sparse.AbstractSparseNdArray<T,U>
-
- org.tensorflow.ndarray.impl.sparse.SparseNdArray<T,U>
-
- All Implemented Interfaces:
NdArray<T>,Shaped,SparseNdArray<T,U>
public class SparseNdArray<T,U extends NdArray<T>> extends AbstractSparseNdArray<T,U> implements SparseNdArray<T,U>
sparse array for the any data typeA sparse array has two separate dense arrays: indices, values, and a shape that represents the dense shape.
NOTE: all Sparse Arrays are readonly for the
AbstractSparseNdArray.set(NdArray, long...)andAbstractSparseNdArray.setObject(Object, long...)methodsSparseNdArray<String> st = new SparseNdArray<>( StdArrays.of(new long[][] {{0, 0}, {1, 2}}), NdArrays.vectorOf("first", "second"), Shape.of(3, 4));represents the dense array:
[[true, false, false, false] [false, false, true, false] [false, false, false, false]]
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSparseNdArray(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions)Creates a SparseNdArray
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, DataBuffer<T> dataBuffer, DimensionalSpace dimensions)Creates a new SparseNdArray from a data bufferstatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, DataBuffer<T> buffer, Shape shape)Creates a new empty SparseNdArray from a float data bufferstatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, DataBuffer<T> dataBuffer, T defaultValue, DimensionalSpace dimensions)Creates a new SparseNdArray from a data bufferstatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, DataBuffer<T> buffer, T defaultValue, Shape shape)Creates a new empty SparseNdArray from a float data bufferstatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, DimensionalSpace dimensions)Creates a new empty SparseNdArray from a data bufferstatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, LongNdArray indices, U values, DimensionalSpace dimensions)Creates a new SparseNdArraystatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions)Creates a new SparseNdArraystatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, T defaultValue, DimensionalSpace dimensions)Creates a new empty SparseNdArray from a data bufferstatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, U src)Creates a new SparseNdArray from a NdArraystatic <T,U extends NdArray<T>>
SparseNdArray<T,U>create(Class<T> type, U src, T defaultValue)Creates a new SparseNdArray from a NdArrayUcreateDefaultArray()Creates the NdArray with the default value as a scalarUcreateValues(Shape shape)Creates a NdArray of the specified shapeNdArray<T>fromDense(NdArray<T> src)Populates this sparse array from a dense arrayClass<T>getType()Gets the class type for this sparse arrayNdArray<T>read(DataBuffer<T> dst)Read the content of this N-dimensional array into the destination buffer.Uslice(long position, DimensionalSpace sliceDimensions)UtoDense()Converts the sparse array to a dense arrayStringtoString()A String showing the type, default value, number of elements and the dense shape of this sparse ndarray.NdArray<T>write(DataBuffer<T> src)Write the content of this N-dimensional array from the source buffer.-
Methods inherited from class org.tensorflow.ndarray.impl.sparse.AbstractSparseNdArray
copyTo, elements, equals, get, getDefaultArray, getDefaultValue, getIndices, getIndicesCoordinates, getObject, getValues, hashCode, locateIndex, positionOf, set, setDefaultValue, setIndices, setObject, setValues, slice, slowCopyTo, sortIndicesAndValues, toCoordinates
-
Methods inherited from class org.tensorflow.ndarray.impl.AbstractNdArray
dimensions, scalars, shape, slowEquals, slowHashCode
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.tensorflow.ndarray.NdArray
copyTo, elements, equals, get, getObject, scalars, set, setObject, slice, streamOfObjects
-
Methods inherited from interface org.tensorflow.ndarray.SparseNdArray
getIndices, getValues
-
-
-
-
Constructor Detail
-
SparseNdArray
protected SparseNdArray(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions)
Creates a SparseNdArray- Parameters:
indices- A 2-D LongNdArray of shape[N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example,indices=[[1,3], [2,4]]specifies that the elements with indexes of[1,3]and[2,4]have non-default values.values- A 1-D NdArray of Boolean type and shape[N], which supplies the values for each element in indices. For example, givenindices=[[1,3], [2,4]], the parametervalues=[18, 3.6]specifies that element[1,3]of the sparse NdArray has a value of18, and element[2,4]of the NdArray has a value of3.6.defaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensional space for the dense object represented by this sparse array,
-
-
Method Detail
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, LongNdArray indices, U values, DimensionalSpace dimensions)
Creates a new SparseNdArray- Parameters:
indices- A 2-D LongNdArray of shape[N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example,indices=[[1,3], [2,4]]specifies that the elements with indexes of[1,3]and[2,4]have non-default values.values- A 1-D NdArray of any type and shape[N], which supplies the values for each element in indices. For example, givenindices=[[1,3], [2,4]], the parametervalues=[18, 3.6]specifies that element[1,3]of the sparse NdArray has a value of18, and element[2,4]of the NdArray has a value of3.6.dimensions- the dimensional space for the dense object represented by this sparse array.- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions)
Creates a new SparseNdArray- Parameters:
indices- A 2-D LongNdArray of shape[N, ndims], that specifies the indices of the elements in the sparse array that contain non-default values (elements are zero-indexed). For example,indices=[[1,3], [2,4]]specifies that the elements with indexes of[1,3]and[2,4]have non-default values.values- A 1-D NdArray of any type and shape[N], which supplies the values for each element in indices. For example, givenindices=[[1,3], [2,4]], the parametervalues=[18, 3.6]specifies that element[1,3]of the sparse NdArray has a value of18, and element[2,4]of the NdArray has a value of3.6.defaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensional space for the dense object represented by this sparse array.- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> dataBuffer, DimensionalSpace dimensions)
Creates a new SparseNdArray from a data buffer- Parameters:
dataBuffer- the databuffer containing the dense arraydimensions- the dimensional space for the sparse array- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> dataBuffer, T defaultValue, DimensionalSpace dimensions)
Creates a new SparseNdArray from a data buffer- Parameters:
dataBuffer- the databuffer containing the dense arraydefaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()dimensions- the dimensional space for the sparse array- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DimensionalSpace dimensions)
Creates a new empty SparseNdArray from a data buffer- Parameters:
dimensions- the dimensions array- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, T defaultValue, DimensionalSpace dimensions)
Creates a new empty SparseNdArray from a data buffer- Parameters:
dimensions- the dimensions arraydefaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> buffer, Shape shape)
Creates a new empty SparseNdArray from a float data buffer- Parameters:
buffer- the data buffershape- the shape of the sparse array.- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, DataBuffer<T> buffer, T defaultValue, Shape shape)
Creates a new empty SparseNdArray from a float data buffer- Parameters:
buffer- the data bufferdefaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()shape- the shape of the sparse array.- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, U src)
Creates a new SparseNdArray from a NdArray- Parameters:
src- the NdArray- Returns:
- the new Sparse Array
-
create
public static <T,U extends NdArray<T>> SparseNdArray<T,U> create(Class<T> type, U src, T defaultValue)
Creates a new SparseNdArray from a NdArray- Parameters:
defaultValue- Scalar value to set for indices not specified inAbstractSparseNdArray.getIndices()src- the NdArray- Returns:
- the new Sparse Array
-
createDefaultArray
public U createDefaultArray()
Creates the NdArray with the default value as a scalar- Specified by:
createDefaultArrayin classAbstractSparseNdArray<T,U extends NdArray<T>>- Returns:
- the default NdArray of the default value as a scalar
-
createValues
public U createValues(Shape shape)
Creates a NdArray of the specified shape- Specified by:
createValuesin classAbstractSparseNdArray<T,U extends NdArray<T>>- Parameters:
shape- the shape of the dense array.- Returns:
- a NdArray of the specified shape
-
slice
public U slice(long position, DimensionalSpace sliceDimensions)
-
read
public NdArray<T> read(DataBuffer<T> dst)
Read the content of this N-dimensional array into the destination buffer.The size of the buffer must be equal or greater to the
Shaped.size()of this array, or an exception is thrown. After the copy, content of the buffer and of the array can be altered independently, without affecting each other.- Specified by:
readin interfaceNdArray<T>- Parameters:
dst- the destination buffer- Returns:
- this array
- See Also:
DataBuffer.size()
-
write
public NdArray<T> write(DataBuffer<T> src)
Write the content of this N-dimensional array from the source buffer.The size of the buffer must be equal or greater to the
Shaped.size()of this array, or an exception is thrown. After the copy, content of the buffer and of the array can be altered independently, without affecting each other.- Specified by:
writein interfaceNdArray<T>- Parameters:
src- the source buffer- Returns:
- this array
- See Also:
DataBuffer.size()
-
toDense
public U toDense()
Converts the sparse array to a dense array
-
fromDense
public NdArray<T> fromDense(NdArray<T> src)
Populates this sparse array from a dense array- Parameters:
src- the dense array- Returns:
- this sparse array
-
getType
public Class<T> getType()
Gets the class type for this sparse array- Returns:
- the class type for this sparse array.
-
-