-
Notifications
You must be signed in to change notification settings - Fork 220
Added Tensor.dataToString() #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
9e90751
b3a2ab7
e30d9b5
8bf627a
61e7cad
ffdf879
da04cd4
5821e99
6f8dedc
98d320d
921e077
7e56987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.tensorflow.internal.types; | ||
package org.tensorflow; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
|
@@ -11,7 +11,7 @@ | |
/** | ||
* Tensor helper methods. | ||
*/ | ||
public final class Tensors { | ||
final class Tensors { | ||
|
||
/** | ||
* Prevent construction. | ||
|
@@ -30,12 +30,20 @@ public static String toString(Tensor tensor) { | |
} | ||
|
||
/** | ||
* Returns a String representation of a tensor's data. If the output is wider than {@code | ||
* maxWidth} characters, it is truncated and "{@code ...}" is inserted in place of the removed | ||
* data. | ||
* | ||
* @param tensor a tensor | ||
* @param maxWidth the maximum width of the output in characters ({@code null} if unlimited). This | ||
* limit may surpassed if the first or last element are too long. | ||
* @return the String representation of the tensor | ||
*/ | ||
public static String toString(Tensor tensor, Integer maxWidth) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about having that many ways to print a tensors (if we can call I have a new design to propose, let me know what you think. But what about having this class TensorPrinter {
TensorPrinter(Tensor tensor, int maxWidth) { ... }
TensorPrinter withMaxWidth(int maxWidth) {
return new TensorPrinter(this.tensor, maxWidth);
}
String print() {
return .... (this logic here)
}
}
interface Tensor {
String print() {
return new TensorPrinter(this, null).print();
}
TensorPrinter printer() {
return new TensorPrinter(this, null);
}
}
Tensor t = TFloat32.scalarOf(10.0f);
t.print();
t.printer().maxWidth(10).anotherOption(234).print(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer your proposal. It is exactly what I was hoping to implement in the long term but I ended up pushing what you see above as a stepping stone in the right direction. Also, consider the relationship (if any) between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we do that, then I would then expect it to work the same as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I picked the "print" expression in this example just to show the logic but it can for sure be named otherwise to avoid the confusion. Maybe |
||
if (tensor instanceof RawTensor) { | ||
System.out.println("Got rawTensor: " + tensor); | ||
cowwoc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tensor = ((RawTensor) tensor).asTypedTensor(); | ||
} | ||
if (!(tensor instanceof NdArray)) { | ||
throw new AssertionError("Expected tensor to extend NdArray"); | ||
cowwoc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you only accept typed tensors for this method, the endpoint should probably be added to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @karllessard Is there a way for me to handle all tensors (not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @karllessard How do you create a
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't user-side, but all of the |
||
|
@@ -62,7 +70,7 @@ public static String toString(Tensor tensor, Integer maxWidth) { | |
private static String toString(Iterator<? extends NdArray<?>> iterator, Shape shape, | ||
int dimension, Integer maxWidth) { | ||
if (dimension < shape.numDimensions() - 1) { | ||
StringJoiner joiner = new StringJoiner(",\n", indent(dimension) + "[\n", | ||
StringJoiner joiner = new StringJoiner("\n", indent(dimension) + "[\n", | ||
"\n" + indent(dimension) + "]"); | ||
for (long i = 0, size = shape.size(dimension); i < size; ++i) { | ||
String element = toString(iterator, shape, dimension + 1, maxWidth); | ||
|
@@ -92,6 +100,9 @@ private static String toString(Iterator<? extends NdArray<?>> iterator, Shape sh | |
} | ||
|
||
/** | ||
* Truncates the width of a String if it's too long, inserting "{@code ...}" in place of the | ||
* removed data. | ||
* | ||
* @param input the input to truncate | ||
* @param maxWidth the maximum width of the output in characters | ||
* @param lengths the lengths of elements inside input | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the use of a vararg to handle the optional presence of
options
is a good idea. Having a second method accepting no parameter would be better.We use vararg options in the op wrappers because we want to limit the number of methods that ending up in the
*Ops
classes, which is already more than a thousand. But here it's fine "duplicating" it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @karllessard