Interface Tensor
- All Known Implementing Classes:
IndexedTensor
,MappedTensor
,MixedTensor
A tensor consists of a set of dimension names and a set of cells containing scalar values. Each cell is identified by its address, which consists of a set of dimension-label pairs which defines the location of that cell. Both dimensions and labels are string on the form of an identifier or integer.
The size of the set of dimensions of a tensor is called its rank.
In contrast to regular mathematical formulations of tensors, this definition of a tensor allows sparseness as there is no built-in notion of a contiguous space, and even in cases where a space is implied (such as when address labels are integers), there is no requirement that every implied cell has a defined value. Undefined values have no define representation as they are never observed.
Tensors can be read and serialized to and from a string form documented in the toString()
method.
- Author:
- bratseth
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The constant signaling a nonexisting value in operations addressing tensor values by index. -
Method Summary
Modifier and TypeMethodDescriptiondefault Tensor
default Tensor
approxEqual
(Tensor argument) static boolean
approxEquals
(double x, double y) static boolean
approxEquals
(double x, double y, double tolerance) default Tensor
default Tensor
default double
asDouble()
Returns the value of this as a double if it has no dimensions and one valuedefault Tensor
default Tensor
avg()
default Tensor
default Tensor
default Tensor
default Tensor
cellCast
(TensorType.Value valueType) Returns the cell of this in some undefined order.cells()
Returns an immutable map of the cells of this in no particular order.default Tensor
default Tensor
default Tensor
count()
default Tensor
default Tensor
static Tensor
diag
(TensorType type) default Tensor
default Tensor
static boolean
Implement here to make this work across implementations.boolean
Returns whether this tensor and the given tensor is mathematically equal: That they have the same dimension *names* and the same content.default Tensor
default Tensor
static Tensor
from
(double value) Returns a double as a tensor: A dimensionless tensor containing the value as its cellstatic Tensor
from
(TensorType type, String tensorString) Returns a tensor instance containing the given data on the tensor literal form.static Tensor
Returns a tensor instance containing the given data on the tensor literal form.static Tensor
Returns a tensor instance containing the given data on the tensor literal form.static Tensor
double
get
(TensorAddress address) Returns the value of a cell, or 0.0 if this cell does not existgetAsDouble
(TensorAddress address) Returns the value at this address, or null of it does not exist.default Tensor
boolean
has
(TensorAddress address) Returns true if this cell existsint
hashCode()
Returns a hash computed deterministically from the content of this tensordefault boolean
isEmpty()
Returns whether this have any cellsdefault Tensor
join
(Tensor argument, DoubleBinaryOperator combinator) default Tensor
l1Normalize
(String dimension) default Tensor
l2Normalize
(String dimension) default Tensor
default Tensor
largerOrEqual
(Tensor argument) default List<Tensor.Cell>
largest()
Returns the cell(s) of this tensor having the highest valuedefault Tensor
default Tensor
map
(DoubleUnaryOperator mapper) default Tensor
default Tensor
max()
default Tensor
default Tensor
default Tensor
default Tensor
median()
default Tensor
default Tensor
default Tensor
merge
(Tensor argument, DoubleBinaryOperator combinator) default Tensor
min()
default Tensor
default Tensor
default Tensor
default Tensor
modify
(DoubleBinaryOperator op, Map<TensorAddress, Double> cells) Returns a new tensor where existing cells in this tensor have been modified according to the given operation and cells in the given map.default Tensor
default Tensor
default Tensor
default Tensor
prod()
default Tensor
default Tensor
static Tensor
random
(TensorType type) static Tensor
range
(TensorType type) default Tensor
reduce
(Reduce.Aggregator aggregator, String... dimensions) Aggregates cells over a set of dimensions, or over all dimensions if no dimensions are specifieddefault Tensor
reduce
(Reduce.Aggregator aggregator, List<String> dimensions) Aggregates cells over a set of dimensions, or over all dimensions if no dimensions are specifiedremove
(Set<TensorAddress> addresses) Returns a new tensor where existing cells in this tensor have been removed according to the given set of addresses.default Tensor
default Tensor
default long
size()
Returns the number of cells in this, allowing for very large tensors.default int
Returns the size of this as an int or throws an exception if it is too large to fit in an int.default Tensor
default Tensor
smallerOrEqual
(Tensor argument) default List<Tensor.Cell>
smallest()
Returns the cell(s) of this tensor having the lowest valuedefault Tensor
default Tensor
default Tensor
sum()
default Tensor
default Tensor
default String
Returns an abbreviated string representation of this tensor suitable for human-readable messagestoAbbreviatedString
(boolean withType, boolean shortForms) Returns an abbreviated string representation of this tensor suitable for human-readable messagesstatic String
toStandardString
(Tensor tensor, boolean withType, boolean shortForms, long maxCells) Call this from toString in implementations to return this tensor on the tensor literal form.toString()
Returns this tensor on the tensor literal form with type included.toString
(boolean withType, boolean shortForms) Returns this tensor on the tensor literal form.type()
Returns the values of this in some undefined orderstatic String
valueToString
(Tensor tensor, boolean shortForms, long maxCells) withType
(TensorType type) Returns this tensor with the given type if types are compatibledefault Tensor
-
Field Details
-
invalidIndex
static final int invalidIndexThe constant signaling a nonexisting value in operations addressing tensor values by index.- See Also:
-
-
Method Details
-
type
TensorType type() -
isEmpty
default boolean isEmpty()Returns whether this have any cells -
size
default long size()Returns the number of cells in this, allowing for very large tensors. Prefer sizeAsInt in implementations that cannot handle sizes outside the int range. -
sizeAsInt
default int sizeAsInt()Returns the size of this as an int or throws an exception if it is too large to fit in an int. Prefer this over size() with implementations that only handle sizes in the int range.- Throws:
IndexOutOfBoundsException
- if the size is too large to fit in an int
-
get
Returns the value of a cell, or 0.0 if this cell does not exist -
has
Returns true if this cell exists -
getAsDouble
Returns the value at this address, or null of it does not exist. -
cellIterator
Iterator<Tensor.Cell> cellIterator()Returns the cell of this in some undefined order. A cell instance is only valid until next() is called. Call detach() on the cell to obtain a long-lived instance. -
valueIterator
Returns the values of this in some undefined order -
cells
Map<TensorAddress,Double> cells()Returns an immutable map of the cells of this in no particular order. This may be expensive for some implementations - avoid when possible -
asDouble
default double asDouble()Returns the value of this as a double if it has no dimensions and one value- Throws:
IllegalStateException
- if this does not have zero dimensions and one value
-
withType
Returns this tensor with the given type if types are compatible- Throws:
IllegalArgumentException
- if types are not compatible
-
modify
Returns a new tensor where existing cells in this tensor have been modified according to the given operation and cells in the given map. Cells in the map outside existing cells are thus ignored.- Parameters:
op
- the modifying functioncells
- the cells to modify- Returns:
- a new tensor with modified cells
-
remove
Returns a new tensor where existing cells in this tensor have been removed according to the given set of addresses. Only valid for mapped or mixed tensors. For mixed tensors, addresses are assumed to only contain the mapped dimensions, as the entire indexed subspace is removed.- Parameters:
addresses
- list of addresses to remove- Returns:
- a new tensor where cells have been removed
-
map
-
reduce
Aggregates cells over a set of dimensions, or over all dimensions if no dimensions are specified -
reduce
Aggregates cells over a set of dimensions, or over all dimensions if no dimensions are specified -
join
-
merge
-
rename
-
concat
-
concat
-
rename
-
generate
-
cellCast
-
l1Normalize
-
l2Normalize
-
matmul
-
softmax
-
xwPlusB
-
expand
-
argmax
-
argmin
-
diag
-
random
-
range
-
multiply
-
add
-
divide
-
subtract
-
max
-
min
-
atan2
-
pow
-
fmod
-
ldexp
-
larger
-
largerOrEqual
-
smaller
-
smallerOrEqual
-
equal
-
notEqual
-
approxEqual
-
bit
-
hamming
-
avg
-
avg
-
avg
-
count
-
count
-
count
-
max
-
max
-
max
-
median
-
median
-
median
-
min
-
min
-
min
-
prod
-
prod
-
prod
-
sum
-
sum
-
sum
-
largest
Returns the cell(s) of this tensor having the highest value -
smallest
Returns the cell(s) of this tensor having the lowest value -
toString
String toString()Returns this tensor on the tensor literal form with type included. -
toString
Returns this tensor on the tensor literal form.- Parameters:
withType
- whether to prefix the value by the type of thisshortForms
- whether to use short forms where applicable, or always using the verbose form
-
toAbbreviatedString
Returns an abbreviated string representation of this tensor suitable for human-readable messages -
toAbbreviatedString
Returns an abbreviated string representation of this tensor suitable for human-readable messages- Parameters:
withType
- whether to prefix the value by the type of thisshortForms
- whether to use short forms where applicable, or always using the verbose form
-
toStandardString
Call this from toString in implementations to return this tensor on the tensor literal form. (toString cannot be a default method because default methods cannot override super methods).- Parameters:
tensor
- the tensor to return the standard string format ofwithType
- whether the type should be prepended to the contentmaxCells
- the max number of cells to output, after which just , "..." is output to represent the rest of the cells- Returns:
- the tensor on the standard string format
-
valueToString
-
equals
Returns whether this tensor and the given tensor is mathematically equal: That they have the same dimension *names* and the same content. -
hashCode
int hashCode()Returns a hash computed deterministically from the content of this tensor -
equals
Implement here to make this work across implementations. Implementations must override equals and call this because this is an interface and cannot override equals. -
approxEquals
static boolean approxEquals(double x, double y, double tolerance) -
approxEquals
static boolean approxEquals(double x, double y) -
from
Returns a tensor instance containing the given data on the tensor literal form.- Parameters:
type
- the type of the tensor to returntensorString
- the tensor on the standard tensor string format
-
from
Returns a tensor instance containing the given data on the tensor literal form.- Parameters:
tensorType
- the type of the tensor to return, as a string on the tensor type format, given inTensorType.fromSpec(java.lang.String)
tensorString
- the tensor on the standard tensor string format
-
from
Returns a tensor instance containing the given data on the tensor literal form. -
from
Returns a double as a tensor: A dimensionless tensor containing the value as its cell
-