I'm trying to fit a model in R using Keras. The model seems to be created and compiled without any problems. However, my fit function is giving me the following error:
Error: Data passed to Keras must be a vector, matrix, or array (you passed a data frame)
I've looked at various tutorials and they seem to show the preprocessing very similar to what I'm doing
library(tensorflow)
library(caret)
library(keras)
library(tidyverse)
data <- read.table("wdbc.data", header = FALSE, sep=",", stringsAsFactors = FALSE)
data[data == "?"] <- 0
data[data == "M"] <- 0
data[data == "B"] <- 1
x <- floor(0.6*nrow(data))
set.seed(0)
train_ind <- sample(seq_len(nrow(data)), size = x)
train <- data[train_ind,]
test <- data[-train_ind,]
x_train <- subset(train, select=-c(ID, Class))
y_train <- train[-c(1, 3:32)]
x_test <- subset(test, select=-c(ID, Class))
y_test <- test[-c(1, 3:32)]
max_len <- 3
batch_size <- 64
total_epochs <- 5
set.seed(0)
model <- keras_model_sequential()
model %>%
layer_embedding(5000, 64, name = "embedding1") %>%
layer_simple_rnn(64, return_sequences = TRUE, name = "simpleRNN1") %>%
layer_simple_rnn(64, name = "simpleRNN2") %>%
layer_dense(1, activation = "sigmoid", name = "dense1")
model %>% compile(loss = 'binary_crossentropy',
optimizer = 'adam',
metrics = c('accuracy'))
x_train <- array_reshape(x_train, c(nrow(x_train), 30))
x_test <- array_reshape(x_test, c(nrow(x_test), 30))
trainModel <- model %>% fit(
x = x_train,
y = y_train,
batch_size = batch_size,
epochs = total_epochs,
validation_split = 0.1)
I'm running this in a Jupyter notebook, so trainModel is in it's own cell. When I run that cell, I get the error from above. Here is a more detailed version of the error:
Error: Data passed to Keras must be a vector, matrix, or array (you passed a data frame)
Traceback:
1. model %>% fit(x = x_train, y = y_train, batch_size = batch_size,
. epochs = total_epochs, validation_split = 0.1)
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. eval(quote(`_fseq`(`_lhs`)), env, env)
4. eval(quote(`_fseq`(`_lhs`)), env, env)
5. `_fseq`(`_lhs`)
6. freduce(value, `_function_list`)
7. withVisible(function_list[[k]](value))
8. function_list[[k]](value)
9. fit(., x = x_train, y = y_train, batch_size = batch_size, epochs = total_epochs,
. validation_split = 0.1)
10. fit.keras.engine.training.Model(., x = x_train, y = y_train,
. batch_size = batch_size, epochs = total_epochs, validation_split = 0.1)
11. keras_array(x)
12. stop("Data passed to Keras must be a vector, matrix, or array (you passed a ",
. "data frame)", call. = FALSE)
Data can be found at https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/ The first and second columns were named 'ID' and 'Class'