[409433]: / tests / testthat / test-train.R

Download this file

116 lines (97 with data), 4.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
context("training")
test_that("Sucessful training from a dummy model", {
#testthat::skip_if_not_installed("tensorflow")
testthat::skip_if_not(reticulate::py_module_available("tensorflow"))
# language model
maxlen <- 30
batch_size <- 10
model <- create_model_lstm_cnn(
maxlen = maxlen,
layer_dense = 4,
layer_lstm = 8,
solver = "adam",
vocabulary_size = 4,
compile = TRUE)
trainedNetwork <- train_model(reduce_lr_on_plateau = FALSE,
train_type = "lm",
path = "fasta",
path_val = "fasta",
model = model,
train_val_ratio = 0.2,
steps_per_epoch = 3,
batch_size = batch_size,
epochs = 1)
expect_type(trainedNetwork, "list")
expect_type(trainedNetwork[["metrics"]][["loss"]], "double")
expect_gte(trainedNetwork[["metrics"]][["loss"]], 0)
expect_type(trainedNetwork[["metrics"]][["val_loss"]], "double")
expect_gte(trainedNetwork[["metrics"]][["val_loss"]], 0)
# label folder
model <- create_model_lstm_cnn(
maxlen = maxlen,
kernel_size = c(4,4),
pool_size = c(2,2),
filters = c(2,4),
layer_dense = 2,
layer_lstm = 8,
solver = "adam",
vocabulary_size = 4,
compile = TRUE)
trainedNetwork <- train_model(reduce_lr_on_plateau = FALSE,
train_type = "label_folder",
path = rep("fasta", 2),
path_val = rep("fasta", 2),
model = model,
vocabulary_label = c("A", "B"),
train_val_ratio = 0.2,
steps_per_epoch = 3,
batch_size = batch_size,
epochs = 1)
expect_type(trainedNetwork, "list")
expect_type(trainedNetwork[["metrics"]][["loss"]], "double")
expect_gte(trainedNetwork[["metrics"]][["loss"]], 0)
expect_type(trainedNetwork[["metrics"]][["val_loss"]], "double")
expect_gte(trainedNetwork[["metrics"]][["val_loss"]], 0)
# train/val split with csv
maxlen <- 3
model <- create_model_lstm_cnn(
maxlen = maxlen,
layer_dense = 2,
layer_lstm = 6,
solver = "adam",
vocabulary_size = 4,
compile = TRUE)
files <- as.list(list.files(c("fasta_2", "fasta"), full.names = TRUE))
# target csv
A <- sample(c(0,1), length(files), replace = TRUE)
B <- ifelse(A == 0, 1, 0)
target_df <- data.frame(file = basename(unlist(files)), A = A, B = B)
target_from_csv <- tempfile(fileext = ".csv")
write.csv(target_df, target_from_csv, row.names = FALSE)
# train/val csv
train_val_split_csv <- tempfile(fileext = ".csv")
ttv_df <- data.frame(file = basename(unlist(files)),
type = rep(c("train", "validation"), each = 2))
train_files <- ttv_df$file[ttv_df$type == "train"]
val_files <- ttv_df$file[ttv_df$type == "validation"]
write.csv(ttv_df, train_val_split_csv, row.names = FALSE)
path_file_log <- tempfile(fileext = ".csv")
trainedNetwork <- train_model(reduce_lr_on_plateau = FALSE,
train_type = "label_csv",
path = files,
path_val = files,
model = model,
vocabulary_label = c("A", "B"),
train_val_ratio = 0.2,
steps_per_epoch = 3,
batch_size = 4,
max_samples = 2,
target_from_csv = target_from_csv,
train_val_split_csv = train_val_split_csv,
path_file_log = path_file_log,
epochs = 2)
file_log <- read.csv(path_file_log)
train_file_log <- unique(basename(file_log[,1]))
expect_true(all(sort(train_file_log) == sort(train_files)))
expect_true(all(sort(train_file_log) != sort(val_files)))
})