将Keras模型用作Shiny应用程序的一部分

时间:2018-08-24 19:23:04

标签: r shiny keras

所以,我的目标是创建一个Shiny应用程序,该应用程序将用户输入的内容转换为矩阵,然后使用预先保存的Keras模型(基本上是保存的权重)来预测他的某个变量(并打印出来) 。我是Keras和Shiny的新手,所以非常感谢您的帮助。我的程序正确显示了用户输入,但是它正在评估并在出现问题的地方打印输出。任何帮助将不胜感激!代码如下:

server <- function(input, output) {
  hisdata <- matrix(cbind(input$VK_001, input$VK_002, input$VK_003, input$VK_004,input$VK_005,input$VK_006,input$VK_006, input$VK_007, input$VK_008, input$VK_009,input$VK_010,input$VK_011, input$VK_012, input$VK_013,input$VK_015, input$VK_017, input$VK_018, input$VK_021, input$VK_022, input$VK_024, input$VK_025, input$VK_027, input$VK_028, input$VK_030, input$VK_031, input$VK_033, input$VK_034, input$VK_036, input$VK_037, input$VK_039, input$VK_040, input$VK_041))
  test_data <- hisdata
  model <- load_model_hdf5("my_model_bft_003.h5")
  test_predictions <- model %>% predict(test_data)
  output$oid1 <- renderPrint({x <- test_predictions[ , 1]
  print(x)
  })
}
ui <- fluidPage(

  # App title ----
  titlePanel("Shiny + KEras!"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: numeric----

      numericInput("VK_001", "Value:", 10, min = 1),
      numericInput("VK_002", "Value:", 10, min = 1),
      numericInput("VK_003", "Value:", 10, min = 1),
      numericInput("VK_004", "Value:", 10, min = 1),
      numericInput("VK_005", "Value:", 10, min = 1),
      numericInput("VK_006", "Value:", 10, min = 1),
      numericInput("VK_007", "Value:", 10, min = 1),
      numericInput("VK_008", "Value:", 10, min = 1),
      numericInput("VK_009", "Value:", 10, min = 1),
      numericInput("VK_010", "Value:", 10, min = 1),
      numericInput("VK_011", "Value:", 10, min = 1),
      numericInput("VK_012", "Value:", 10, min = 1),
      numericInput("VK_013", "Value:", 10, min = 1),
      numericInput("VK_015", "Value:", 10, min = 1),
      numericInput("VK_017", "Value:", 10, min = 1),
      numericInput("VK_018", "Value:", 10, min = 1),
      numericInput("VK_021", "Value:", 10, min = 1),
      numericInput("VK_022", "Value:", 10, min = 1),
      numericInput("VK_024", "Value:", 10, min = 1),
      numericInput("VK_025", "Value:", 10, min = 1),
      numericInput("VK_027", "Value:", 10, min = 1),
      numericInput("VK_028", "Value:", 10, min = 1),
      numericInput("VK_030", "Value:", 10, min = 1),
      numericInput("VK_031", "Value:", 10, min = 1),
      numericInput("VK_033", "Value:", 10, min = 1),
      numericInput("VK_034", "Value:", 10, min = 1),
      numericInput("VK_036", "Value:", 10, min = 1),
      numericInput("VK_037", "Value:", 10, min = 1),
      numericInput("VK_039", "Value:", 10, min = 1),
      numericInput("VK_040", "Value:", 10, min = 1),
      numericInput("VK_041", "Value:", 10, min = 1)
    ),

  # Main panel for displaying outputs ----
  mainPanel(
    h4('Predicted values'),
    verbatimTextOutput("oid1"))

  )
)
shinyApp(ui = ui, server = server)

所需的输出是值的1x1向量(预测值)。 输入由1x32(1行,32列)的1到10的数值(整数)矩阵组成。

1 个答案:

答案 0 :(得分:0)

有一个R包调用reticulate,使您能够在R中使用python代码。

library(reticulate)

keras <- import("keras")
# '$' in R is equal to '.' in python
# Equivalent to model = keras.models.load_model("my_model_bft_003.h5") in python
model <- keras$models$load_model("my_model_bft_003.h5")
# Equivalent to model.predict(test_data)  in python
test_predictions <- model$predict(test_data)