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

compile function does not work #1448

Closed
Mhrbnn opened this issue May 20, 2024 · 3 comments
Closed

compile function does not work #1448

Mhrbnn opened this issue May 20, 2024 · 3 comments

Comments

@Mhrbnn
Copy link

Mhrbnn commented May 20, 2024

I have tried building keras model and then I got this problem which I cannot solve. Can you provide solution about this problem?

model <- compile(model, loss = loss, optimizer = optimizer, metrics = metrics)
Error in UseMethod("compile") :
no applicable method for 'compile' applied to an object of class "c('keras.src.models.sequential.Sequential', 'keras.models.sequential.Sequential', 'keras.src.models.model.Model', 'keras.models.model.Model', 'keras.src.backend.tensorflow.trainer.TensorFlowTrainer', 'keras.backend.tensorflow.trainer.TensorFlowTrainer', 'keras.src.trainers.trainer.Trainer', 'keras.trainers.trainer.Trainer', 'keras.src.layers.layer.Layer', 'keras.layers.layer.Layer', 'keras.src.backend.tensorflow.layer.TFLayer', 'keras.backend.tensorflow.layer.TFLayer', 'keras.src.backend.tensorflow.trackable.KerasAutoTrackable', 'keras.backend.tensorflow.trackable.KerasAutoTrackable', 'tensorflow.python.trackable.autotrackable.AutoTrackable', 'tensorflow.python.trackable.base.Trackable', 'keras.src.ops.operation.Operation', 'keras.ops.operation.Operation', 'keras.src.saving.keras_saveable.KerasSaveable', 'keras.saving.keras_saveable.KerasSaveable', 'python.builtin.object')"

@t-kalinowski
Copy link
Member

I believe this was fixed with the last release of keras. Can you please run update.packages()? Does the issue go away?

@Mhrbnn
Copy link
Author

Mhrbnn commented May 20, 2024

#install packages for deep learning model
install.packages("keras")
install.packages("tensorflow")
library(tensorflow)
tensorflow::install_tensorflow()
library(keras)
install.packages("keras")
install.packages("tensorflow")
library(tensorflow)
tensorflow::install_tensorflow()
library(keras)
update.packages()

Define input shape for timesteps and features

input_shape <- c(timesteps, features)

Create the input layer

inputs <- layer_input(shape = input_shape)

Define input shape

timesteps <- 10 # Replace with your actual timestep value
features <- 20 # Replace with your actual feature count

Define the model sequence

model <- keras_model_sequential()
model <- keras_model(
sequential() %>%
# LSTM layer with 64 units, returning sequences, and specified input shape
layer(lstm(units = 64, return_sequences = TRUE, input_shape = c(timesteps, features))) %>%
# LSTM layer with 32 units
layer(lstm(units = 32)) %>%
# Flatten layer
layer(flatten()) %>%
# Dense layer with 1 unit (output layer)
layer(dense(units = 1))
)

Compile the model (assuming you have defined loss and optimizer functions)

model %>% compile(loss = loss_function, optimizer = optimizer)

thank you so much for answer my codes create problem :> model <- keras_model(

  • sequential() %>%
  • # LSTM layer with 64 units, returning sequences, and specified input shape
    
  • layer(lstm(units = 64, return_sequences = TRUE, input_shape = c(timesteps, features))) %>%
    
  • # LSTM layer with 32 units
    
  • layer(lstm(units = 32)) %>%
    
  • # Flatten layer
    
  • layer(flatten()) %>%
    
  • # Dense layer with 1 unit (output layer)
    
  • layer(dense(units = 1))
    
  • )
    Error in layer(., dense(units = 1)) : could not find function "layer"

Compile the model (assuming you have defined loss and optimizer functions)

model %>% compile(loss = loss_function, optimizer = optimizer)
Error in UseMethod("compile") :
no applicable method for 'compile' applied to an object of class "c('keras.src.models.sequential.Sequential', 'keras.models.sequential.Sequential', 'keras.src.models.model.Model', 'keras.models.model.Model', 'keras.src.backend.tensorflow.trainer.TensorFlowTrainer', 'keras.backend.tensorflow.trainer.TensorFlowTrainer', 'keras.src.trainers.trainer.Trainer', 'keras.trainers.trainer.Trainer', 'keras.src.layers.layer.Layer', 'keras.layers.layer.Layer', 'keras.src.backend.tensorflow.layer.TFLayer', 'keras.backend.tensorflow.layer.TFLayer', 'keras.src.backend.tensorflow.trackable.KerasAutoTrackable', 'keras.backend.tensorflow.trackable.KerasAutoTrackable', 'tensorflow.python.trackable.autotrackable.AutoTrackable', 'tensorflow.python.trackable.base.Trackable', 'keras.src.ops.operation.Operation', 'keras.ops.operation.Operation', 'keras.src.saving.keras_saveable.KerasSaveable', 'keras.saving.keras_saveable.KerasSaveable', 'python.builtin.object')"

@Mhrbnn
Copy link
Author

Mhrbnn commented May 21, 2024

Thank you for helping, I got solution based on your recommendation.

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

2 participants