Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use the official .NET 8 SDK image
FROM mcr.microsoft.com/dotnet/sdk:8.0
FROM mcr.microsoft.com/dotnet/sdk:10.0

# Set the working directory inside the container
WORKDIR /workspace
Expand Down
5 changes: 4 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "NET 8 DevContainer",
"name": "NET 10 DevContainer",
"build": {
"dockerfile": "Dockerfile"
},
"mounts": [
"source=${localEnv:USERPROFILE}/.nuget/packages,target=/root/.nuget/packages,type=bind,consistency=cached"
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout
uses: actions/checkout@main

- name: Setup .NET 8
- name: Setup .NET 10
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.100'
dotnet-version: '10.0.x'

- name: Build
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout
uses: actions/checkout@main

- name: Setup .NET 8
- name: Setup .NET 10
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.100'
dotnet-version: '10.0.x'
source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
env:
NUGET_AUTH_TOKEN: ${{ github.token }}
Expand Down
16 changes: 8 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.idea/
.vscode/

tiny-brain.sln.DotSettings.user

obj/
bin/

.idea/
.vscode/
tiny-brain.sln.DotSettings.user
obj/
bin/
63 changes: 63 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Commands

```bash
# Build entire solution
dotnet build code/tiny-brain.sln

# Run all tests
dotnet test code/test/tiny-brain-test.csproj

# Run tests with coverage
dotnet test code/test/tiny-brain-test.csproj /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov

# Run a single test by name
dotnet test code/test/tiny-brain-test.csproj --filter "FullyQualifiedName~<TestName>"

# Run the bigram use-case
dotnet run --project code/use-cases/biagram/biagram.csproj

# Run the playground
dotnet run --project code/use-cases/playground/playgroung.csproj
```

## Architecture

The library is a from-scratch implementation of a neural network with autograd (inspired by micrograd / cs231n). Target: .NET 8, namespace `TinyBrain`.

### Core abstraction: `Operand`

`Operand` (`code/src/Expression/Operand.cs`) is the fundamental building block — a scalar value that carries its gradient and a `_backward` closure. All arithmetic operators (`+`, `-`, `*`, `/`) and unary ops (`Exp`, `Log`, `Pow`, `Relu`, `Tanh`) are overloaded to return new `Operand` instances that record their `Previous` pair. Calling `Backpropagation()` on an `Operand` runs a non-recursive topological sort over the expression graph and then invokes each node's `_backward` in reverse order.

### Neural network layers

```
Brain (MLP)
└─ Layer[] (one per hidden/output layer)
└─ Neuron[] (each neuron owns its weights + bias as Operands)
```

- `Neuron` (`code/src/Neuron/Neuron.cs`) computes `f(Σ(xᵢ·wᵢ) + b)` where `f` is determined by `ActivationType`.
- `Layer` (`code/src/Layers/Layer.cs`) fans inputs through all neurons; `Forward` returns one `Operand` per neuron.
- `Brain` (`code/src/MLP/Brain.cs`) chains layers via `Fold`; `Forward` auto-zeros gradients before each pass.

### Activation functions

`Activations.cs` holds the dispatch dictionary keyed on `ActivationType` (currently `None` and `Tanh`). `Tanh` is expressed in terms of `Exp`, `/`, and `-` on `Operand` so its gradient flows automatically through the graph — no hand-coded derivative needed.

### Functional style

The library depends on `tiny-fp` (functional primitives: `Option`, `Unit`, `Map`, `Tee`, `Fold`, `ForEach`). Prefer these combinators over imperative loops; `Unit` is used as the return type of side-effecting void-like operations.

### Use-cases

`code/use-cases/biagram/` implements a character-level bigram language model using a `Brain(27 → 27)` with `ActivationType.None` followed by a manual softmax. Parameters can be saved/loaded via `SaveParameters` / `LoadParameters` to `parameters.txt`.

`code/use-cases/playground/` is a scratch area for manual experiments.

### Test structure

Tests use NUnit 3 + Shouldly assertions. `MLPTrainingTests.Training` is a full end-to-end gradient-descent loop that runs until loss < 0.00001 or 10 000 steps — it is intentionally slow and exercises the entire autograd pipeline.
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2025 Franco Melandri

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2025 Franco Melandri
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# tiny-brain

The aim of the project is to create a small system based on neural network.

This system is called `tiny brain`.

I would like to consider this like a small brain that could be integrated inside bigger application
I would like to use the brain as a base engine to perform tasks


# tiny-brain
The aim of the project is to create a small system based on neural network.
This system is called `tiny brain`.
I would like to consider this like a small brain that could be integrated inside bigger application
I would like to use the brain as a base engine to perform tasks
Loading
Loading