Skip to main content

Gradient-free optimization of neural network parameters.

Project description

Intro

  • Gradient-free optimization of neural network parameters

  • Example: f(x) = x^2 (loss = log10(MSE(y_predict, x**2)))

  • Genetic algorithm:

  • AdamW (for comparison):

Usage

pip install grad-free-optim
import torch as tc
from pymoo.algorithms.soo.nonconvex.ga import GA as Algo
from trading_models.simple_models import MLP
from trading_models.utils import mod_keys, train_model

from grad_free_optim.pymoo import GradFreeOptim


def make_data():
    x = tc.rand((100, 1))
    yt = x**2
    return dict(x=x, yt=yt)


def loss_fn(data, mode):
    x, yt = data["x"], data["yt"]
    y = net(x)
    loss = tc.log10(((y - yt) ** 2).mean())
    info = {"SCORE": -loss}
    plots = {}
    return loss, mod_keys(info, mode), mod_keys(plots, mode)


tc.manual_seed(0)
net = MLP([1, 32, 32, 1])
train_data, test_data = make_data(), make_data()

if 1:
    bound = [-1.0, 1.0]
    opt = GradFreeOptim(bound, net, train_data, test_data, loss_fn)
    opt.run(Algo())
else:  # AdamW
    train_model(net, train_data, test_data, loss_fn)

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

grad_free_optim-0.0.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

grad_free_optim-0.0.1-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file grad_free_optim-0.0.1.tar.gz.

File metadata

  • Download URL: grad_free_optim-0.0.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for grad_free_optim-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d1060aa2a23ed77bc5898942a94ce5a6287be38a8927d3eef2c34a33ee1656a8
MD5 f28ed8a947b1340d05af9a85abf7586c
BLAKE2b-256 69e4c38cdd39150eaf5b3375fa8046b6f7e2dab335ca1798b5dbc05432bfe410

See more details on using hashes here.

File details

Details for the file grad_free_optim-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for grad_free_optim-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce7abf6b4373b64c4c90a3b436ed71698ce27464a09f08de931dfb57855d34d7
MD5 3cf1c3505e327aeac29098232f274c80
BLAKE2b-256 2d1ced0a806999fe28e0e319cfe06e25ce56ab3656f5ab33932f2acabb72e877

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