我想构建一个处理分类和数字输入层的深层神经网络。我有三个具有多个级别(300+)的分类变量和三个只有几个级别的分类变量。
我想为每个类别变量制作一个嵌入层,以减小尺寸大小并提高预测性能。我将所有类别级别编码为二进制变量。我在一个概念模型中形象化了我的想法:Conceptual model
但是,我正在努力实现我的网络体系结构,并且对此结构是否有意义感到怀疑。我有多个输出变量。
我用分类和数字输入做了一个可复制的例子。实际上,我的变量有更多的层次。
library(dplyr)
library(keras)
library(caret)
#Load the fictional data
data(cars)
#Set as factor
for(i in 3:18){
cars[,i] <- as.factor(cars[,i])
}
#Create numerical variables
cars$weight <-runif( 804, 500.0, 700.5)
cars$heigth <-runif( 804, 1.0, 2.0)
#Select Y
Y <- cars[,1:2]
#Categorical X
X_cat <- cars[,-c(1,2)] %>% select_if(.,negate(is.numeric))
#Numerical X
X_num <- cars[,-c(1,2)] %>% select_if(.,is.numeric)
#Format categorical variables to one hot
dmy <- caret::dummyVars(" ~ .", data = X_cat,fullRank=FALSE)
X_cat <- data.frame(predict(dmy, newdata = X_cat))
#Categorical input
cat_layer <- layer_input(shape = 33, name = 'cat_input') %>% layer_embedding(input_dim = 33, output_dim = 16,trainable = TRUE) %>% layer_dense(units=50, activation= "relu", trainable = TRUE) %>% layer_reshape(target_shape = 50,trainable = TRUE,name="cat_layer")
#Numerical input
num_layer <- layer_input(shape = 2, name = 'num_input') %>% layer_dense(units=50, activation= "relu",input_shape = 2, trainable = TRUE) %>% layer_reshape(target_shape = 50,trainable = TRUE,name="num_layer")
#make model
model_keras <- keras_model_sequential() %>%
#COncatenate both inputs
layer_concatenate(inputs=c(cat_layer,num_layer),trainable = TRUE) %>%
#Only one hidden layer
layer_dense(
units = 2,
kernel_initializer = "normal",
activation = "relu",
input_shape = 50) %>%
#Compile model
compile(
optimizer = 'adam',
loss = 'mean_absolute_percentage_error',
metrics = 'mean_absolute_percentage_error'
)
#Fit the model
model_keras %>% fit(list(cat_input, num_input), epochs = 100)
我知道需要嵌入层。但是,由于数值和类别变量的大小不同,我很费劲。