-
Notifications
You must be signed in to change notification settings - Fork 167
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
Add numpy-style ToString method #572
Conversation
…a. TODO: Add more tests
src/TorchSharp/Tensor/Tensor.cs
Outdated
@@ -5369,6 +5369,79 @@ public override string ToString() | |||
return sb.ToString(); | |||
} | |||
|
|||
/// <summary> |
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.
This belongs in TensorExtensionMethods, I think.
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.
Another possibility is to add a 'style' argument to the verbose ToString() that currently takes a boolean.
Instead of
public string ToString(bool withData, string fltFormat = "g5", int width = 100, CultureInfo? cultureInfo = null)
it would be something like this:
public enum TensorStringStyle { Metadata, Julia, Numpy }
public string ToString(TensorStringStyle style, string fltFormat = "g5", int width = 100, CultureInfo? cultureInfo = null)
{
if (style == TensorStringStyle.Metadata) return ToString();
...
}
// For backward compat:
public string ToString(bool withData, string fltFormat = "g5", int width = 100, CultureInfo? cultureInfo = null)
{
return ToString(withData ? TensorStringStyle.Julia : TensorStringStyle.Metadata, fltFormat, width, cultureInfo);
}
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.
Then how about this?
public static TensorStringStyle DefaultOutputStyle = TensorStringStyle.Metadata;
public override string ToString() => ToString(DefaultOutputStyle);
public string ToString(TensorStringStyle style, string fltFormat = "g5", int width = 100,
CultureInfo? cultureInfo = null) => style switch {
TensorStringStyle.Metadata => ToMetadataString(),
TensorStringStyle.Julia => ToJuliaString(fltFormat, width, cultureInfo),
TensorStringStyle.Numpy => ToNumpyString(this, ndim),
_ => throw new InvalidEnumArgumentException("Not supported type")
};
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.
Sure. Don't you want CultureInfo and fltFormat on the NumPy string?
@@ -41,7 +48,7 @@ public static Modules.Parameter AsParameter(this Tensor tensor) | |||
/// </remarks> | |||
public static string str(this Tensor tensor, string fltFormat = "g5", int width = 100) |
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.
Maybe this should also take a style argument, but defaulted to 'Julia,' not 'Metadata' There could also be a 'npstr()' that defaults to NumPy. Same for print().
New test failures. This time, I think it's because of the tests doing this: .Select(x => x * 2.5 + 5) which leads to rounding differences in various runtimes. For a test like this, I think you probably want to have a fixed collection of numbers, not involving any FP arithmetic, so that you avoid the risk of rounding or other arithmetic-related errors. |
Added float format and culture to NumpyString().
Numpy string -- some suggested changes.
@dayo05 -- I just merged your changes. Looking good! With this, I will prepare and make a new release later today. |
New method is exists on
Tensor::str()
. I'll create more tests for that.