Skip to content

Repository files navigation

🧠 TAS · Machine Learning Framework for C#

TAS (Training Autograd System) is a minimalist pytorch-like machine learning framework made with C#, built on top of SimpleLinearAlgebra, a dependency-free matrix library that also lives in this repo — together with the whole family of from-scratch neural networks that grew around them.

CILanguageTargetUnityLicense

This project was made using as reference the work of @iamtrask and his book Grokking Deep Learning.

Repo map

FolderWhat it is
src/DLFrameworkThe TAS framework: tensors, autograd operations, layers, losses and optimizers
SimpleLinearAlgebraThe matrix + rank-4 tensor library everything is built on — zero dependencies, works anywhere (including Unity)
SimpleLinearAlgebra/examplesThree from-scratch neural networks written directly with matrices: mono-layer, multi-layer and a convolutional network trained end-to-end
examples/TasXorXOR trained with TAS, from raw tensors up to Sequential + SGD
examples/AutogradDemoGuided tour of the autograd engine — every gradient checked against the derivative by hand
examples/SinRegressionAn MLP fits y = sin(x) end-to-end and plots the result as ASCII art
tests/ · SimpleLinearAlgebra/testsxUnit suites for the framework and the library

Everything targets netstandard2.0 with C# 7.3 and zero external dependencies, so both libraries work in modern .NET, .NET Framework, Mono and Unity — you can even copy the sources straight into an Assets/ folder.

Until 2026 these lived as five separate repos (Simple_Linear_Algebra, Simple-vectorized-mono-layer-perceptron, Vectorized-multilayer-neural-network, Convolutional-Neural-Network-From-Scratch and this one); they were merged, modernized and tested here.

Quick start

dotnet build TAS.sln # build everything
dotnet test TAS.sln # run the test suites
dotnet run --project examples/TasXor # XOR with TAS
dotnet run --project examples/AutogradDemo # guided tour of the autograd engine
dotnet run --project examples/SinRegression # fit sin(x), plotted in the terminal
dotnet run --project SimpleLinearAlgebra/examples/MonoLayerPerceptron # logic gates, by hand
dotnet run --project SimpleLinearAlgebra/examples/MultiLayerPerceptron # XOR, generalized
dotnet run --project SimpleLinearAlgebra/examples/ConvolutionalNeuralNetwork # Fashion-MNIST (auto-downloads)

How TAS works

TAS is an automatic differentiation framework inspired by pytorch. It uses a dynamic computational graph that allows changes at runtime, which makes it perfect for experimentation at the expense of performance. The core of TAS are the Tensors, a generalization of the concept of matrix for superior dimensions — TAS currently supports tensors of up to 2 dimensions, which is enough for text analysis, reinforcement learning and classic dense networks.

Creating tensors

usingLinearAlgebra;usingDLFramework;usingDLFramework.Operations;vardata=newTensor((Matrix)newdouble[,]{{0,0},{0,1},{1,0},{1,1}},true);

The first argument is the Matrix that will be converted into a tensor; the second marks the tensor as autograd, which lets gradients flow through it.

Math operations chain naturally, and if the operands are autograd the result will be too:

varmultiplication=data.MatMul(weights);

Backpropagate a gradient through the graph with Backward:

loss.Backward(newTensor(Matrix.Ones(loss.Data.X,loss.Data.Y)));weight.Data-=weight.Gradient.Data*0.1;weight.Gradient.Data*=0;

Layers, losses and optimizers

The manual process above is wrapped by familiar abstractions — Sequential, Linear, SigmoidLayer/ReLuLayer, MeanSquaredError and StochasticGradientDescent:

varseq=newSequential();seq.Layers.Add(newLinear(2,5,r));seq.Layers.Add(newSigmoidLayer());seq.Layers.Add(newLinear(5,1,r));seq.Layers.Add(newSigmoidLayer());varsgd=newStochasticGradientDescent(seq.Parameters,1);varmse=newMeanSquaredError();for(vari=0;i<300;i++){varpred=seq.Forward(data);varloss=mse.Forward(pred,target);loss.Backward(newTensor(Matrix.Ones(loss.Data.X,loss.Data.Y)));sgd.Step();}

examples/TasXor walks through five versions of the same XOR network, from raw tensors and manual gradient descent (FirstNN) to the full Sequential + SGD + MSE stack (FifthNN) — that progression is the best tour of the framework.

Features

  • Autograd operations: Add, Sub, Neg, Mul (element-wise), MatMul, Transpose, Expand, Sum
  • Layers: Linear, Sigmoid, ReLu, Sequential
  • Loss: Mean Squared Error
  • Optimizer: Stochastic Gradient Descent
  • Initializators: Gaussian and Uniform random

TODO

  • Support for real tensors, not just matrices
  • Performance adjustments
  • More layers, loss functions and activation functions

More interesting projects

License

This project was totally handcrafted, so the license is MIT — use it as you want.

Let's connect 😋

Hector's LinkedInHector's TwitterHector's TwitchHector's Youtube

About

TAS, a minimalist pytorch-like autograd framework in C#, plus SimpleLinearAlgebra: a dependency-free netstandard2.0 matrix library (Unity-compatible) with from-scratch perceptron and CNN examples. Tested and CI'd.

Topics

Resources

Stars

14 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages