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

TypeError: 'float' object cannot be interpreted as an integer #52

Closed
Ocalak opened this issue Apr 3, 2024 · 5 comments · Fixed by #53
Closed

TypeError: 'float' object cannot be interpreted as an integer #52

Ocalak opened this issue Apr 3, 2024 · 5 comments · Fixed by #53

Comments

@Ocalak
Copy link

Ocalak commented Apr 3, 2024

Hey all,

I try to implement FNN model and simple hyper-parameter tuning. Here is the code :

  model <- keras_model_sequential()
  
  # Define the range of hyperparameters
  units <- hp$Int("units", min_value = 32, max_value = 512, step = 32)
  dropout_rate <- hp$Int("dropout", min_value = 0, max_value = 0.5, step = 0.1)
  
  # Add layers to the model
  model %>%
    layer_dense(units = units, activation = "relu", input_shape = ncol(mtrainx),
                kernel_regularizer = regularizer_l1(0.01)) %>%
    layer_batch_normalization() %>%
    layer_dense(units = units, activation = "relu",
                kernel_regularizer = regularizer_l1(0.01)) %>%
    layer_batch_normalization() %>%
    layer_dense(units = units, activation = "relu",
                kernel_regularizer = regularizer_l1(0.01)) %>%
    layer_batch_normalization() %>%
    layer_dense(units = 1)
  
  # Compile the model
  model %>% compile(
    optimizer = tf$keras$optimizers$legacy$Adam(
      learning_rate = hp$Int("learning_rate", min_value = 1e-4, max_value = 1e-2, sampling = "log"),
      beta_1 = 0.9,
      beta_2 = 0.999
    ),
    loss = "mse",
    metrics = list("mae")
  )
  
  return(model)
}

# Initialize the tuner
tuner <- RandomSearch(
  hypermodel = build_model,
  objective = "val_loss",
  max_trials = 1,
  executions_per_trial = 1,
  directory = "my_dir",
  project_name = "my_project"
)
tuner %>% search_summary()

tuner %>% fit_tuner(mtrainx,mtrainy,
                    epochs = 5, validation_data = list(valx,valy)))

Then I got the following error:

Traceback (most recent call last):
  File "/Users/ocalkaptan/.virtualenvs/r-tensorflow/lib/python3.9/site-packages/keras_tuner/src/engine/base_tuner.py", line 274, in _try_run_and_update_trial
    self._run_and_update_trial(trial, *fit_args, **fit_kwargs)
  File "/Users/ocalkaptan/.virtualenvs/r-tensorflow/lib/python3.9/site-packages/keras_tuner/src/engine/base_tuner.py", line 239, in _run_and_update_trial
    results = self.run_trial(trial, *fit_args, **fit_kwargs)
  File "/Users/ocalkaptan/.virtualenvs/r-tensorflow/lib/python3.9/site-packages/keras_tuner/src/engine/tuner.py", line 306, in run_trial
    for execution in range(self.executions_per_trial):
TypeError: 'float' object cannot be interpreted as an integer

Search: Running Trial #3

Value             |Best Value So Far |Hyperparameter
224               |224               |units
0                 |0                 |dropout
0.0010637         |0.0010637         |learning_rate

Error in py_call_impl(callable, call_args$unnamed, call_args$named) : 
  RuntimeError: Number of consecutive failures exceeded the limit of 3.
Traceback (most recent call last):
  File "/Users/ocalkaptan/.virtualenvs/r-tensorflow/lib/python3.9/site-packages/keras_tuner/src/engine/base_tuner.py", line 274, in _try_run_and_update_trial
    self._run_and_update_trial(trial, *fit_args, **fit_kwargs)
  File "/Users/ocalkaptan/.virtualenvs/r-tensorflow/lib/python3.9/site-packages/keras_tuner/src/engine/base_tuner.py", line 239, in _run_and_update_trial
    results = self.run_trial(trial, *fit_args, **fit_kwargs)
  File "/Users/ocalkaptan/.virtualenvs/r-tensorflow/lib/python3.9/site-packages/keras_tuner/src/engine/tuner.py", line 306, in run_trial
    for execution in range(self.executions_per_trial):
TypeError: 'float' object cannot be interpreted as an integer

Run `reticulate::py_last_error()` for details. ```




Thanks!
@turgut090
Copy link
Member

Hello, can you check this part and make changes?

 units <- hp$Int("units", min_value = 32L, max_value = 512L, step = 32L)

@turgut090 turgut090 linked a pull request Apr 13, 2024 that will close this issue
Merged
@Ocalak
Copy link
Author

Ocalak commented Apr 15, 2024

Hello Turgut, thanks for your answer. I also tried changing hp$Int to hp$Float but I didn't work. I am still having the same error.

Hello, can you check this part and make changes?

 units <- hp$Int("units", min_value = 32L, max_value = 512L, step = 32L)

@turgut090
Copy link
Member

Hello Turgut, thanks for your answer. I also tried changing hp$Int to hp$Float but I didn't work. I am still having the same error.

Hello, can you check this part and make changes?

 units <- hp$Int("units", min_value = 32L, max_value = 512L, step = 32L)

Please, reinstall/update package and let me know.

@Ocalak
Copy link
Author

Ocalak commented May 1, 2024

Hello Turgut, thanks for your answer. I also tried changing hp$Int to hp$Float but I didn't work. I am still having the same error.

Hello, can you check this part and make changes?

 units <- hp$Int("units", min_value = 32L, max_value = 512L, step = 32L)

Please, reinstall/update package and let me know.

I did that many times too.
I have encountered the same error while using the example code here "https://eagerai.github.io/kerastuneR/" .

Traceback (most recent call last): File "/opt/anaconda3/envs/tf_env_/lib/python3.8/site-packages/keras_tuner/engine/base_tuner.py", line 270, in _try_run_and_update_trial self._run_and_update_trial(trial, *fit_args, **fit_kwargs) File "/opt/anaconda3/envs/tf_env_/lib/python3.8/site-packages/keras_tuner/engine/base_tuner.py", line 235, in _run_and_update_trial results = self.run_trial(trial, *fit_args, **fit_kwargs) File "/opt/anaconda3/envs/tf_env_/lib/python3.8/site-packages/keras_tuner/engine/tuner.py", line 279, in run_trial for execution in range(self.executions_per_trial): TypeError: 'float' object cannot be interpreted as an integer
` ▆

  1. ├─tuner %>% ...
  2. └─kerastuneR::fit_tuner(...)
  3. ├─base::do.call(tuner$search, args)
  4. └─reticulate (local) <python.builtin.method>(...)
  5. └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)
    

See reticulate::py_last_error()$r_trace$full_call for more details.`

@turgut090
Copy link
Member

Please, change code to this:

'''

tuner <- RandomSearch(
hypermodel = build_model,
objective = "val_loss",
max_trials = 1L,
executions_per_trial = 1L,
directory = "my_dir",
project_name = "my_project"
)
'''

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

Successfully merging a pull request may close this issue.

2 participants