R中的循环管道操作符



Im目前正在R中使用keras构建深度神经网络。每次我想在网络中添加一层时,我都必须手动添加,如下所示:

型号%>%

layer_dense(units=5,activation = "relu",input_shape = c(4))%>%
layer_dense(units=5,activation = "relu",input_shape = c(4))%>%
layer_dense(units=100,activation = "relu",input_shape = c(4))%>%

但是,如果我想要任意大的网络,这是不方便的。有没有一种方法可以"循环"管道操作员?也就是说,如果我指定了我想要的深度,我可以构建一个循环,将层添加到这个数量吗?

实际上,它非常直接:

model<-keras_model_sequential() 
for(i in 1:Depth){   
model %>%
layer_dense(units=5,activation = "relu",input_shape = c(4)) %>% 
layer_dense(units=Height,activation = "relu",input_shape = c(4))
}   
model %>% layer_dense(units=1)

最新更新