[WIP: Feb2023] Pytorch to TorchSharp using .NET interactive (.NET c# kernel) #733
Replies: 10 comments 18 replies
-
|
Beta Was this translation helpful? Give feedback.
-
numpy_arr = np.array([[1.0, 2.0, 3.0],
[10.0, 20.0, 30.0],
[100.0, 200.0, 300.0]])
tensor_from_numpy = torch.from_numpy(numpy_arr) How to handle that in TorchSharp?
|
Beta Was this translation helpful? Give feedback.
-
PyTorch to TorchSharp: the python wayPyTorchmy_tensor = torch. Tensor([[1,2,3],[4,5,6]], dtype = torch.float32) PyToCsusing System.Collections.Generic;
public static class testModule {
public object my_tensor = torch.tensor(new List<object> {
new List<object> {
1,
2,
3
},
new List<object> {
4,
5,
6
}
}, dtype: torch.float32);
} Desired c# TorchSharp's python wayvar my_tensor = torch. Tensor(new int[,]{{1,2,3},{3,5,6}}, ScalarType.Float32) In PyToCs terms
suggestionOnce torch is detected in method, TorchSharp specific argument translation is applied
|
Beta Was this translation helpful? Give feedback.
-
FYI@NiklasGustafsson |
Beta Was this translation helpful? Give feedback.
-
@GeorgeS2019 -- unfortunately, I can't pretend that I know what it is you are proposing. What does this 'PyToC' tool do, who would use it, and why/when? |
Beta Was this translation helpful? Give feedback.
-
PyToCs is python to cSharp. It is WIP.
This could serve a few purposes
|
Beta Was this translation helpful? Give feedback.
-
What do you think of porting e.g. Microsoft python 101 bootcamp class (especially those teaching pytorch) into TorchSharp? The ported notebooks could serve as the starting education materials for colleges and universities. The NEXT significant milestones for TorchSharp? |
Beta Was this translation helpful? Give feedback.
-
As far as numpy goes, and as we have discussed elsewhere, there's no need for that. In the bootcamp notebooks, it's only used to create tensors from arrays, and we have that in TorchSharp already -- from_array, etc. I understand the general desire for a solid numerics library for .NET, but that's far outside the scope of TorchSharp, and not required for these tutorials. We don't need a .NET numerics library for n-d arrays, we already have those in .NET. Matplotlib is another matter, but I would prefer that we'd rely on a .NET solution, instead of requiring Python interop in order to get things to work. In fact, I will be very reluctant to merge a PR that isn't pure .NET, even in TorchSharpExamples. That's the philosophy behind TorchSharp -- no Python required. You may agree or disagree with that stance (there are good arguments both ways), but that's a core principle. There are .NET-based plotting libraries, and we should use them for this. There's a third library used in the bootcamp -- Pandas. However, the only thing I believe it's used for is to read CSV files, which isn't trivial, but neither is it rocket science. There are several CSV libraries for .NET, too. |
Beta Was this translation helpful? Give feedback.
-
BTW, all of the bootcamp courses look fine. The CV content is fairly well covered in the example, and some of the NLP ones, but I agree that it would be great to port the specific course material to TorchSharp. |
Beta Was this translation helpful? Give feedback.
-
Sure, we can and should come up with ideas on how to accomplish things. As far as a consensus goes, I don't think we need that. TorchSharp is, and will remain, a thin layer on top of libtorch. That's it. How folks want to plot data or integrate with other libraries is not something TorchSharp should mandate or have a strong opinion on. Libraries and repos that take a dependency on TorchSharp are free to do whatever they want and involve the runtime stacks that make sense for their users. For TorchSharp, rule #1 is that it must be a .NET-based solution. We have nothing against libraries and solutions that involve Python in the runtime stack -- it's an excellent solution for many scenarios. That said, TorchSharp's #1 architectural principle is that it does not depend on Python at runtime in any way. Library code going in dotnet/TorchSharp, and examples going in dotnet/TorchSharpExamples should be .NET-based. |
Beta Was this translation helpful? Give feedback.
-
After ML.NET, PyToCs is the second most Stars C# github repos that is using TorchSharp!
Thanks for @toolgood
============ 2023 ===========================================
Recently @toolgood contributed the additional PyToCS (Python To CSharp) codes needed to move these discussions forwards.
============ 2022 ===========================================
This will be a joint collaboration between the TorchSharp, :NET Interactive teams, and perhaps others
Use Case
I would like to open a working PyTorch notebook using .NET interactive to determine which of these PyTorch statements in python NOT supported in TorchSharp.
For each of these "nonsupported" PyTorch statements, add an issue to TorchSharp GitHub
Please note
These notebooks will be those used in PyTorch courses. More on PyTorch syntax and NOT a mix with other python libraries like e.g. numpy
Beta Was this translation helpful? Give feedback.
All reactions