dotnet / TorchSharp

A .NET library that provides access to the library that powers PyTorch.
MIT License
1.4k stars 182 forks source link

Is there interest to make TorchSharp for user friendly with e.g. .NET Interactive? #297

Closed GeorgeS2019 closed 3 years ago

GeorgeS2019 commented 3 years ago
New TorchSharp Test Project
Torch.SetSeed(1234);
var tensor = Float32Tensor.rand(new long[] { 4, 4 });
Console.Write(tensor)

Currently, there is no friendly print function when using TorchSharp in e.g. .NET Interactive , or I could have miss it?

NiklasGustafsson commented 3 years ago

@GeorgeS2019 -- if I understand your request, it's that you want a better 'ToString()' for TorchTensor, right?

[BTW, I added convenience overloads for 'rand' that takes the rows and columns, so you don't have to construct an array. Not sure whether that's in the latest NuGet or not, but it's coming.]

GeorgeS2019 commented 3 years ago

@NiklasGustafsson

Tensorflow.NET is setup to be as close to python code as possible in different environment e.g. SciSharpCube (.NET jupyter).

The motivation is that eventually it is possible to use a pythonToCsharp converter to port e.g. pyTorch code to TorchSharp in almost similar code layout in high percentage.

image

GeorgeS2019 commented 3 years ago

@NiklasGustafsson

torch.normal(mean=0.5,  std=torch.arange(0.2,0.6))

Torch.Arange seems to take only Integer parameters. Please correct me if I am wrong :-)

GeorgeS2019 commented 3 years ago

FYI all sample codes come from

https://github.com/Apress/pytorch-recipes

NiklasGustafsson commented 3 years ago

The Python version of arange() takes all kinds of numbers.

See: https://pytorch.org/docs/stable/generated/torch.arange.html?highlight=arange#torch.arange

NiklasGustafsson commented 3 years ago

Working on a better ToString() would be interesting. I'm personally partial to how Julia prints tensors with more than two dimensions:

julia> x = rand(5,5, 5,5)
5×5×5×5 Array{Float64, 4}:
[:, :, 1, 1] =
 0.223483   0.753928   0.452195  0.715721  0.126067
 0.0451747  0.0833317  0.748877  0.4885    0.00116933
 0.34196    0.742832   0.838799  0.581502  0.317049
 0.175496   0.710619   0.129817  0.860807  0.0574395
 0.282793   0.828668   0.998318  0.874329  0.814805

[:, :, 2, 1] =
 0.728446  0.198255  0.809055  0.170687  0.207785
 0.022082  0.954964  0.461224  0.919482  0.990253
 0.101978  0.551891  0.57791   0.265696  0.759774
 0.854822  0.685592  0.837295  0.306617  0.368236
 0.527747  0.337722  0.347441  0.487223  0.813792

[:, :, 3, 1] =
 0.0282523  0.864711  0.656607  0.990119  0.365758
 0.485043   0.046835  0.788797  0.956218  0.888144
 0.0170756  0.602304  0.131042  0.530784  0.189504
 0.358518   0.610178  0.262251  0.70639   0.374479
 0.377738   0.609482  0.992617  0.754387  0.0318602

I like that the indices are printed (Julia is a column-major language, so the indices would be in the first dimensions rather than the last in .NET.)

@dsyme -- you probably have thoughts on this topic.

NiklasGustafsson commented 3 years ago

I'm proposing a second ToString() overload defined on TorchTensor:

public string ToString(bool withData, string fltFormat = "g5", int width = 100)

The boolean is there to distinguish from the overriden ToString(), and is not necessary if the method is called something else.

One could also define it as:

public new string ToString(bool withData = false, string fltFormat = "g5", int width = 100)

to hide the overridden method.

Anyway, it would produce output like this:

[2x2x4], type = Float32, device = cpu

[0,:,:] =
 0.0000000   3.1410000 6.2834000    3.1415200
 0.0000063 -13.1415300 0.0100000 4713.1400000

[1,:,:] =
 0.0100000 0.0000000 0.0000000 0.0000000
 0.0000000 0.0000000 0.0000000 0.0000000

In this case, by passing in "0.0000000" as the format string.

The width parameter is there to prevent the method from making up extremely long lines. Instead, when the width is reached, each row ends with '...'

GeorgeS2019 commented 3 years ago

@NiklasGustafsson

FYI more time needed to load LibTorch CPU and GPU

image

dsyme commented 3 years ago

@NiklasGustafsson I like all your proposals here

GeorgeS2019 commented 3 years ago

@NiklasGustafsson

FYI

New TorchSharp Test Project
NiklasGustafsson commented 3 years ago

@GeorgeS2019 -- it will currently adhere to the current locale as far as comma vs. period.

It'll impact the unit tests, but apart from that, it seems to me the right behavior.

GeorgeS2019 commented 3 years ago

@NiklasGustafsson

Combined pull requests 301 and 302 in Visual Studio Code .NET Interactive!

image