B# is a dynamically typed multi-paradigm programming language designed for writing high-level modules (like machine learning pipeline using ML.NET) in .NET applications.
C# 11 | B# |
using System;
using torch = System.AI.torch;
using nn = System.AI.torch.nn;
using F = System.AI.torch.nn.functional;
using optim = System.AI.torch.optim;
public sealed class LinearRegression : nn.Module
{
public nn.Parameter weight;
public nn.Parameter bias;
public LinearRegression()
{
this.weight = new nn.Parameter(torch.randn());
this.bias = new nn.Parameter(torch.randn());
}
public torch.Tensor forward(torch.Tensor x)
{
return x * this.weight + this.bias;
}
}
static (torch.Tensor, torch.Tensor) GetData(int batch_size)
{
return (torch.randn(batch_size), torch.randn(batch_size));
}
static void Main()
{
var regr = new LinearRegression();
var opt = new optim.SGD(regr.parameters());
for(int ep = 0; ep < epoch; ++ep)
{
(var input, var target) = GetData();
y = regr.op_Invocation(input);
var loss = F.l1_loss(y, target);
loss.backward();
opt.step();
Console.WriteLine(loss);
}
} |
using * from System;
using torch from System.AI;
using nn from System.AI.torch;
using functional from System.AI.torch.nn as F;
using optim from System.AI.torch;
public sealed class LinearRegression : nn.Module
{
public:
weight;
bias;
constructor()
{
this.weight = new nn.Parameter(torch.randn());
this.bias = new nn.Parameter(torch.randn());
}
forward(x)
{
return x * this.weight + this.bias;
}
}
get_data(batch_size)
{
return (torch.randn(batch_size), torch.randn(batch_size));
}
main()
{
var regr = new LinearRegression();
var opt = new optim.SGD(regr.parameters());
for(var ep = 0; ep < epoch; ++ep)
{
(var input, var target) = GetData();
y = regr(input);
var loss = F.l1_loss(y, target);
loss.backward();
opt.step();
Console.WriteLine(loss);
}
} |
For more examples see B# vs C# vs Python
B# is compatible with given operating systems and .NET implementations:
OS | .NET |
---|---|
MS Windows XP and higer | Mono 2.0, .NET Framework 4.0 and higer, .NET Core, .NET |
Apple macOS 12.0 and higer | Mono 2.0, .NET Core, .NET |
Linux (Mint, Ubuntu) | Mono 2.0, .NET Core, .NET |
Android 10 and higer | Mono 2.0 |
- Windows 7 x64
- .NET Framework 4.5.2
- C# 5
- SharpDevelop 5.1 (IDE)
- Jay (Parser generator)