philippmuench
12/23/2019 - 3:46 PM

functional api

functional api

trainMinimalFunctionalAPI <- function(path = "example_files/fasta") {
  
  message("Initialize model! This can take a few minutes.")

  input <- keras::layer_input(batch_shape = c(256, 50, 6))
  
  cnn <- 
    keras::layer_conv_1d(
      object = input,
      kernel_size = 3,
      # 3 charactes are representing a codon
      padding = "same",
      activation = "relu",
      filters = 81
    )
    pool = keras::layer_max_pooling_1d(object = cnn, pool_size = 3)
    norm = keras::layer_batch_normalization(object = pool, momentum = .8)
    lstm = keras::layer_cudnn_lstm(object = norm, 512)
    dense = keras::layer_dense(object = lstm, 6)
    output = keras::layer_activation(object = dense, "softmax")
  model <- keras::keras_model(input, output)
  
  summary(model)
  
  model %>% keras::compile(loss = "categorical_crossentropy",
                           optimizer = "adam",
                           metrics = c("acc"))
  

  gen <-
    fastaFileGenerator(
      corpus.dir = path,
      batch.size = 256,
      maxlen = 50,
      step = 1,
      randomFiles = FALSE,
      seqStart = "l",
      seqEnd = "l",
      withinFile =  "p",
      vocabulary = c("l", "p", "a", "c", "g", "t")
    )
 

  message("Start training ...")
  history <-
    model %>% keras::fit_generator(
      generator = gen,
      steps_per_epoch = 100,
      max_queue_size = 100,
      epochs = 10
      )
  
}