Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deep Learning vs Elementary Math

Solving 2x = 1 with everything except common sense.

This repository is a tiny experimental playground where we ask an important question that nobody needed answered:

How many deep learning paradigms can we throw at a problem that elementary algebra solves in one line?

The answer is: more than society probably intended.

We compare a few wildly overqualified methods on simple linear equations of the form ax = b. Some methods solve the task. Some learn slowly. Some hallucinate politely. Some are here because once PyTorch is installed, human judgment apparently leaves the room.

Why does this exist?

Because simple problems are excellent microscopes.

When a model fails on a hard benchmark, everyone can hide behind complexity.
When a model fails on 2x = 1, there is nowhere to run.

This project is partly a joke, partly a teaching demo, and partly a small behavioral study of how different learning paradigms behave when forced into an absurdly simple symbolic/numeric task.

In other words:

  • algebra solves the problem instantly;
  • regression approximates the obvious;
  • reinforcement learning turns division into a lifestyle;
  • GANs attempt diplomacy with a single number;
  • transformers may learn that x = should be followed by some number, which is adorable in the way a toaster trying to write poetry is adorable.

Project layout

.
├── train.py                 # Unified CLI entry point
├── configs/
│   └── default.json          # Default experiment configuration
├── models/                   # One module per method
├── utils/
│   ├── benchmark.py           # Run multiple methods and save JSON records
│   ├── leaderboard.py         # Rank saved runs by various questionable metrics
│   └── ...                    # Config loading, equation parsing, recording, helpers
└── outputs/runs/              # Default directory for generated run records

Quick start

Install dependencies:

pip install -r requirements.txt

Run one method:

python train.py --method algebra
python train.py --method linear
python train.py --method reinforce
python train.py --method gan
python train.py --method gan_point
python train.py --method dqn_discrete
python train.py --method dqn_general
python train.py --method transformer
python train.py --method custom

Each run writes a JSON record to outputs/runs/ by default.

For a throwaway run:

python train.py --method linear --no-save

Custom equations

The project supports equations in the form:

ax=b

Examples:

python train.py --method algebra --equation "3x=7"
python train.py --method linear --equation "-2.5x=1"

If the coefficient is negative, the safest CLI form is:

python train.py --method linear --equation=-2.5x=1

Because command-line parsers, like neural networks, enjoy misinterpreting simple things.

Available methods

algebra

The undefeated final boss.

It solves ax=b using division, as civilization intended.

linear

A one-layer regression model trained to solve the equation numerically.

This is still overkill, but at least it is the kind of overkill that can be explained to a committee.

reinforce

A policy-gradient method searching for the solution.

Useful if you have ever looked at 2x = 1 and thought:

This should be a sequential decision-making problem.

gan

A GAN that learns a distribution centered around the solution.

Because apparently one number was not enough. We needed a generator, a discriminator, and a small philosophical crisis.

gan_point

A GAN trained against a single solution point.

This method is included for scientific completeness and comedic density.

dqn_discrete

A DQN agent with discrete step actions for the original 2x = 1 environment.

It approaches algebra the way a person approaches a locked door by training a robot to slowly bump into it.

dqn_general

A DQN setup on random ax=b instances.

Slightly more general, still deeply unnecessary.

transformer

A tiny seq2seq transformer that maps equation strings to solution strings.

This is where things become interesting. Sometimes the model appears to learn the structure. Sometimes it appears to learn that after x =, a number should happen.

This is not symbolic reasoning. This is autocomplete wearing a lab coat.

custom

A plug-in interface for your own method.

Because if you have another unreasonable way to solve elementary algebra, this project welcomes your contribution. Regrettably.

Configuration

The default configuration lives at:

configs/default.json

Run with a config file:

python train.py --config configs/default.json --method linear

Override config values from the command line:

python train.py --method linear --set methods.linear.epochs=20 --set methods.linear.lr=0.05

This lets you tune the amount of computational drama applied to a problem with a closed-form solution.

Custom model plug-in

Set methods.custom.entry_point to an import path in the format:

module.path:function

Examples:

models.custom_example:solve
myteam.solver:train

The driver passes all keys from methods.custom as keyword arguments, except:

  • entry_point
  • params_count
  • runner_id

You can add hyperparameters in JSON or override them with --set.

Optional fields:

  • runner_id: short name used in saved records and leaderboards;
  • params_count: parameter count used by the Reasoning / Comedy leaderboards, honor system, because apparently we still believe in society.

When using:

--equation "ax=b"

the values a and b are injected into compatible methods, including:

  • algebra
  • linear
  • gan_point
  • custom

Security note:

entry_point runs arbitrary importable code. Only use configs from people you trust.

If someone sends you a mysterious config and says “just run it,” maybe do not let natural selection win today.

See:

models/custom_example.py

for a minimal custom implementation.

Benchmarking

Run multiple methods and save one result file per method:

python -m utils.benchmark --methods algebra linear gan_point
python -m utils.benchmark --methods algebra linear --set methods.linear.epochs=20
python -m utils.benchmark --methods algebra linear gan_point --equation "3x=7"

Keep going even if one method explodes with dignity:

python -m utils.benchmark --methods algebra linear gan_point --continue-on-error

Custom equations are applied to methods with a stable single-equation interface:

  • algebra, linear, gan_point, custom: receive a and b;
  • gan: receives the target solution as its real-sample center;
  • transformer: receives a formatted test_eq.

The following methods still use their original tiny environments:

  • reinforce
  • dqn_discrete
  • dqn_general

Their equation logic is baked into the environment, because even toy projects deserve legacy design debt.

Leaderboards

Read saved JSON records and print rankings:

python -m utils.leaderboard
python -m utils.leaderboard --worst-per-method

The leaderboard categories are named like serious benchmark metrics, then ranked in the least flattering direction:

Leaderboard Ranked by
Reasoning Most parameters
Coding Slowest convergence
Math Largest absolute error
Comedy Most overkill

This is not because we hate deep learning.

It is because we respect algebra.

Example module runs

You can also run some modules directly:

python -m models.linear
python -m models.transformer

Useful for quick experiments, debugging, or quietly confirming that a neural network has once again found a creative way to misunderstand a one-line equation.

What should I look for?

This project is not about beating algebra. Algebra already won.

The interesting part is watching how different methods fail, converge, overfit, approximate, or accidentally reveal their inductive biases.

Possible things to inspect:

  • final absolute error;
  • convergence speed;
  • parameter count;
  • runtime;
  • output format mistakes;
  • whether the method learned the equation or merely learned the shape of an answer;
  • how much machinery was required to rediscover division.

Contributing

Contributions are welcome if they satisfy at least one of the following:

  • they add a new unnecessarily powerful method;
  • they improve the benchmark or leaderboard;
  • they make the failure modes easier to inspect;
  • they add visualization;
  • they increase the scientific value;
  • they increase the comedic value;
  • ideally, both.

Possible future additions:

  • LSTM / RNN solver;
  • neural arithmetic units;
  • symbolic regression baseline;
  • chain-of-thought style solver;
  • more equation families;
  • visualizations of training dynamics;
  • a Hall of Shame for especially cursed outputs.

Citation

There is no paper. Yet.

If this repository helps you teach, debug, laugh, or explain why next-token prediction is not the same thing as algebra, feel free to cite the repo.

If it convinces you to use algebra for 2x = 1, even better. Society heals one equation at a time.

About

Solving 2x = 1 with everything except algebra.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages