Skip to content

Commit 137b0d8

Browse files
committed
Javadoc and rebase
Signed-off-by: Ryan Nett <[email protected]>
1 parent 26672b3 commit 137b0d8

File tree

7 files changed

+101
-159
lines changed

7 files changed

+101
-159
lines changed

ndarray/src/main/java/org/tensorflow/ndarray/index/At.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,4 @@ public String toString() {
7171
.add("keepDim=" + keepDim)
7272
.toString();
7373
}
74-
75-
@Override
76-
public String toString() {
77-
return new StringJoiner(", ", At.class.getSimpleName() + "(", ")")
78-
.add("coord=" + coord)
79-
.add("keepDim=" + keepDim)
80-
.toString();
81-
}
8274
}

ndarray/src/main/java/org/tensorflow/ndarray/index/Ellipsis.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,4 @@ public boolean isEllipsis() {
4545
public String toString() {
4646
return Ellipsis.class.getSimpleName() + "()";
4747
}
48-
49-
@Override
50-
public String toString() {
51-
return "Ellipsis()";
52-
}
5348
}

ndarray/src/main/java/org/tensorflow/ndarray/index/NewAxis.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,4 @@ public boolean isNewAxis() {
5050
public String toString() {
5151
return NewAxis.class.getSimpleName() + "()";
5252
}
53-
54-
@Override
55-
public String toString() {
56-
return NewAxis.class.getSimpleName() + "()";
57-
}
5853
}

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/Ops.java

