Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

Problem while creating a keras model in R

I was searching for a solution on this great forum, but I didn't find it yet, so I am now writing what's my problem.

I am trying to dig into neural networks with R, by using multiple packages. The problem is the one from ...

[https://www.kaggle.com/c/titanic/overview]

With keras, I find a problem in the fitting step.

The model I built:

data$PassengerId <- NULL
data$Name <- NULL
data$Ticket <- NULL
data$Cabin <- NULL

library(caTools)
split = sample.split(data$Survived, SplitRatio = 0.75)
train_data <- subset(data, split==TRUE)
test_data <- subset(data, split==FALSE)

train_keras <- train_data
test_keras <- test_data

train_keras$Survived <- as.numeric(train_keras$Survived)
train_keras$Pclass <- as.numeric(train_keras$Pclass) 
train_keras$Sex <- as.numeric(train_keras$Sex) 
train_keras$Embarked <- as.numeric(train_keras$Embarked)
train_keras$Survived <- train_keras$Survived -1

test_keras$Survived <- as.numeric(test_keras$Survived)
test_keras$Pclass <- as.numeric(test_keras$Pclass) 
test_keras$Sex <- as.numeric(test_keras$Sex) 
test_keras$Embarked <- as.numeric(test_keras$Embarked) 
test_keras$Survived <- test_keras$Survived -1

train_keras[,-1] <- scale(train_keras[,-1])
test_keras[,-1] <- scale(test_keras[,-1])

library(keras)
library(tidyverse)

X_train <- train_keras %>% 
  select(-Survived)
y_train <- to_categorical(train_keras$Survived)

X_test <- test_keras %>% 
  select(-Survived)
y_test <- to_categorical(test_keras$Survived)

classifier <- keras_model_sequential() 

classifier %>% 
  layer_dense(units = 256, activation = 'relu', input_shape = ncol(X_train)) %>% 
  layer_dropout(rate = 0.4) %>% 
  layer_dense(units = 128, activation = 'relu') %>%
  layer_dropout(rate = 0.3) %>%
  layer_dense(units = 2, activation = 'sigmoid')

history <- classifier %>% compile(
  loss = 'binary_crossentropy',
  optimizer = 'adam',
  metrics = c('accuracy')
)

So, after cleaning the data, dividing it into test and train, typing it correctly, I build the model in keras. Until now, no problem. The problem, as I said, it was in the moment of fitting this model.

classifier %>% fit(
  X_train, y_train, 
  epochs = 100, 
  batch_size = 5,
  validation_split = 0.2
)

This raises the error:

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code:

Digging deeper, I see something strange, pointing at the dimensions on the sequential model.

assert_input_compatibility ' input tensors. Inputs received: ' + str(inputs)) ValueError: Layer sequential_12 expects 1 inputs, but it received 7 input tensors. Inputs received: [<tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=float32>

If I look even deeper, I see that the whole architecture was defined with tensors of (None,1) shape, which (and sorry for my lack of experience on this topic) for me is strange. I know the problem is the way I feed the model, but I still don't know when do I have the problems.

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...