You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
tune.choice, tune.qrandint not compatible with keras
I wanted to do hparam search based on tune functionalities. Unfortunately, usage of tune built-in methods did not work, for neurons it raised TypeError: int() argument must be a string, a bytes-like object or a number, not 'Integer', when I replaced function with simple int, debugger stumbled upon tune.choice next: TypeError: Expected float32, got <ray.tune.sample.Categorical object at 0x7fe7d1835a90> of type 'Categorical' instead.
The error reproduced on the script with mnist data, which I specially prepared and in the clean environment with newly installed ray. However, everything worked, when I launched hyperparameter hp for config creation:
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "3,4,5"
os.environ["TF_XLA_FLAGS"] = "--tf_xla_cpu_global_jit"
# loglevel : 0 all printed, 1 I not printed, 2 I and W not printed, 3 nothing printed
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
from tensorflow import keras
import ray
from ray import tune
from ray.tune.schedulers import AsyncHyperBandScheduler
from ray.tune.suggest.hyperopt import HyperOptSearch
from ray.tune.suggest import ConcurrencyLimiter
ray.init(configure_logging=False)
EPOCHS = 20
num_samples = 100
experiment_name = "test_1"
config = {
"learning_rate": tune.qloguniform(1e-4, 1e-1, 5e-5),
"batch_size": tune.choice([32, 64, 128, 256]),
"neurons1": tune.qrandint(32, 1024, 32),
"neurons2": tune.qrandint(32, 1024, 32),
"dropout": tune.choice([0.1, 0.2, 0.3,]),
}
class TuneReporter(keras.callbacks.Callback):
"""Tune Callback for Keras."""
def on_epoch_end(self, epoch, logs=None):
tune.report(keras_info=logs, val_loss=logs['val_loss'], val_accuracy=logs["val_accuracy"])
def trainer(config):
# Load MNIST dataset as NumPy arrays
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess the data
x_train = x_train.reshape(-1, 784).astype('float32') / 255
x_test = x_test.reshape(-1, 784).astype('float32') / 255
model = keras.Sequential([
keras.layers.Dense(config["neurons1"], input_shape=(784,), activation='relu', name='dense_1'),
keras.layers.Dropout(config['dropout']),
keras.layers.Dense(config["neurons2"], activation='relu', name='dense_2'),
keras.layers.Dense(10, activation='softmax', name='predictions'),
])
model.compile(optimizer=optimizers.Adam(learning_rate = config['learning_rate']),
loss=keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
earlystopping = keras.callbacks.EarlyStopping(monitor="val_loss",
patience=10,
min_delta=1e-4,
mode='min',
restore_best_weights=True,
verbose=1)
tunerrep = TuneReporter()
callbacks_ = [earlystopping, tunerrep,]
history = model.fit(
x_train,
y_train,
batch_size=config["batch_size"],
validation_data=(x_test, y_test),
epochs=EPOCHS,
callbacks=callbacks_)
return history
scheduler = AsyncHyperBandScheduler(time_attr='training_iteration',
metric="val_loss",
mode="min",
grace_period=10)
#Use bayesian optimisation with TPE implemented by hyperopt
search_alg = HyperOptSearch(config,
metric="val_loss",
mode="min",
)
search_alg = ConcurrencyLimiter(search_alg, max_concurrent=4)
analysis = tune.run(trainer,
verbose=1,
local_dir="ray_results",
name=experiment_name,
num_samples=num_samples,
search_alg=search_alg,
scheduler=scheduler,
raise_on_failed_trial=False,
resources_per_trial={"cpu": 2, "gpu": 1},
log_to_file=("stdout.log", "stderr.log"),
fail_fast=True,
)
best_config = analysis.get_best_config(metric="val_loss", mode='min')
print(f'Best config: {best_config}')
Here is the partial log of two errors:
Traceback (most recent call last):
(pid=82997) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/threading.py", line 916, in _bootstrap_inner
(pid=82997) self.run()
(pid=82997) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 246, in run
(pid=82997) raise e
(pid=82997) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 227, in run
(pid=82997) self._entrypoint()
(pid=82997) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 290, in entrypoint
(pid=82997) self._status_reporter.get_checkpoint())
(pid=82997) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 497, in _trainable_func
(pid=82997) output = train_func(config)
(pid=82997) File "test_ray.py", line 49, in trainer
(pid=82997) keras.layers.Dense(config["neurons1"], input_shape=(784,), activation='relu', name='dense_1'),
(pid=82997) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py", line 1081, in __init__
(pid=82997) self.units = int(units) if not isinstance(units, int) else units
(pid=82997) TypeError: int() argument must be a string, a bytes-like object or a number, not 'Integer'
------------------------------------------------------------
(pid=84324) Traceback (most recent call last):
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/threading.py", line 916, in _bootstrap_inner
(pid=84324) self.run()
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 246, in run
(pid=84324) raise e
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 227, in run
(pid=84324) self._entrypoint()
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 290, in entrypoint
(pid=84324) self._status_reporter.get_checkpoint())
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/ray/tune/function_runner.py", line 497, in _trainable_func
(pid=84324) output = train_func(config)
(pid=84324) File "test_ray.py", line 50, in trainer
(pid=84324) keras.layers.Dense(10, activation='softmax', name='predictions'),
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
(pid=84324) result = method(self, *args, **kwargs)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 116, in __init__
(pid=84324) self.add(layer)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
(pid=84324) result = method(self, *args, **kwargs)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 203, in add
(pid=84324) output_tensor = layer(self.outputs[0])
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 773, in __call__
(pid=84324) outputs = call_fn(cast_inputs, *args, **kwargs)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py", line 183, in call
(pid=84324) lambda: array_ops.identity(inputs))
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/tf_utils.py", line 59, in smart_cond
(pid=84324) pred, true_fn=true_fn, false_fn=false_fn, name=name)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/smart_cond.py", line 59, in smart_cond
(pid=84324) name=name)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func
(pid=84324) return func(*args, **kwargs)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py", line 1174, in cond
(pid=84324) return cond_v2.cond_v2(pred, true_fn, false_fn, name)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/ops/cond_v2.py", line 83, in cond_v2
(pid=84324) op_return_value=pred)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func
(pid=84324) func_outputs = python_func(*func_args, **func_kwargs)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py", line 179, in dropped_inputs
(pid=84324) rate=self.rate)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func
(pid=84324) return func(*args, **kwargs)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/ops/nn_ops.py", line 4289, in dropout
(pid=84324) return dropout_v2(x, rate, noise_shape=noise_shape, seed=seed, name=name)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/ops/nn_ops.py", line 4383, in dropout_v2
(pid=84324) rate, dtype=x.dtype, name="rate")
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1314, in convert_to_tensor
(pid=84324) ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 317, in _constant_tensor_conversion_function
(pid=84324) return constant(v, dtype=dtype, name=name)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 258, in constant
(pid=84324) allow_broadcast=True)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py", line 296, in _constant_impl
(pid=84324) allow_broadcast=allow_broadcast))
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_util.py", line 451, in make_tensor_proto
(pid=84324) _AssertCompatible(values, dtype)
(pid=84324) File "/home/gsukhorukov/.conda/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_util.py", line 331, in _AssertCompatible
(pid=84324) (dtype.name, repr(mismatch), type(mismatch).__name__))
(pid=84324) TypeError: Expected float32, got <ray.tune.sample.Categorical object at 0x7fe7d1835a90> of type 'Categorical' instead.
Ray version and other system information (Python version, TensorFlow version, OS):
ray==1.0.0
tensorflow=2.1.0=gpu_py36h2e5cdaa_0
python=3.6.10=h0371630_0
-os = CentOS Linux 7 (Core)
Reproduction (REQUIRED)
Please provide a script that can be run to reproduce the issue. The script should have no external library dependencies (i.e., use fake or mock data / environments):
If we cannot run your script, we cannot fix your issue.
I have verified my script runs in a clean environment and reproduces the issue.
I have verified the issue also occurs with the latest wheels.
The text was updated successfully, but these errors were encountered:
gregoruar
added
bug
Something that is supposed to be working; but isn't
triage
Needs triage (eg: priority, bug/not-bug, and owning component)
labels
Oct 16, 2020
gregoruar
changed the title
tune.choice, tune.qrandint not compatible with keras [tune]
[tune] tune.choice, tune.qrandint not compatible with keras
Oct 16, 2020
richardliaw
added
P1
Issue that should be fixed within a few weeks
tune
Tune-related issues
and removed
triage
Needs triage (eg: priority, bug/not-bug, and owning component)
labels
Oct 19, 2020
Hi @gregoruar - first thank you for raising this issue and for the exhaustive reproduction script!
The error is due to passing a tune search space directly to HyperOptSearch - until now we only allowed automatic conversion of tune search space when passed as the tune.run(config) parameter.
tune.choice, tune.qrandint not compatible with keras
I wanted to do hparam search based on tune functionalities. Unfortunately, usage of tune built-in methods did not work, for neurons it raised
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Integer'
, when I replaced function with simple int, debugger stumbled upontune.choice
next:TypeError: Expected float32, got <ray.tune.sample.Categorical object at 0x7fe7d1835a90> of type 'Categorical' instead.
The error reproduced on the script with mnist data, which I specially prepared and in the clean environment with newly installed ray. However, everything worked, when I launched hyperparameter hp for config creation:
Here is the script:
Here is the partial log of two errors:
Here is the conda yml env:
Ray version and other system information (Python version, TensorFlow version, OS):
-os = CentOS Linux 7 (Core)
Reproduction (REQUIRED)
Please provide a script that can be run to reproduce the issue. The script should have no external library dependencies (i.e., use fake or mock data / environments):
If we cannot run your script, we cannot fix your issue.
The text was updated successfully, but these errors were encountered: