Skip to main content

Neural network with zero multiplications at inference. AdderNet + Hyperdimensional Computing.

Project description

AdderNet

Rede neural que não usa multiplicação na inferência. Zero.

Rede normal:   resultado = entrada * peso + bias    (mulsd + addsd)
AdderNet:      resultado = tabela[entrada + offset]  (1 acesso à memória)

O que faz?

AdderNet aprende funções usando apenas adição e subtração durante o treinamento, e na hora de prever o resultado basta ler uma posição da tabela — nenhuma conta de verdade.

Funciona bem para funções de uma variável: converter Celsius em Fahrenheit, aprender y = 3x + 7, etc.

Instalação

Requisitos: GCC, Python 3.8+, numpy

cd addernet_lib
make

Isso gera libaddernet.so.

Uso (Python)

from addernet import AdderNetLayer

# Criar a rede
rede = AdderNetLayer(size=256, bias=50, input_min=-50, input_max=200, lr=0.1)

# Dados de treino: Celsius -> Fahrenheit
celsius    = [0, 10, 20, 25, 30, 37, 50, 80, 100]
fahrenheit = [32, 50, 68, 77, 86, 98.6, 122, 176, 212]

# Treinar
rede.train(celsius, fahrenheit)

# Prever
print(rede.predict(37))   # 98.60
print(rede.predict(100))  # 212.00

Salvar e carregar

# Salvar modelo treinado
rede.save("meu_modelo.bin")

# Carregar depois
rede = AdderNetLayer.load("meu_modelo.bin")
print(rede.predict(37))  # 98.60

Previsão em lote (numpy)

import numpy as np

entradas = np.array([0, 25, 37, 100], dtype=np.float64)
saidas = rede.predict_batch(entradas)
# array([32.0, 77.0, 98.6, 212.0])

Usando em C

#include "addernet.h"

an_layer *rede = an_layer_create(256, 50, -50, 200, 0.1);

double inputs[]  = {0, 10, 25, 37, 100};
double targets[] = {32, 50, 77, 98.6, 212};
an_train(rede, inputs, targets, 5, 1000, 4000);

double resultado = an_predict(rede, 37.0);  // 98.60

an_save(rede, "modelo.bin");
an_layer_free(rede);

Compile com:

gcc -O3 -o meu_programa meu_programa.c -L. -laddernet -lm

Performance

Método Velocidade vs Python puro
Python AdderNet (dict) ~31K pred/s 1x
C AdderNet (uma por uma via ctypes) ~1.3M pred/s ~42x
C AdderNet (lote numpy) ~247M pred/s ~8000x

Para melhor performance em Python, use predict_batch() com arrays numpy.

Exemplos prontos

cd addernet_lib

# Converter Celsius -> Fahrenheit
python3 examples/celsius_fahrenheit.py

# Aprender funções de adição
python3 examples/addition.py

# Benchmark de performance
python3 examples/benchmark.py

Limitações

  • Funciona para funções de uma variável (entrada inteira -> saída double)
  • A entrada é truncada para int antes de indexar a tabela
  • Para valores fora do range de treino, a precisão diminui (extrapolação linear)
  • Não substitui redes neurais convencionais para problemas complexos (visão, linguagem, etc.)

Como funciona?

  1. Treino: Para cada amostra, testa se peso + lr ou peso - lr reduz o erro. Aplica a melhor direção.
  2. Expansão: Interpola entre os pontos de treino para gerar dados densos, e extrapola além das bordas usando a inclinação dos dois pontos mais próximos.
  3. Inferência: A tabela já tem o resultado final. É só acessar tabela[(entrada + offset) & máscara] — uma leitura de memória, zero aritmética.

Arquivos

addernet_lib/
├── addernet.h          Cabeçalho da API
├── addernet.c          Implementação em C
├── addernet.py         Bindings Python (ctypes)
├── Makefile            Compilação
├── test_main.c         Teste em C
└── examples/
    ├── celsius_fahrenheit.py
    ├── addition.py
    └── benchmark.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

addernet-0.1.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

addernet-0.1.0-py3-none-any.whl (41.7 kB view details)

Uploaded Python 3

File details

Details for the file addernet-0.1.0.tar.gz.

File metadata

  • Download URL: addernet-0.1.0.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for addernet-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0ba4a8e46837e6cea3168e2f6c55fa3eb7db56c372c1586a08d72d91e5e9dc7f
MD5 c3cd0627cd13f3850a12ff7498ca3a02
BLAKE2b-256 f76101013bfc56f89c59c9bcebd961a196adcb08dfbd7ad6df35b450d5d1985a

See more details on using hashes here.

File details

Details for the file addernet-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: addernet-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for addernet-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6ca97cd96f839c6654f019c6a3f5bdec78a3ea7cb5dc8fa02d8c1400d3785c1
MD5 4548e93b302dac3c5b2bc700869c2da2
BLAKE2b-256 3d6116f463cc8c8b0692c84250aa44d65a60caa101b2afc2f55e7990a8eb93a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page