深度学习,神经网络

时间:2019-05-13 16:13:57

标签: r

我对在分类数据中应用神经网络有疑问。

1-我有一个数字(Connection.Duration)输出

2-我有5个输入,其中4个(EVSE.IDUser.IDFeeDay)是分类的,1(Time)是数字。

我想应用神经网络来预测Connection.Duration。我不知道用于分类数据的正确命令。我使用了model.matrix,但没有继续处理包含分类数据的新数据框(m)。

我想寻求帮助。

data$Fee <- as.factor(data$Fee)
data$EVSE.ID <- as.factor(data$EVSE.ID)
data$User.ID <- as.factor(data$User.ID)
data$Day <- as.factor(data$Day)
data$Time <- as.factor(data$Time)
data$Connection.Duration <- as.factor(data$Connection.Duration)

m <- model.matrix(Connection.Duration ~ EVSE.ID+Time+Day+Fee+User.ID,
              data= data)

# Neural Networks 
n <- neuralnet(Connection.Duration ~ EVSE.ID+Time+Day+Fee+User.ID,
           data = m,
           hidden=c(100,60))

# Data partition 
set.seed(1234)
ind <- sample(2, nrow(m), replace = TRUE, prob = c(0.7, 0.3))
training <- m[ind==1,1:5]
testing <- m[ind==2,1:5]
trainingtarget <- m[ind==1, 6]
testingtarget <- m[ind==2, 6]

# Normalize
m <- colMeans(training)
s <- apply(training, 2, sd)
training <- scale(training, center = m, scale = s)
testing <- scale(testing, center = m, scale = s)

# Create Model
model <- keras_model_sequential()
model %>%
layer_dense(units = 5, activation = 'relu', input_shape = c(5)) %>%
layer_dense(units = 1)

# Compile
model %>% compile(loss= 'mse',
              optimizer= 'rmsprop',
              metrics='mae')
# Fit model 
mymodel <- model %>%

  fit(training,
      trainingtarget,
      epochs= 100,
      batch_size = 32,
      validation_split = 0.2)

# Evaluate 
model %>% evaluate(testing, testingtarget)
pred <- model %>% predict(testing)
mean(testingtarget- pred^2)
plot(testingtarget, pred)

# Fine-tune Model
model <- keras_model_sequential()
model %>%
layer_dense(units = 100, activation = 'relu', input_shape = c(5)) %>%
 layer_dropout(rate = 0.4) %>%
layer_dense(units = 60, activation = 'relu', input_shape = c(5)) %>%
 layer_dropout(rate = 0.2) %>%
 layer_dense(units = 1)

# Compile
model %>% compile(loss= 'mse',
              optimizer= optimizer_rmsprop(lr=0.0001),
              metrics='mae')

# Fit model
mymodel <- model %>%
  fit(training,
      trainingtarget,
      epochs= 100,
      batch_size = 32,
      validation_split = 0.2)

# Evaluate 
model %>% evaluate(testing, testingtarget)
  pred <- model %>% predict(testing)
  mean(testingtarget- pred^2)
  plot(testingtarget, pred)

1 个答案:

答案 0 :(得分:0)

您要查找的内容称为“一种热编码”。 tensorflow / keras中有一些功能可以帮助编码。

但是,否则,我会尝试先做。我不会依赖ofxBaseGui,因为它不能完全满足您的需求。

您可以轻松地编写自己的函数,但这是使用model.matrix包的示例:

mltools

注意:它需要library(data.table) library(mltools) one_hot(data.table(x = factor(letters), n = 1:26)) 而不是data.table,但是您可以来回转换数据。