4

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?

0

1 Answer 1

2
+50

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)

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you now the model runs as you have said. But when I try to run further code like model %>% evaluate(dataTrain_x, dataTrain_y, verbose = 0) or model %>% predict(dataTest_x), it gives error like Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Data cardinality is ambiguous: x sizes: 13 y sizes: 354 Make sure all arrays contain the same number of samples. Can you please check it?
Both of those commands work as expected on my system: evaluate() returns "loss 103.927383" and "mean_absolute_error 7.386703" and predict() returns a column of probabilities. Is this a problem with your 'actual' data, and/or with the Boston example data? Perhaps remove all objects from the environment and try again?
I have copied your full code and ran it. It is now running. But I am getting different loss and mean_absolute_error. Why this may be happening? Can you explain how to decide the input_shape?
You get different values for loss/MAE because your model is different to mine. This behaviour is expected, because CNN training is stochastic (machinelearningmastery.com/stochastic-in-machine-learning). The input_shape is explained: rdocumentation.org/packages/keras/versions/2.4.0/topics/…. Basically, it's the "Dimensionality of the input not including the samples axis" , so for your array (354, 13, 1) you exclude the samples axis (354) and you have (13, 1) (13 vectors of 1-dimensional vectors). See baeldung.com/cs/ml-understanding-dimensions-cnn
Thank you very much for the informative comment. There is no way like using set.seed() to get the same output for the same input set?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.