Skip to content

Add share_optimizer option on CmaEsSampler#1776

Closed
c-bata wants to merge 5 commits intooptuna:masterfrom
c-bata:cmaes-non-sharing-optimizer
Closed

Add share_optimizer option on CmaEsSampler#1776
c-bata wants to merge 5 commits intooptuna:masterfrom
c-bata:cmaes-non-sharing-optimizer

Conversation

@c-bata
Copy link
Copy Markdown
Member

@c-bata c-bata commented Sep 1, 2020

Motivation

fix #1775

Description of the changes

  • Add share_optimizer option which enables to store CMA object in system_attrs (default: False).
  • Add randomize_x0 option to sample initial starting points randomly.

Discussion points

Which ones should be set as a default argument, share_optimizer=False or share_optimizer=True?

  • Set share_optimizer=False from the next release: Existing users may be confused by this breaking change. From the next release, CmaEsSampler cannot resume optimizations.
  • Set share_optimizer=True until v3.0.0 release: It keeps current behavior. New users may be confused because this option does not work with PostgreSQL and MySQL.

@c-bata c-bata added bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. needs-discussion Issue/PR which needs discussion. optuna.samplers Related to the `optuna.samplers` submodule. This is automatically labeled by github-actions. labels Sep 1, 2020
@HideakiImamura HideakiImamura self-assigned this Sep 2, 2020
@maartenpants
Copy link
Copy Markdown

maartenpants commented Sep 2, 2020

@HideakiImamura @c-bata Thanks for this! Is there an easy way for me to install this version locally to verify that it fixes the issue?

@HideakiImamura
Copy link
Copy Markdown
Member

@maartenpants You can install this version from the @c-bata's specific branch by pip install git+ssh://git@github.com/c-bata/optuna.git@cmaes-non-sharing-optimizer

@maartenpants
Copy link
Copy Markdown

@c-bata We just encountered the same error again while running with this branch.

[I 2020-09-11 10:35:12,599] Trial 12 pruned. Trial was pruned at epoch 145.
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
    cursor, statement, parameters, context
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(2048)

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/optuna/storages/_rdb/storage.py", line 1104, in _commit
    session.commit()
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1036, in commit
    self.transaction.commit()
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 503, in commit
    self._prepare_impl()
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 482, in _prepare_impl
    self.session.flush()
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2479, in flush
    self._flush(objects)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2617, in _flush
    transaction.rollback(_capture_exception=True)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
    raise value
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2577, in _flush
    flush_context.execute()
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
    rec.execute(self)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
    uow,
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
    insert,
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1137, in _emit_insert_statements
    statement, params
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
    return meth(self, multiparams, params)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
    distilled_params,
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
    e, statement, parameters, cursor, context
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
    raise value.with_traceback(tb)
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
    cursor, statement, parameters, context
  File "/opt/conda/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.DataError
: 
(psycopg2.errors.StringDataRightTruncation) value too long for type character varying(2048)
[SQL: INSERT INTO trial_system_attributes (trial_id, key, value_json) VALUES (%(trial_id)s, %(key)s, %(value_json)s) RETURNING trial_system_attributes.trial_system_attribute_id]
[parameters: {'trial_id': 192, 'key': 'cma:optimizer', 'value_json': '"800363636d6165732e5f636d610a434d410a7100298171017d71022858060000005f6e5f64696d71034b0458080000005f706f7073697a6571044b0858030000005f6d7571054b045807 ... (3224 characters truncated) ... 002e000000000000002f0000000000000030000000000000003100000000000000320000000000000071667471676258080000005f657073696c6f6e7168473e45798ee2308c3a75622e"'}]

@maartenpants
Copy link
Copy Markdown

It only seems to occur after many epochs. CC @HideakiImamura

@c-bata
Copy link
Copy Markdown
Member Author

c-bata commented Sep 11, 2020

Hi @maartenpants! Thank you for checking this branch.
You need to pass a share_optimizer=False argument to CmaEsSampler like:

study = optuna.create_study(
    storage=...,
    sampler=optuna.samplers.CmaEsSampler(share_optimizer=False),
)

@maartenpants
Copy link
Copy Markdown

@c-bata Ah! Thanks

@maartenpants
Copy link
Copy Markdown

maartenpants commented Sep 11, 2020

Will this have any effect on the sampling process? We're running with 4 "workers" to speed up the optimization.

@c-bata c-bata marked this pull request as ready for review September 12, 2020 03:42
@c-bata
Copy link
Copy Markdown
Member Author

c-bata commented Sep 12, 2020

Will this have any effect on the sampling process? We're running with 4 "workers" to speed up the optimization.

Yes.
Each workers cannot share a CMA optimizer object and solutions if passing share_optimizer=False.
So it may lead to the performance degradation on unimodal functions and may lead to the performance improvement on multimodal functions.

@c-bata c-bata added the compatibility Change that breaks compatibility. label Sep 12, 2020
@c-bata
Copy link
Copy Markdown
Member Author

c-bata commented Sep 15, 2020

I opened #1833 for an alternative solution. It has no effects on the sampling process.

@c-bata c-bata closed this Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. compatibility Change that breaks compatibility. needs-discussion Issue/PR which needs discussion. optuna.samplers Related to the `optuna.samplers` submodule. This is automatically labeled by github-actions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot store CMA object on PostgreSQL and MySQL.

3 participants