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

ValueError: Only input tensors may be passed as positional arguments #1428

Open
coltonp2022 opened this issue Apr 8, 2024 · 6 comments
Open

Comments

@coltonp2022
Copy link

coltonp2022 commented Apr 8, 2024

Hi,

I am having trouble getting a model to run. I was able to install all the needed packages, but when I run this section of code.

# Start the model
model <- keras_model_sequential()

# Now define it
model %>%
  layer_dense(units = 50,
              activation = "relu") %>%
  layer_dense(units = 50,
              activation = "relu") %>%
  layer_dense(units = 50,
              activation = "relu") %>%
  layer_dense(units = 1,
              activation = "sigmoid")

I get this error.

ValueError: Only input tensors may be passed as positional arguments. The following argument value should be passed as a keyword argument: <Sequential name=sequential, built=False> (of type <class 'keras.src.models.sequential.Sequential'>) 

Could someone help me understand why it is happening?

@t-kalinowski
Copy link
Member

This happens if both keras and keras3 are loaded. Until this is fixed with an updated release of keras, please uninstall keras to avoid accidentally loading it, and only use keras3.

remove.packages("keras")
install.packages("keras3") # or remotes::install_github("rstudio/keras")

library(keras3) 

@coltonp2022
Copy link
Author

Alright, thank you very much for the help!

@mcgrawcm
Copy link

I have a similar error after a similar simple set of calls in R using keras.

library(tensorflow)
library(keras)

model <- keras_model_sequential()
model %>%
  layer_dense(units = 128, activation = 'relu') %>%
  layer_dense(units = 10, activation = 'softmax')

Error:

Error in py_call_impl(callable, call_args$unnamed, call_args$named) : ValueError: Only input tensors may be passed as positional arguments. The following argument value should be passed as a keyword argument: <Sequential name=sequential_2, built=False> (of type <class 'keras.src.models.sequential.Sequential'>) Run `reticulate::py_last_error()` for details.

I don't have keras3 installed though.
I tried installing keras3 but I will have to update my R version, which I had been avoiding to do at least until I finish the analysis to complete a manuscript I have in progress. Could this be the reason or is it a red herring?

R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS
[1] keras_2.15.0 dplyr_1.1.0 tensorflow_2.16.0.9000 Biobase_2.46.0 BiocGenerics_0.32.0

@jonbry
Copy link

jonbry commented Apr 28, 2024

[1] keras_2.15.0 dplyr_1.1.0 tensorflow_2.16.0.9000 Biobase_2.46.0 BiocGenerics_0.32.0

Were you able to successfully build models with keras previously? I noticed that you're using keras 2.15 and tensorflow 2.16, which may be causing a problem. I think you'll need tensorflow 2.15 to use the latest version of the keras package.

I believe you can run keras::install_keras() it will delete and recreate your r-tensorflow virtualenv and give you the right versions or keras and tensorflow.

@erkanozhan
Copy link

erkanozhan commented May 8, 2024

This happens if both keras and keras3 are loaded. Until this is fixed with an updated release of keras, please uninstall keras to avoid accidentally loading it, and only use keras3.

remove.packages("keras")
install.packages("keras3") # or remotes::install_github("rstudio/keras")

library(keras3) 

keras3 works to solve this problem. (On R 4.4.0)

OliverSchacht added a commit to CausalAIBook/MetricsMLNotebooks that referenced this issue Jul 31, 2024
@mdozmorov
Copy link

Changing the code to

network <- keras_model_sequential(input_shape = c(28 * 28)) %>% 
  layer_dense(units = 512, activation = "relu") %>% 
  layer_dense(units = 10, activation = "softmax")

worked after upgrading to keras3 and doing keras3::install_keras(), on R 4.4.0

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

No branches or pull requests

6 participants