V
- The type of the values which ought to be represented by the Tensor
built by this NdaBuilder
.public final class NdaBuilder<V> extends java.lang.Object implements WithShapeOrScalarOrVectorOnDevice<V>, IterByOrIterFromOrAllTensor<V>, ToForTensor<V>, StepForTensor<V>
Nda
/Tensor
instances.
A simple example would be:
Tensor.of(Double.class)
.withShape( 2, 3, 4 )
.andFill( 5, 3, 5 )
It is also possible to define a range using the API to populate the tensor with values:
Tensor.of(Double.class)
.withShape( 2, 3, 4 )
.andFillFrom( 2 ).to( 9 ).step( 2 )
If one needs a simple scalar then the following shortcut is possible:
Tensor.of(Float.class).scalar( 3f )
This principle works for vectors as well:
Tensor.of(Byte.class).vector( 2, 5, 6, 7, 8 )
For more fine-grained control over the initialization one can
pass an initialization lambda to the API:
Tensor.of(Byte.class).withShape(2, 3).andWhere( (i, indices) -> i * 5 - 30 )
Constructor and Description |
---|
NdaBuilder(java.lang.Class<V> typeClass) |
Modifier and Type | Method and Description |
---|---|
Tensor<V> |
all(V value)
This method creates and return a
Tensor instance which
will be homogeneously filled by the the provided value irrespective
of the previously defined shape. |
Tensor<V> |
andFill(V... values)
Provide an array of values which will be used to fill
the
Tensor instance returned by this last fluent builder method. |
ToForTensor<V> |
andFillFrom(V index)
This part of the builder API allows for specifying a range which starts from the
provided value and will end at the value specified in the next
builder step returned by this method.
|
Tensor<V> |
andSeed(java.lang.Object seed)
This method creates and return a
Tensor instance which
will be filled based on the provided seed object. |
Tensor<V> |
andWhere(Filler<V> filler)
|
WithShapeOrScalarOrVectorTensor<V> |
on(Device<? super V> device)
Use this to specify the type onto which the tensor should be stored.
|
Tensor<V> |
scalar(V value)
This method created and return a scalar
Tensor instance
which wraps the provided value. |
Tensor<V> |
step(double size)
This is the last step in the call transition graph of the fluent builder API when
building a
Tensor instance populated based on the values within a defined range. |
StepForTensor<V> |
to(V index)
This step in the call transition graph of the fluent builder API is a followup call
from the
IterByOrIterFromOrAll.andFillFrom(Object) method which
expects a range to be specified whose values will be used to populate the Tensor instance. |
Tensor<V> |
vector(java.lang.Object[] values)
This method creates and returns a vector
Tensor instance
which wraps the provided values. |
IterByOrIterFromOrAllTensor<V> |
withShape(int... shape)
Define a tensor shape by passing an array of int values to this method,
which represent the shape of the
Tensor that should be built. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
vector, vector, withShape
andFill
public NdaBuilder(java.lang.Class<V> typeClass)
typeClass
- The type of the values which ought to be represented by the Tensor
built by this NdaBuilder
.@SafeVarargs public final Tensor<V> andFill(V... values)
IterByOrIterFromOrAllTensor
Tensor
instance returned by this last fluent builder method.
If the configured tensor is larger than the number of provided
elements, then they will simply be read multiple times until
the result has been sufficiently populated.andFill
in interface IterByOrIterFromOrAll<V>
andFill
in interface IterByOrIterFromOrAllTensor<V>
values
- The values which will recurrently populate the returned Tensor
with values until it is filled.Tensor
instance populated by the array of values supplied to this method.public Tensor<V> andWhere(Filler<V> filler)
Filler
lambda which will be
used to populate the Tensor
instance produced by this API with values.andWhere
in interface IterByOrIterFromOrAll<V>
andWhere
in interface IterByOrIterFromOrAllTensor<V>
filler
- The Filler
which ought to populate the returned Tensor
.Tensor
instance populated by the lambda supplied to this method.public ToForTensor<V> andFillFrom(V index)
IterByOrIterFromOrAllTensor
Tensor
instance built by this API, then the resulting
range will fill the underlying data array of the tensor recurrently.andFillFrom
in interface IterByOrIterFromOrAll<V>
andFillFrom
in interface IterByOrIterFromOrAllTensor<V>
index
- The start of the range which ought to supply the Tensor
instance built by this API.public Tensor<V> all(V value)
IterByOrIterFromOrAllTensor
Tensor
instance which
will be homogeneously filled by the the provided value irrespective
of the previously defined shape.all
in interface IterByOrIterFromOrAll<V>
all
in interface IterByOrIterFromOrAllTensor<V>
value
- The value which ought to populate the entire Tensor
.Tensor
instance.public Tensor<V> andSeed(java.lang.Object seed)
IterByOrIterFromOrAllTensor
Tensor
instance which
will be filled based on the provided seed object.
The seed can be any object whose hash will serve as a basis for
supplying the tensor with random data...andSeed
in interface IterByOrIterFromOrAll<V>
andSeed
in interface IterByOrIterFromOrAllTensor<V>
seed
- The seed based on which the value for populating the entire Tensor
will be generated.Tensor
instance.public IterByOrIterFromOrAllTensor<V> withShape(int... shape)
WithShapeOrScalarOrVectorTensor
Tensor
that should be built.
This should be called immediately after having specified the type of the tensor.withShape
in interface WithShapeOrScalarOrVector<V>
withShape
in interface WithShapeOrScalarOrVectorTensor<V>
shape
- The shape array of the Tensor
that should be built.public Tensor<V> vector(java.lang.Object[] values)
WithShapeOrScalarOrVectorTensor
Tensor
instance
which wraps the provided values.vector
in interface WithShapeOrScalarOrVector<V>
vector
in interface WithShapeOrScalarOrVectorTensor<V>
values
- The values which ought to be wrapped by a new vector Tensor
instance.Tensor
instance wrapping the provided values.public Tensor<V> scalar(V value)
WithShapeOrScalarOrVectorTensor
Tensor
instance
which wraps the provided value.scalar
in interface WithShapeOrScalarOrVector<V>
scalar
in interface WithShapeOrScalarOrVectorTensor<V>
value
- The value which ought to be wrapped by a new scalar Tensor
instance.Tensor
instance wrapping the provided value.public StepForTensor<V> to(V index)
ToForTensor
IterByOrIterFromOrAll.andFillFrom(Object)
method which
expects a range to be specified whose values will be used to populate the Tensor
instance.
This method allows one to set the end point of the range whose start has previously between set
in the IterByOrIterFromOrAll.andFillFrom(Object)
method...to
in interface To<V>
to
in interface ToForTensor<V>
index
- The end point of the range previously specified in IterByOrIterFromOrAll.andFillFrom(Object)
.Tensor
instances.public Tensor<V> step(double size)
StepForTensor
Tensor
instance populated based on the values within a defined range.
This method embodies the last part of this range definition which consists of the chained
methods IterByOrIterFromOrAll.andFillFrom(Object)
, To.to(Object)
and lastly this very method Step.step(double)
.
This method allows one to set the step size used to space the entries within
the previously defined span between what has
been passed to IterByOrIterFromOrAll.andFillFrom(Object)
and what has been passed to To.to(Object)
...public WithShapeOrScalarOrVectorTensor<V> on(Device<? super V> device)
WithShapeOrScalarOrVectorOnDevice
on
in interface WithShapeOrScalarOrVectorOnDevice<V>
device
- The Device
which should host the tensor built by this builder.