TorchSharp is a .NET library that provides access to the library that powers PyTorch. It is a work in progress, but already provides a .NET API that can be used to perform (1) various operations on ATen Tensors; (2) scoring of TorchScript models; (3) Training of simple neural networks.
Our current focus is to bind the entire API surfaced by libtorch.
Things that you can try:
using TorchSharp;
using TorchSharp.Tensor;
var lin1 = NN.Module.Linear(1000, 100);
var lin2 = NN.Module.Linear(100, 10);
var seq = NN.Module.Sequential(lin1, NN.Module.Relu(), lin2);
var x = FloatTensor.RandomN(new long[] { 64, 1000 }, device: "cpu:0");
var y = FloatTensor.RandomN(new long[] { 64, 10 }, device: "cpu:0");
double learning_rate = 0.00004f;
float prevLoss = float.MaxValue;
var optimizer = NN.Optimizer.Adam(seq.Parameters(), learning_rate);
var loss = NN.LossFunction.MSE(NN.Reduction.Sum);
for (int i = 0; i < 10; i++)
{
var eval = seq.Forward(x);
var output = loss(eval, y);
var lossVal = output.DataItem<float>();
Assert.True(lossVal < prevLoss);
prevLoss = lossVal;
optimizer.ZeroGrad();
output.Backward();
optimizer.Step();
}
Requirements:
- Visual Studio
- git
- cmake (tested with 3.14)
Commands:
- Building:
build.cmd
- Building from Visual Studio: first build using the command line
- See all configurations:
build.cmd -?
- Run tests from command line:
build.cmd -runtests
- Build packages:
build.cmd -buildpackages
Requirements:
- requirements to run .NET Core 2.0
- git
- cmake (tested with 3.14)
- clang 3.9
Example to fulfill the requirements in Ubuntu 16:
sudo apt-get update
sudo apt-get install git clang cmake libunwind8 curl
sudo apt-get install libssl1.0.0
sudo apt-get install libomp-dev
Commands:
- Building:
./build.sh
- Building from Visual Studio: first build using the command line
- See all configurations:
./build.sh -?
- Run tests from command line:
./build.sh -runtests
- Build packages:
./build.sh -buildpackages
This is used to update SHA hashes for LibTorch downloads:
msbuild src\Redist\build.proj /p:UpdateSHA=true /p:AssumeOS=linux
msbuild src\Redist\build.proj /p:UpdateSHA=true /p:AssumeOS=windows
msbuild src\Redist\build.proj /p:UpdateSHA=true /p:AssumeOS=macos
Downloads will not be repeated.
To change the package version update this file. Everything is currently considered in preview.
Use the following two MSBuild arguments in order to control the -preview and the build numbers in the name of the nuget packages produced (use one of the two generally):
Name | Value | Example Version Output |
---|---|---|
StabilizePackageVersion | true | 1.0.0 |
IncludeBuildNumberInPackageVersion | false | 1.0.0-preview |
Sample command: ./build.cmd -release -buildpackages -- /p:StabilizePackageVersion=true
For GPU support it is required to install CUDA 9.0 and make it available to the dynamic linker.
Porting of the more famous network architectures to TorchSharp is in progress. For the moment we only support MNIST and AlexNet