rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
832 stars 283 forks source link

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

Open coltonp2022 opened 4 months ago

coltonp2022 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

Alright, thank you very much for the help!

mcgrawcm commented 4 months ago

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 commented 4 months ago

[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 commented 3 months ago

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) 

keras3works to solve this problem. (On R 4.4.0)