Skip to main content

Tunafish is an autotuner for plain ol' Python functions. Under the hood, it uses genetic algorithms to determine which arguments maximize a function's return value (i.e. it's fitness); however, Tunafish eliminates the need to think about genetic algorithms. Instead, you can focus more on just writing code.

Project description

Tunafish

Autotuner For Python Functions

Tunafish is an autotuner for plain ol' Python functions. Under the hood, it uses genetic algorithms to determine which arguments maximize a function's return value (i.e. it's fitness); however, Tunafish eliminates the need to think about genetic algorithms. Instead, you can focus more on just writing code.

To use Tunafish, your objective function must satisfy the following two conditions:

  1. Arguments must be annotated as primitives types, like float, int, str, and bool, typing.Text, etc.
  2. The return value must be expressed as a single float (i.e. a fitness value).

Example: Automated Trading

Consider a function whose objective is to place orders to buy and sell stocks. The input arguments are aggression, which regulates the a minimum amount of time between orders, and window, which determines how far back the trading algorithm should looks when deciding when take action. The return value is simply the net gain or loss generated while trading, which is used as a proxy for fitness.

Define The Objective Function

from example_project import create_trader, load_historical_trading_data

trader = create_trader()
training_data = load_historical_trading_data(start, stop, interval)

def trade(aggression: float, window: int) -> float:
  gains = trader.trade(training_data, aggression, window)
  return gains  # AKA fitness

Tune it...

from tunafish import FunctionTuner

tuner = FunctionTuner()
arguments = tuner.tune(trade, options={
  'aggression': {'min': 0.01, 'max': 1.0},
  'window': {'min': 5, 'max': 20}
})

More Examples

Working examples can be found in tunafish.examples. The "basic" and "early_stopping" examples differ only in that "early_stopping" shows you how to control a bit more of the internals of the genetic algorithm. In particular, we tell it to exit the training loop early if we reach a fitness goal before all 500 epochs have run. Running these examples should generate a plot, showing convergence of fitness versus time.

Max Fitness Per Epoch Graph

Running Examples

Just do python -m tunafish.examples.basic!

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

tunafish-0.0.3.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

tunafish-0.0.3-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file tunafish-0.0.3.tar.gz.

File metadata

  • Download URL: tunafish-0.0.3.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.4

File hashes

Hashes for tunafish-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5b51a79da8c90648f1c109de65c1a421468467d162c443ef939f65753f0117c2
MD5 818682248b18ddf982155ce04f0b29af
BLAKE2b-256 811eaec1f85bd928e2f1ee92517b051da076dc6a5d9e213156cd529c0146e9af

See more details on using hashes here.

File details

Details for the file tunafish-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: tunafish-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.4

File hashes

Hashes for tunafish-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 da4f49cbdc7a7e46d53360679597dd59807a6f06cecd10841aba1de81f4ee001
MD5 b0dac67ea6b43cdb6033ca1b296182a7
BLAKE2b-256 6c97b17ff61f56795c15c567f791d4fc7b70d390734d054f4abbad113825e30b

See more details on using hashes here.

Supported by

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