Git Product home page Git Product logo

Comments (2)

basnijholt avatar basnijholt commented on August 10, 2024

originally posted by Anton Akhmerov (@anton-akhmerov) at 2018-10-25T19:14:37.496Z on GitLab

@jorn impressive writeup and a really cool proposal!

from adaptive.

basnijholt avatar basnijholt commented on August 10, 2024

originally posted by Bas Nijholt (@basnijholt) at 2018-10-25T19:54:53.532Z on GitLab

Like we discussed already this week, this is super awesome!

I took the relevant code (notebook has a lot of NameErrors) and sped it up a bit:

from bisect import bisect_left

import adaptive
adaptive.notebook_extension()
import numpy as np

from adaptive.learner.learner1D import default_loss
from adaptive.learner.learnerND import volume


def simple_runner(learner, goal):
    while not goal(learner):
        x = learner.ask(1)[0][0]
        y = learner.function(x)
        learner.tell(x, y)

        # Hack to also update the losses of neigbouring intervals
        sorted_data = sorted(learner.data)
        index = bisect_left(sorted_data, x)
        xs = [sorted_data[i] for i in range(index-1, index+3)
              if 0 <= i < len(sorted_data)]
        if len(xs) > 2:
            for i in range(len(xs)-1):
                ival = xs[i], xs[i+1]
                learner._update_interpolated_loss_in_interval(*ival)

def loss_of_multi_interval(xs, function_values):
    pts = [(x, function_values[x]) for x in xs]
    N = len(pts) - 2
    return sum(volume(pts[i:i+3]) for i in range(N)) / N

def triangle_loss(interval, scale, function_values):
    _default_loss = default_loss(interval, scale, function_values)
    x_left, x_right = interval
    data = sorted(function_values)
    index = bisect_left(data, x_left)
    xs = [data[i] for i in range(index-1, index+3) if 0 <= i < len(data)]
    dx = x_right - x_left
    if len(xs) <= 2:
        return dx
    else:
        triangle_loss = loss_of_multi_interval(xs, function_values)
        return triangle_loss**0.5 + 0.02 * _default_loss + 0.02 * dx

def f5(x):
    return np.exp(-(x-0.3)**2/0.1**3)

learner = adaptive.Learner1D(f5, (-1, 1), loss_per_interval=triangle_loss)
simple_runner(learner, goal=lambda l: l.npoints > 1000)

learner.plot()

from adaptive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.