Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Underlying algorithm and MATLAB integration #175

Closed
hainingpan opened this issue Mar 22, 2019 · 9 comments
Closed

Underlying algorithm and MATLAB integration #175

hainingpan opened this issue Mar 22, 2019 · 9 comments
Labels

Comments

@hainingpan
Copy link

Would it be possible to elaborate on the underlying algorithm or refer to some papers so that I can properly cite it?

@basnijholt
Copy link
Member

basnijholt commented Mar 23, 2019

Thank you for your interest. Most of the algorithms are of our own making except what is specified in the credits.

We plan to write a paper that should be released around August. For now you could cite Zenodo where we have a DOI 10.5281/zenodo.1182437.

If you experience any issues feel free to ask for support on Gitter chat or open an issue.

@hainingpan
Copy link
Author

Thanks for the information! Another reason that I asked abouth the algorithm is that my current codes are all in matlab, but this algorithm seems helpful(And migrate all my previous matlab function to python seems a big burden ). Therefore, I am thinking about translating this adaptive learner to matlab. My current case is very simple 1D, R->R. So that's why I want to know more about the algorithm.

@basnijholt
Copy link
Member

basnijholt commented Mar 26, 2019

Adaptive excels in doing the calculation in parallel while being able to live-plot your data. This functionality won't be (easily or at all) portable to MATLAB.

Another benefit of using Adaptive is that one can easily adjust the sampling strategy by specifying another loss function, see this.

I would not recommend porting the code over because this will take an unreasonable amount of time. Rather, maybe you could call MATLAB code from Python.

Without the live-plotting and parallelism, your code would just be:

import adaptive

def goal(learner):
    return learner.loss() < 0.01

adaptive.Learner1D(
    function=lambda _:_, # you won't need to put the function here, because you would have it in MATLAB
    bounds=(-1, 1), # the bounds in between which you want to learn the function
)
while not goal(learner):
   points, loss_improvements = learner.ask(1)  # you don't need to use the `loss_improvements`
   x = points[0]
   value = calculation_in_matlab(x)  # This is the function you would need to implement.
   learner.tell((point, value)) 

If you would really be able to create a function with that exact interface (calculation_in_matlab(x)) then you can even use the normal runner and make use of all of Adaptive's cool functionalities.

@hainingpan
Copy link
Author

Cool! Thanks for your advice!

@hainingpan
Copy link
Author

Maybe one last question please..

I find I can surely call matlab function from Python, but when I set a lambda function for calculation_in_matlab() it just did not run. learner.data is empty even runner.task.don() says True.

Here is a minimal example,
In matlab, I have a simple script function test.m:

function y=test(x)
y=x^2;
end

Now I call matlab function from python, the code is:

import matlab.engine
import adaptive
import numpy as np
import matplotlib.pyplot as plt

eng = matlab.engine.start_matlab()
def goal(learner):
    return learner.loss() < 1e-2
func2=lambda x:eng.test(x)
learner=adaptive.Learner1D(function=func2,bounds=(-.3,.3))
runner=adaptive.Runner(learner,goal)
runner.task.done()
learner.data

You may try to type func2(2.) which return correct, native float type 4.0, meaning the calling seems to be successful.

But, I found that learner.data is acutally empty after the task is claimed to be done.

@jbweston
Copy link
Contributor

Please give more information about how you're running adaptive (jupyter notebook or regular python script?).

@hainingpan
Copy link
Author

Hi Weston,

Thanks for your package and great talk in Maryland again!

I just use jupyter notebook on Windows 10. Python 3.6.(Because Matlab engine API for Python at most support 3.6). Matlab version is 2018a. Basically, every native function defined in python is working properly but the function that calling external matlab function fails. (Please see the above minimal example)

@basnijholt
Copy link
Member

Could you try the "simple runner" like adaptive.runner.simple(learner, goal).

It might not work because of the parallelism.

@basnijholt basnijholt changed the title Underlying algorithm Underlying algorithm and MATLAB integration Apr 4, 2019
@hainingpan
Copy link
Author

Thanks. Cange to simple runner now works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants