question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

suggest_uniform throws error using MySQL

See original GitHub issue

Conditions

  • Optuna version:0.10.0
  • Python version:3.7.3
  • OS:Ubuntu 16.04.6

Using MySQL, when reloading a study and optimizing over n_trials>1, an error occurs when optuna processes the suggest_uniform command.

Code to reproduce

import optuna
import numpy as np

def run(trial):

    variable = trial.suggest_uniform(name='Var', low=0, high=1)
    loss = np.random.rand()

    return loss

study = optuna.create_study(study_name="bugReproduce",
                            storage='mysql+pymysql://login:password@host/BugReport',
                            load_if_exists=True)

study.optimize(run, n_trials=10)

Error messages, stack traces, or logs

The first time around everything runs fine

[I 2019-05-07 10:48:58,797] A new study created with name: bugReproduce
[I 2019-05-07 10:48:58,863] Finished trial#0 resulted in value: 0.04761668417531628. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:58,916] Finished trial#1 resulted in value: 0.8832107587679441. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:58,968] Finished trial#2 resulted in value: 0.7393299144453355. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,022] Finished trial#3 resulted in value: 0.29801114852614885. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,074] Finished trial#4 resulted in value: 0.6454763494664104. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,126] Finished trial#5 resulted in value: 0.5779246647098616. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,179] Finished trial#6 resulted in value: 0.7350985202999127. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,231] Finished trial#7 resulted in value: 0.46640685007041527. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,284] Finished trial#8 resulted in value: 0.9331917753402831. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.
[I 2019-05-07 10:48:59,337] Finished trial#9 resulted in value: 0.5997692796594932. Current best value is 0.0476167 with parameters: {'Int': 1, 'Categorical': True, 'Var': 0.190561}.

The second time though the following AttributeError is thrown:

[I 2019-05-07 10:49:00,922] Using an existing study with name 'bugReproduce' instead of creating a new one.
[W 2019-05-07 10:49:00,978] Setting status of trial#10 as TrialState.FAIL because of the following error: AttributeError("'numpy.float64' object has no attribute 'translate'")
Traceback (most recent call last):
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/optuna/study.py", line 399, in _run_trial
    result = func(trial)
  File "bug1.py", line 10, in run
    variable = trial.suggest_uniform(name='Var', low=0, high=1)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/optuna/trial.py", line 151, in suggest_uniform
    return self._suggest(name, distributions.UniformDistribution(low=low, high=high))
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/optuna/trial.py", line 413, in _suggest
    param_value_in_internal_repr, distribution)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/optuna/storages/rdb/storage.py", line 369, in set_trial_param
    commit_success = self._commit_with_integrity_check(session)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/optuna/storages/rdb/storage.py", line 624, in _commit_with_integrity_check
    session.commit()
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1026, in commit
    self.transaction.commit()
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 493, in commit
    self._prepare_impl()
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 472, in _prepare_impl
    self.session.flush()
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2451, in flush
    self._flush(objects)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2589, in _flush
    transaction.rollback(_capture_exception=True)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
    raise value
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2549, in _flush
    flush_context.execute()
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
    rec.execute(self)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
    uow,
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
    insert,
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1120, in _emit_insert_statements
    statement, params
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 988, in execute
    return meth(self, multiparams, params)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
    distilled_params,
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    e, statement, parameters, cursor, context
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1468, in _handle_dbapi_exception
    util.reraise(*exc_info)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
    raise value
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
    cursor, statement, parameters, context
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
    cursor.execute(statement, parameters)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/cursors.py", line 168, in execute
    query = self.mogrify(query, args)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/cursors.py", line 147, in mogrify
    query = query % self._escape_args(args, conn)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/cursors.py", line 127, in _escape_args
    return {key: conn.literal(val) for (key, val) in args.items()}
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/cursors.py", line 127, in <dictcomp>
    return {key: conn.literal(val) for (key, val) in args.items()}
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/connections.py", line 467, in literal
    return self.escape(obj, self.encoders)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/connections.py", line 460, in escape
    return converters.escape_item(obj, self.charset, mapping=mapping)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/converters.py", line 27, in escape_item
    val = encoder(val, mapping)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/converters.py", line 118, in escape_unicode
    return u"'%s'" % _escape_unicode(value)
  File "/home/dbi/merlin/anaconda3/envs/GQA2/lib/python3.7/site-packages/pymysql/converters.py", line 73, in _escape_unicode
    return value.translate(_escape_table)
AttributeError: 'numpy.float64' object has no attribute 'translate'

This occurs when choosing a sufficiently high number of trials. Setting n_trials=10 has consistently allowed me to reproduce the error.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
MerlinDumeurcommented, May 8, 2019

@toshihikoyanase Thank you for the advice! mysqlconnector seems to work on my environment as well.

@sile Thanks for the suggestion. I tend to avoid working with unreleased libraries as they tend to be unstable, however it might prove useful in case there are no alternatives.

0reactions
silecommented, May 8, 2019

FYI

@MerlinDumeur As another workaround different from the above, you can install the latest (unreleased) Optuna via pip install git+https://github.com/pfnet/optuna.git command.

Read more comments on GitHub >

github_iconTop Results From Across the Web

10.12.2 errors.Error Exception - MySQL :: Developer Zone
This exception is the base class for all other exceptions in the errors module. It can be used to catch all errors in...
Read more >
Throw an error preventing a table update in a MySQL trigger
The solution I've found is to simulate a signal throwing a table_name doesn't exist error, pushing a customized error message into the table_name...
Read more >
Common Errors When Using MySQL - O'Reilly
MySQL server has gone away Error. This section also covers the related Lost connection to server during query error. The most common reason...
Read more >
How to Fix the MySQL 1064 Error (5 Methods) - Kinsta
Database errors can throw a wrench in your plans, and may even compromise your website's stability. Knowing how to resolve issues such as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found