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

restructure algorithm configuration for hyperopt_service #1161

Merged
merged 6 commits into from
Apr 26, 2020

Conversation

sperlingxx
Copy link
Member

Restructuring algorithm configuration related codes for hyperopt_service, which makes it easier to modify algorithm schema in future.

@andreyvelich @gaocegege @c-bata

@kubeflow-bot
Copy link

This change is Reviewable

Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sperlingxx! Thank you for adding validations 👍 I put some review comments.

pkg/suggestion/v1alpha3/hyperopt/base_hyperopt_service.py Outdated Show resolved Hide resolved
pkg/suggestion/v1alpha3/hyperopt_service.py Outdated Show resolved Hide resolved
Comment on lines 54 to 65
__schema_dict = {
'tpe': {
'gamma': (lambda x: float(x), lambda x: 1 > float(x) > 0),
'prior_weight': (lambda x: float(x), lambda x: float(x) > 0),
'n_EI_candidates': (lambda x: int(x),
lambda x: float(x).is_integer() and int(x) > 0),
"random_state": (lambda x: int(x), lambda x: x.isdigit()),
},
"random": {
"random_state": (lambda x: int(x), lambda x: x.isdigit())
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mind that validate functions ( __schema_dict[algo_name][s.name][1] ) cannot return the message why parameters are invalid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I personally feels this dict is a bit tricky. I prefer the following code:

@classmethod
def _validate_tpe_setting(algorithm_settings):
    for s in algorithm_settings:
        if s == 'gamma' and 0 < float(x) < 1:
            return False, "gamma should be in the range of [0, 1]"
        ...
    return True, ""

@classmethod
def validate_algorithm_spec(cls, algorithm_spec):
    algo_name = algorithm_spec.algorithm_name
    if algo_name == "tpe":
        return cls._validate_tpe_setting(algorithm_spec.algorithm_setting)
    elif algo_name == "random":
        return cls._validate_random_setting(algorithm_spec.algorithm_setting)
    return False, "invalid algorithm name"

But I want to hear opinions of maintainers ( @andreyvelich @gaocegege ).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with both code styles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @c-bata, I think for validation we should say why settings is not correct. Like we did with enas: https://github.com/kubeflow/katib/blob/master/pkg/suggestion/v1alpha3/nas/enas_service.py#L218

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c-bata @andreyvelich I've refactored codes of validation part.

Copy link
Member

@andreyvelich andreyvelich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for improve hyperopt, I added few comments.

pkg/suggestion/v1alpha3/hyperopt_service.py Outdated Show resolved Hide resolved
pkg/suggestion/v1alpha3/hyperopt_service.py Outdated Show resolved Hide resolved
Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits!

/lgtm

Comment on lines 91 to 101
if not (1 > float(s.value) > 0):
return False, "gamma should be in the range of (0, 1)"
elif s.name == 'prior_weight':
if not (float(s.value) > 0):
return False, "prior_weight should be great than zero"
elif s.name == 'n_EI_candidates':
if not (int(s.value) > 0):
return False, "n_EI_candidates should be great than zero"
elif s.name == 'random_state':
if not (int(s.value) >= 0):
return False, "random_state should be great or equal than zero"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits] I guess you can remove redundant brackets. It's OK to ignore this comment.

Suggested change
if not (1 > float(s.value) > 0):
return False, "gamma should be in the range of (0, 1)"
elif s.name == 'prior_weight':
if not (float(s.value) > 0):
return False, "prior_weight should be great than zero"
elif s.name == 'n_EI_candidates':
if not (int(s.value) > 0):
return False, "n_EI_candidates should be great than zero"
elif s.name == 'random_state':
if not (int(s.value) >= 0):
return False, "random_state should be great or equal than zero"
if not 1 > float(s.value) > 0:
return False, "gamma should be in the range of (0, 1)"
elif s.name == 'prior_weight':
if not float(s.value) > 0:
return False, "prior_weight should be great than zero"
elif s.name == 'n_EI_candidates':
if not int(s.value) > 0:
return False, "n_EI_candidates should be great than zero"
elif s.name == 'random_state':
if not int(s.value) >= 0:
return False, "random_state should be great or equal than zero"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only found this commit suggestion can be applied directly after I pushed the same commit = =!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem 👍 Thanks.

@c-bata
Copy link
Member

c-bata commented Apr 24, 2020

/lgtm

@k8s-ci-robot k8s-ci-robot removed the lgtm label Apr 24, 2020
Copy link
Member

@andreyvelich andreyvelich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sperlingxx.
/lgtm
/assign @gaocegege @johnugeorge

Copy link
Member

@gaocegege gaocegege left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

Thanks for your contribution! 🎉 👍

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gaocegege

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot removed the lgtm label Apr 26, 2020
@sperlingxx
Copy link
Member Author

@gaocegege There exists a conflict with latest merged commit. And I've solved it.

@gaocegege
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot merged commit e06cb27 into kubeflow:master Apr 26, 2020
@sperlingxx sperlingxx deleted the add_gamma_for_tpe branch April 26, 2020 08:24
sperlingxx added a commit to sperlingxx/katib that referenced this pull request Jul 9, 2020
* restructure configuration for hyperopt_service

* refine

* refine schema_dict of OptimizerConfiguration

* transfer code style of validation

* remove redundant brackets

* format print style change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants