-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment-2-helpers.R
83 lines (72 loc) · 3.12 KB
/
assignment-2-helpers.R
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
use_multi_cpu <- function(threads) {
library(tensorflow)
library(keras)
k_clear_session()
config <- tf$ConfigProto(intra_op_parallelism_threads = threads, inter_op_parallelism_threads = threads)
session = tf$Session(config=config)
k_set_session(session)
}
load_sunspots_dataset <- function() {
data = read.table("data/SN_m_tot_V2.0.csv", sep=";", header=F)
data = data.frame(data[, 1], data[, 2], data[, 4])
names(data) <- c("Year", "Month", "Mean monthly sunspots")
data
}
create_sequences_x_y <- function(data, sequence_length, target_shift, step_shift) {
data <- as.matrix(data)
start_index = 1
end_index = dim(data)[1]
# We assume that the input is in legal ranges
elements = end_index - start_index + 1
# our targets are a single data point
target_len = 1
single_sequence_length = sequence_length + target_shift + target_len
number_of_sequences = floor((elements - single_sequence_length)/step_shift) + 1
# Initialise variables we need in the loop
# We store the index which we should start with in each loop in current_start_index
current_start_index = start_index
sequence_x <- array(0, dim = c(number_of_sequences, sequence_length, dim(data)[2]))
sequence_y <- array(0, dim = c(number_of_sequences, dim(data)[2]))
for (sequence_index in 1:number_of_sequences) {
# We get the current sequence data
sequence_x[sequence_index,,] <- data[current_start_index:(current_start_index+sequence_length-1),]
sequence_y[sequence_index,] <- data[(current_start_index+sequence_length+target_shift):(current_start_index+sequence_length+target_shift+target_len-1),]
# We update our next start
current_start_index <- current_start_index + step_shift
}
list(x = sequence_x, y = sequence_y)
}
split_dataset <- function(x_data, y_data, fraction = 0.2) {
train_start_index <- 1
train_end_index <- train_start_index + floor((1-fraction) * dim(x_data)[1]) - 1
test_start_index <- train_end_index + 1
test_end_index <- dim(x_data)[1]
x_train <- x_data[train_start_index:train_end_index,,]
x_train <- array(x_train, dim = c(dim(x_train)[1], dim(x_train)[2], dim(x_data)[3]))
x_test <- x_data[test_start_index:test_end_index,,]
x_test <- array(x_test, dim = c(dim(x_test)[1], dim(x_test)[2], dim(x_data)[3]))
y_train <- y_data[train_start_index:train_end_index]
y_test <- y_data[test_start_index:test_end_index]
list(x_train = x_train,
y_train = y_train,
x_test = x_test,
y_test = y_test)
}
Progress <- R6::R6Class("Progress",
inherit = KerasCallback,
public = list(
num_epochs = NULL,
update_frequency = NULL,
epoch = NULL,
batch = NULL,
initialize = function() {
self$epoch <- 1
},
on_epoch_end = function(epoch, logs = list()) {
validation_info <- ''
if ('val_loss' %in% names(logs))
validation_info <- paste(', val loss: ', logs[['val_loss']], sep = '')
cat('Epoch ', epoch + 1, ' - loss: ', logs[['loss']], validation_info, ' \r', sep = '')
flush.console()
}
))