'R Programming Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Data cardinality is ambiguous: x sizes:13704 y sizes: 5710

I am trying to learn deep learning using R programming. But stuck with this error. Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Data cardinality is ambiguous: x sizes: 13704 y sizes: 5710 Make sure all arrays contain the same number of samples.

Code:

# Libraries required
install_tensorflow(method = "auto")
library(keras)
library(tensorflow)
library(caret)
library(reticulate)
library(stringr)

data <- fnd2
head(data)
x <- data[,c(2:4)]    
y <- data[,5]       

head(x)
head(y)

train_data_percentage <- 0.75
train_number <- round(nrow(x)*train_data_percentage)  
set.seed(1234)

index <- sample( c(1:nrow(data)), train_number, replace = F )

train_x <- as.matrix(x[index,])
train_y <- as.matrix(y[index])

test_x <- as.matrix(x[-index,])
test_y <- as.matrix(y[-index])

length(train_x); length(test_x); length(train_y); length(test_y)

table(train_y)
table(test_y)

# Padding sequences
train_x <- str_pad(train_x, width = 0, pad = " ")
test_x <- str_pad(test_x, width = 0, pad = " ")

# Model
model <- keras_model_sequential() 
model %>% 
  layer_embedding(input_dim = 500, output_dim = 32) %>%
  layer_simple_rnn(units = 32) %>%  
  layer_dense(units = 1, activation = "sigmoid")

# Compile
model %>% compile(optimizer = "rmsprop",
              loss = "binary_crossentropy",
              metrics = c("acc"))

# Fit model
history <- model %>% fit(train_x, train_y,
                     epochs = 25,
                     batch_size = 128,
                     validation_split = 0.2)

After executing fit model part, this error pops up. Can someone please help? Thanks..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source