Search code examples
rtensorflowkeras

ValueError: in user code while using keras model in R


I am trying to run 1 dimensional CNN in R using keras package. I am using the following code

library(MASS)
library(keras)

##Create some data
data("Boston")
data <- Boston
# create a list of 70% of the rows in the original dataset we can use for training
set.seed(123)
training <- sample(nrow(data), 0.7 * nrow(data))

dataTrain <- data[training,]
dataTest <- data[-training,]

dataTrain_y <- as.matrix(dataTrain$medv)
dataTrain_x <- as.matrix(subset(dataTrain, select = -c(medv)))

dataTest_y <- as.matrix(dataTest$medv)
dataTest_x <- as.matrix(subset(dataTest, select = -c(medv)))

#Reshaping the data for CNN
dataTrain_x <- array_reshape(dataTrain_x, c(ncol(dataTrain_x), nrow(dataTrain_x), 1))
dataTest_x <- array_reshape(dataTest_x, c(ncol(dataTest_x), nrow(dataTest_x), 1))

#CNN model
model <- keras_model_sequential() %>%
  layer_conv_1d(filters=32, kernel_size=4, activation="relu", 
                input_shape=c(ncol(dataTrain_x), nrow(dataTrain_x))) %>%
  layer_max_pooling_1d(pool_size=2) %>%
  layer_conv_1d(filters=64, kernel_size=2, activation="relu") %>%
  layer_max_pooling_1d(pool_size=2) %>%
  layer_dropout(rate=0.4) %>%
  layer_flatten() %>%
  layer_dense(units=100, activation="relu") %>%
  layer_dropout(rate=0.2) %>%
  layer_dense(units=1, activation="linear")

model %>% compile(
  loss = "mse",
  optimizer =  "adam", #'sgd' can also be used
  metrics = list("mean_absolute_error")
)

model %>% summary()

history <- model %>% fit(dataTrain_x, dataTrain_y, 
                         epochs = 100, batch_size = 50, 
                         #callbacks = callback_tensorboard("logs/run_a"),
                         validation_split = 0.2)

But it returns me the following error

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: in user code:
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function  *
        return step_function(self, iterator)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step  **
        outputs = model.train_step(data)
    C:\Python37\lib\site-pac 

Now how can I solve this error?


Solution

  • The error message is truncated, so it's not much help, but it looks to me like you've got some minor typos. If you make the suggested changes (the "###" comments) the model appears to compile and train as expected, e.g.

    library(MASS)
    library(keras)
    
    ##Create some data
    data("Boston")
    data <- Boston
    # create a list of 70% of the rows in the original dataset we can use for training
    set.seed(123)
    training <- sample(nrow(data), 0.7 * nrow(data))
    
    dataTrain <- data[training,]
    dataTest <- data[-training,]
    
    dataTrain_y <- as.matrix(dataTrain$medv)
    dataTrain_x <- as.matrix(subset(dataTrain, select = -c(medv)))
    
    dataTest_y <- as.matrix(dataTest$medv)
    dataTest_x <- as.matrix(subset(dataTest, select = -c(medv)))
    
    #Reshaping the data for CNN
    ### These dimensions don't look correct; switch ncol() with nrow()
    dataTrain_x <- array_reshape(dataTrain_x, c(nrow(dataTrain_x), ncol(dataTrain_x), 1))
    dataTest_x <- array_reshape(dataTest_x, c(nrow(dataTest_x), ncol(dataTest_x), 1))
    
    #CNN model
    model <- keras_model_sequential() %>%
      layer_conv_1d(filters=32, kernel_size=4, activation="relu", 
                    ### The input shape doesn't look correct; instead of 
                    ### `c(ncol(dataTrain_x), nrow(dataTrain_x))` (354, 13)
                    ### I believe you want `dim(dataTest_x)` (13, 1)
                    input_shape=c(ncol(dataTrain_x), 1)) %>%
      layer_max_pooling_1d(pool_size=2) %>%
      layer_conv_1d(filters=64, kernel_size=2, activation="relu") %>%
      layer_max_pooling_1d(pool_size=2) %>%
      layer_dropout(rate=0.4) %>%
      layer_flatten() %>%
      layer_dense(units=100, activation="relu") %>%
      layer_dropout(rate=0.2) %>%
      layer_dense(units=1, activation="linear")
    
    model %>% compile(
      loss = "mse",
      optimizer =  "adam", #'sgd' can also be used
      metrics = list("mean_absolute_error")
    )
    
    model %>% summary()
    #> Model: "sequential"
    #> ________________________________________________________________________________
    #> Layer (type)                        Output Shape                    Param #     
    #> ================================================================================
    #> conv1d_1 (Conv1D)                   (None, 10, 32)                  160         
    #> ________________________________________________________________________________
    #> max_pooling1d_1 (MaxPooling1D)      (None, 5, 32)                   0           
    #> ________________________________________________________________________________
    #> conv1d (Conv1D)                     (None, 4, 64)                   4160        
    #> ________________________________________________________________________________
    #> max_pooling1d (MaxPooling1D)        (None, 2, 64)                   0           
    #> ________________________________________________________________________________
    #> dropout_1 (Dropout)                 (None, 2, 64)                   0           
    #> ________________________________________________________________________________
    #> flatten (Flatten)                   (None, 128)                     0           
    #> ________________________________________________________________________________
    #> dense_1 (Dense)                     (None, 100)                     12900       
    #> ________________________________________________________________________________
    #> dropout (Dropout)                   (None, 100)                     0           
    #> ________________________________________________________________________________
    #> dense (Dense)                       (None, 1)                       101         
    #> ================================================================================
    #> Total params: 17,321
    #> Trainable params: 17,321
    #> Non-trainable params: 0
    #> ________________________________________________________________________________
    
    history <- model %>% fit(dataTrain_x, dataTrain_y, 
                             epochs = 100, batch_size = 50, 
                             #callbacks = callback_tensorboard("logs/run_a"),
                             validation_split = 0.2)
    

    example_1.png

    Created on 2021-07-20 by the reprex package (v2.0.0)