检查R包keras是否编译了模型

时间:2017-06-15 14:20:57

标签: r rstudio keras

当我运行以下R脚本时,我获得有关keras模型及其添加的层的摘要信息,但没有确认模型已编译。如何检查编译步骤是否已完成?

library(keras)

model <- keras_model_sequential()

model %>% 
  layer_dense(units = 64, activation = 'relu', input_shape = c(20)) %>% 
  layer_dropout(rate = 0.5) %>% 
  layer_dense(units = 64, activation = 'relu') %>% 
  layer_dropout(rate = 0.5) %>% 
  layer_dense(units = 10, activation = 'softmax') %>%
  compile(
    loss = 'categorical_crossentropy',
    optimizer = optimizer_sgd(lr = 0.01, decay = 1e-6, 
    momentum = 0.9, nesterov = TRUE),
    metrics = c('accuracy')     
    )

summary(model)

1 个答案:

答案 0 :(得分:3)

检查内置标志?

library(keras)
model <- keras_model_sequential()

model$built # False

model %>% 
  layer_dense(units = 64, activation = 'relu', input_shape = c(20)) %>% 
  layer_dropout(rate = 0.5) %>% 
  layer_dense(units = 64, activation = 'relu') %>% 
  layer_dropout(rate = 0.5) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 10) %>% 
  layer_activation(activation = 'softmax')

model$built # False

model %>%
  compile(
    loss = 'categorical_crossentropy',
    optimizer = optimizer_sgd(lr = 0.01, decay = 1e-6, 
                          momentum = 0.9, nesterov = TRUE),
    metrics = c('accuracy')     
  )

model$built # True