Lines changed: 43 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.tensorflow.op.core.BatchToSpaceNd;
6161
import org.tensorflow.op.core.Bitcast;
6262
import org.tensorflow.op.core.BooleanMask;
63+
import org.tensorflow.op.core.BooleanMaskUpdate;
6364
import org.tensorflow.op.core.BroadcastDynamicShape;
6465
import org.tensorflow.op.core.BroadcastTo;
6566
import org.tensorflow.op.core.Bucketize;
@@ -95,7 +96,6 @@
9596
import org.tensorflow.op.core.Identity;
9697
import org.tensorflow.op.core.IdentityN;
9798
import org.tensorflow.op.core.ImmutableConst;
98-
import org.tensorflow.op.core.Indexing;
9999
import org.tensorflow.op.core.Init;
100100
import org.tensorflow.op.core.InitializeTable;
101101
import org.tensorflow.op.core.InitializeTableFromTextFile;
@@ -349,10 +349,10 @@ public final class Ops {
349349

350350
public final SignalOps signal;
351351

352-
public final QuantizationOps quantization;
353-
354352
public final TrainOps train;
355353

354+
public final QuantizationOps quantization;
355+
356356
private final Scope scope;
357357

358358
private Ops(Scope scope) {
@@ -374,8 +374,8 @@ private Ops(Scope scope) {
374374
math = new MathOps(this);
375375
audio = new AudioOps(this);
376376
signal = new SignalOps(this);
377-
quantization = new QuantizationOps(this);
378377
train = new TrainOps(this);
378+
quantization = new QuantizationOps(this);
379379
}
380380

381381
/**
@@ -992,9 +992,9 @@ public <U extends TType> Bitcast<U> bitcast(Operand<? extends TType> input, Clas
992992
}
993993

994994
/**
995-
* Apply boolean mask to tensor.
995+
* Apply boolean mask to tensor. Returns the flat array of each element corresponding to a {@code true} in the mask.
996996
* <p>
997-
* Numpy equivalent is `tensor[mask]`.
997+
* Numpy equivalent is {@code tensor[mask]}.
998998
* <p>
999999
* In general, {@code 0 < dim(mask) = K <= dim(tensor)}, and {@code mask}'s shape must match
10001000
* the first K dimensions of {@code tensor}'s shape. We then have:
@@ -1016,6 +1016,36 @@ public <T extends TType> Operand<T> booleanMask(Operand<T> tensor, Operand<TBool
10161016
return BooleanMask.create(scope, tensor, mask, options);
10171017
}
10181018

1019+
/**
1020+
* Updates a tensor at the masked values, and returns the updated tensor. Does not mutate the input tensors. {@code
1021+
* updates} will be broadcasted by default
1022+
* <p>
1023+
* Numpy equivalent is `tensor[mask] = updates`.
1024+
* <p>
1025+
* In general, {@code 0 < dim(mask) = K <= dim(tensor)}, and {@code mask}'s shape must match the first K dimensions of
1026+
* {@code tensor}'s shape. We then have: {@code booleanMask(tensor, mask)[i, j1,...,jd] =
1027+
* tensor[i1,...,iK,j1,...,jd]} where {@code (i1,...,iK)} is the ith {@code true} entry of {@code mask} (row-major
1028+
* order).
1029+
* <p>
1030+
* The {@code axis} could be used with {@code mask} to indicate the axis to mask from (it's 0 by default). In that
1031+
* case, {@code axis + dim(mask) <= dim(tensor)} and {@code mask}'s shape must match the first {@code axis +
1032+
* dim(mask)} dimensions of {@code tensor}'s shape.
1033+
* <p>
1034+
* The shape of {@code updates} should be {@code [n, t_1, t_2, ...]} where {@code n} is the number of true values in
1035+
* {@code mask} and {@code t_i} is the {@code i}th dimension of {@code tensor} after {@code axis} and {@code mask}.
1036+
* {@code updates} will be broadcasted to this shape by default, which can be disabled using {@code options}.
1037+
*
1038+
* @param tensor The tensor to mask.
1039+
* @param mask The mask to apply.
1040+
* @param updates the new values
1041+
* @param options carries optional attributes values
1042+
* @return The masked tensor.
1043+
*/
1044+
public <T extends TType> Operand<T> booleanMaskUpdate(Operand<T> tensor, Operand<TBool> mask,
1045+
Operand<T> updates, BooleanMaskUpdate.Options... options) {
1046+
return BooleanMaskUpdate.create(scope, tensor, mask, updates, options);
1047+
}
1048+
10191049
/**
10201050
* Return the shape of s0 op s1 with broadcast.
10211051
* <p>
@@ -1861,13 +1891,14 @@ public Constant<TInt32> constant(Shape shape, IntDataBuffer data) {
18611891
}
18621892

18631893
/**
1864-
* Creates a scalar of {@code type}, with the value of {@code number}.
1865-
* {@code number} may be truncated if it does not fit in the target type.
1894+
* Creates a scalar of {@code type}, with the value of {@code number}. {@code number} may be truncated if it does not
1895+
* fit in the target type.
18661896
*
18671897
* @param type the type of tensor to create. Must be concrete (i.e. not {@link org.tensorflow.types.family.TFloating})
18681898
* @param number the value of the tensor
18691899
* @return a constant of the passed type
1870-
* @throws IllegalArgumentException if the type is abstract (i.e. {@link org.tensorflow.types.family.TFloating}) or unknown.
1900+
* @throws IllegalArgumentException if the type is abstract (i.e. {@link org.tensorflow.types.family.TFloating}) or
1901+
* unknown.
18711902
*/
18721903
public <T extends TNumber> Constant<T> constant(Class<T> type, Number number) {
18731904
return Constant.tensorOf(scope, type, number);
@@ -1919,14 +1950,14 @@ public <T extends TType> Constant<T> constantOf(T tensor) {
19191950
}
19201951

19211952
/**
1922-
* Creates a scalar of the same type as {@code toMatch}, with the value of {@code number}.
1923-
* {@code number} may be truncated if it does not fit in the target type.
1953+
* Creates a scalar of the same type as {@code toMatch}, with the value of {@code number}. {@code number} may be
1954+
* truncated if it does not fit in the target type.
19241955
*
19251956
* @param toMatch the operand providing the target type
19261957
* @param number the value of the tensor
19271958
* @return a constant with the same type as {@code toMatch}
1928-
* @see Ops#constant(Class, Number)
19291959
* @throws IllegalArgumentException if the type is unknown (which should be impossible).
1960+
* @see Ops#constant(Class, Number)
19301961
*/
19311962
public <T extends TNumber> Constant<T> constantOfSameType(Operand<T> toMatch, Number number) {
19321963
return Constant.tensorOfSameType(scope, toMatch, number);
@@ -5950,61 +5981,6 @@ public <T extends TType> StopGradient<T> stopGradient(Operand<T> input) {
59505981
return StopGradient.create(scope, input);
59515982
}
59525983

5953-
/**
5954-
* Return a strided slice from `input`.
5955-
* <p>
5956-
* The goal of this op is to produce a new tensor with a subset of
5957-
* the elements from the `n` dimensional `input` tensor. The subset is chosen using
5958-
* a sequence of `m` sparse range specifications encoded into the arguments
5959-
* of this function. Note, in some cases
5960-
* `m` could be equal to `n`, but this need not be the case. Each
5961-
* range specification entry can be one of the following:
5962-
* <p>
5963-
* - An ellipsis (...) using {@link Index#ellipses()}. Ellipses are used to imply zero or more
5964-
* dimensions of full-dimension selection and are produced using
5965-
* `ellipsis_mask`. For example, `foo[...]` is the identity slice.
5966-
* <p>
5967-
* - A new axis using {@link Index#newAxis()}. This is used to insert a new shape=1 dimension and is
5968-
* produced using `new_axis_mask`. For example, `foo[:, ...]` where
5969-
* `foo` is shape `(3, 4)` produces a `(1, 3, 4)` tensor.
5970-
* <p>
5971-
* - A range `begin:end:stride` using {@link Index#slice(Singular, Singular, int) Index.slice()} or {@link Index#all()}. This is used to specify how much to choose from
5972-
* a given dimension. `stride` can be any integer but 0. `begin` is an integer
5973-
* which represents the index of the first value to select while `end` represents
5974-
* the index of the last value to select. The number of values selected in each
5975-
* dimension is `end - begin` if `stride > 0` and `begin - end` if `stride < 0`.
5976-
* `begin` and `end` can be negative where `-1` is the last element, `-2` is
5977-
* the second to last. `begin_mask` controls whether to replace the explicitly
5978-
* given `begin` with an implicit effective value of `0` if `stride > 0` and
5979-
* `-1` if `stride < 0`. `end_mask` is analogous but produces the number
5980-
* required to create the largest open interval. For example, given a shape
5981-
* `(3,)` tensor `foo[:]`, the effective `begin` and `end` are `0` and `3`. Do
5982-
* not assume this is equivalent to `foo[0:-1]` which has an effective `begin`
5983-
* and `end` of `0` and `2`. Another example is `foo[-2::-1]` which reverses the
5984-
* first dimension of a tensor while dropping the last two (in the original
5985-
* order elements). For example `foo = [1,2,3,4]; foo[-2::-1]` is `[4,3]`.
5986-
* <p>
5987-
* - A single index using {@link Index#point(int)}. This is used to keep only elements that have a given
5988-
* index. For example (`foo[2, :]` on a shape `(5,6)` tensor produces a
5989-
* shape `(6,)` tensor. This is encoded in `begin` and `end` and
5990-
* `shrink_axis_mask`.
5991-
* <p>
5992-
*
5993-
* <i>Requirements</i>:
5994-
* `0 != strides[i] for i in [0, m)`
5995-
* Only one ellipsis.
5996-
*
5997-
* @param scope current scope
5998-
* @param <T> data type for {@code output()} output
5999-
* @param input
6000-
* @param indices The indices to slice. See {@link Index}.
6001-
* @return a new instance of StridedSlice
6002-
* @see Index
6003-
*/
6004-
public <T extends TType> StridedSlice<T> stridedSlice(Operand<T> input, Index... indices) {
6005-
return Indexing.stridedSlice(scope, input, indices);
6006-
}
6007-
60085984
/**
60095985
* Return a strided slice from `input`.
60105986
* <p>
@@ -6167,29 +6143,6 @@ public <T extends TType, U extends TNumber> StridedSlice<T> stridedSlice(Operand
61676143
return StridedSlice.create(scope, input, begin, end, strides, options);
61686144
}
61696145

6170-
/**
6171-
* Assign `value` to the sliced l-value reference of `ref`.
6172-
* <p>
6173-
* The values of `value` are assigned to the positions in the variable
6174-
* `ref` that are selected by the slice parameters. The slice parameters
6175-
* `begin`, `end`, `strides`, etc. work exactly as in `StridedSlice`.
6176-
* <p>
6177-
* NOTE this op currently does not support broadcasting and so `value`'s
6178-
* shape must be exactly the shape produced by the slice of `ref`.
6179-
*
6180-
* @param <T> data type for {@code outputRef()} output
6181-
* @param scope current scope
6182-
* @param ref the tensor to assign to.
6183-
* @param value the value to assign.
6184-
* @param indices The indices to slice. See {@link Index}.
6185-
* @return a new instance of StridedSliceAssign
6186-
* @see org.tensorflow.op.Ops#stridedSlice(Operand, Index...)
6187-
*/
6188-
public <T extends TType> StridedSliceAssign<T> stridedSliceAssign(Operand<T> ref,
6189-
Operand<T> value, Index... indices) {
6190-
return Indexing.stridedSliceAssign(scope, ref, value, indices);
6191-
}
6192-
61936146
/**
61946147
* Assign `value` to the sliced l-value reference of `ref`.
61956148
* <p>

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/BooleanMask.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.tensorflow.Operand;
2222
import org.tensorflow.ndarray.Shape;
2323
import org.tensorflow.ndarray.index.Indices;
24-
import org.tensorflow.op.Ops;
2524
import org.tensorflow.op.Scope;
2625
import org.tensorflow.op.annotation.Endpoint;
2726
import org.tensorflow.op.annotation.Operator;
@@ -36,7 +35,7 @@ public abstract class BooleanMask {
3635
/**
3736
* Apply boolean mask to tensor. Returns the flat array of each element corresponding to a {@code true} in the mask.
3837
* <p>
39-
* Numpy equivalent is `tensor[mask]`.
38+
* Numpy equivalent is {@code tensor[mask]}.
4039
* <p>
4140
* In general, {@code 0 < dim(mask) = K <= dim(tensor)}, and {@code mask}'s shape must match
4241
* the first K dimensions of {@code tensor}'s shape. We then have:

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/core/BooleanMaskUpdate.java

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.tensorflow.op.core;
1818

1919
import java.util.Arrays;
20-
import java.util.Collections;
2120
import org.tensorflow.Operand;
2221
import org.tensorflow.ndarray.Shape;
2322
import org.tensorflow.ndarray.index.Indices;
@@ -32,50 +31,24 @@
3231
@Operator
3332
public abstract class BooleanMaskUpdate {
3433

35-
/*
36-
Python:
37-
def boolean_mask_update(tensor, mask, update, axis=0, name="boolean_mask_update"):
38-
with tf.name_scope(name):
39-
tensor = tf.convert_to_tensor(tensor, name="tensor")
40-
mask = tf.convert_to_tensor(mask, name="mask")
41-
update = tf.convert_to_tensor(update, name="value")
42-
43-
shape_mask = mask.get_shape()
44-
ndims_mask = shape_mask.ndims
45-
shape_tensor = tensor.get_shape()
46-
if ndims_mask == 0:
47-
raise ValueError("mask cannot be scalar.")
48-
if ndims_mask is None:
49-
raise ValueError(
50-
"Number of mask dimensions must be specified, even if some dimensions"
51-
" are None. E.g. shape=[None] is ok, but shape=None is not.")
52-
axis = 0 if axis is None else axis
53-
axis_value = tf.constant(axis)
54-
if axis_value is not None:
55-
axis = axis_value
56-
shape_tensor[axis:axis + ndims_mask].assert_is_compatible_with(shape_mask)
57-
58-
leading_size = tf.reduce_prod(tf.shape(tensor)[:axis + ndims_mask], [0])
59-
innerShape = tf.shape(tensor)[axis + ndims_mask:]
60-
61-
tensor = tf.reshape(
62-
tensor,
63-
tf.concat([
64-
[leading_size],
65-
innerShape
66-
], 0))
67-
68-
indices = tf.where(mask)
69-
70-
updateShape = tf.concat([tf.shape(indices)[:-1], innerShape], 0)
71-
72-
update = tf.broadcast_to(update, updateShape)
73-
result = tf.tensor_scatter_nd_update(tensor, indices, update)
74-
return tf.reshape(result, shape_tensor)
75-
*/
76-
7734
/**
78-
* TODO
35+
* Updates a tensor at the masked values, and returns the updated tensor. Does not mutate the input tensors. {@code
36+
* updates} will be broadcasted by default
37+
* <p>
38+
* Numpy equivalent is `tensor[mask] = updates`.
39+
* <p>
40+
* In general, {@code 0 < dim(mask) = K <= dim(tensor)}, and {@code mask}'s shape must match the first K dimensions of
41+
* {@code tensor}'s shape. We then have: {@code booleanMask(tensor, mask)[i, j1,...,jd] =
42+
* tensor[i1,...,iK,j1,...,jd]} where {@code (i1,...,iK)} is the ith {@code true} entry of {@code mask} (row-major
43+
* order).
44+
* <p>
45+
* The {@code axis} could be used with {@code mask} to indicate the axis to mask from (it's 0 by default). In that
46+
* case, {@code axis + dim(mask) <= dim(tensor)} and {@code mask}'s shape must match the first {@code axis +
47+
* dim(mask)} dimensions of {@code tensor}'s shape.
48+
* <p>
49+
* The shape of {@code updates} should be {@code [n, t_1, t_2, ...]} where {@code n} is the number of true values in
50+
* {@code mask} and {@code t_i} is the {@code i}th dimension of {@code tensor} after {@code axis} and {@code mask}.
51+
* {@code updates} will be broadcasted to this shape by default, which can be disabled using {@code options}.
7952
*
8053
* @param tensor The tensor to mask.
8154
* @param mask The mask to apply.
@@ -147,8 +120,9 @@ public static <T extends TType> Operand<T> create(Scope scope, Operand<T> tensor
147120

148121
Operand<TInt64> indices = Where.create(scope, mask);
149122

150-
if(broadcast) {
123+
if (broadcast) {
151124
Operand<TInt32> indicesShape = org.tensorflow.op.core.Shape.create(scope, indices);
125+
// this is the number of true values
152126
Operand<TInt32> batchShape = StridedSliceHelper.stridedSlice(scope, indicesShape, Indices.sliceTo(-1));
153127

154128
Operand<TInt32> updateShape = Concat.create(
@@ -180,7 +154,7 @@ public static Options axis(Integer axis) {
180154
/**
181155
* Whether to try broadcasting update. True by default.
182156
*/
183-
public static Options broadcast(Boolean broadcast){
157+
public static Options broadcast(Boolean broadcast) {
184158
return new Options().broadcast(broadcast);
185159
}
186160

0 commit comments

Comments
 (0)