-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_training.R
131 lines (109 loc) · 4.19 KB
/
ml_training.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
library(tidyverse)
library(rsample)
library(recipes)
library(textclean)
library(textrecipes)
library(themis)
library(tidymodels)
library(discrim)
library(doMC)
library(tictoc)
library(reticulate)
library(keras)
library(tfdatasets)
source("ml_functions.R")
use_virtualenv("./venv")
# Data Prep --------------------------------------------------------------------
# Load data
# df_raw <- read_csv("data/01_imported/train.csv") %>%
# mutate(
# hate = factor(ifelse(label == 1, "Hate", "Other")),
# tweet_clean = sanitize_tweets(tweet)
# )
# saveRDS(df_raw, "data/02_classification/data_labeled_cleaned.rds")
df_raw <- read_rds("data/02_classification/data_labeled_cleaned.rds") %>%
select(tweet_clean, label) %>%
rename(txt = tweet_clean,
lbl = label) %>%
mutate(lbl = as.factor(lbl))
# Up Sample
df_upsampled <- caret::upSample(x = df_raw %>% select(-lbl),
y = df_raw$lbl,
yname = "lbl") %>%
mutate(lbl = as.numeric(lbl))
# Specify splits
set.seed(8675309)
splits <- df_raw %>%
mutate(lbl = as.numeric(lbl)) %>%
filter(nchar(txt) > 0 & nchar(txt) <= 280) %>%
initial_validation_split(strata = lbl)
splits_up <- df_upsampled %>%
filter(nchar(txt) > 0 & nchar(txt) <= 280) %>%
initial_validation_split(strata = lbl)
df_train <- training(splits)
df_valid <- validation(splits)
df_test <- testing(splits)
# Create validation sets
folds <- vfold_cv(df_train, strata = lbl, v = 10)
# Specify Recipes --------------------------------------------------------------
# Hyper-parameters
max_words <- 20000
max_length <- 30
# ML recipe
ml_rec <- recipe(lbl ~ txt, data = df_train)
# Deep learning recipe
dl_rec <- recipe(~ txt, data = df_train) %>%
step_tokenize(txt) %>%
step_tokenfilter(txt, max_tokens = max_words) %>%
step_sequence_onehot(txt, sequence_length = max_length)
dl_prep <- prep(dl_rec)
# Specify Models ---------------------------------------------------------------
# Dense Neural Network
dnn_model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words + 1,
output_dim = 12,
input_length = max_length) %>%
layer_flatten() %>%
layer_dense(units = 32, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid") %>%
compile_model()
# Base LTSM
lstm_model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words + 1, output_dim = 32) %>%
layer_lstm(units = 32, dropout = 0.4, recurrent_dropout = 0.4) %>%
layer_dense(units = 1, activation = "sigmoid") %>%
compile_model()
# Bi-directional LTSM
lstm_bi_model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words + 1, output_dim = 32) %>%
bidirectional(layer_lstm(units = 32, dropout = 0.4,
recurrent_dropout = 0.4)) %>%
layer_dense(units = 1, activation = "sigmoid") %>%
compile_model()
# Bi-directional, 2-layer LTSM
lstm_bi2_model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words + 1, output_dim = 32) %>%
bidirectional(layer_lstm(units = 32, dropout = 0.4,
recurrent_dropout = 0.4,
return_sequences = TRUE)) %>%
bidirectional(layer_lstm(units = 32, dropout = 0.4,
recurrent_dropout = 0.4)) %>%
layer_dense(units = 1, activation = "sigmoid") %>%
compile_model()
# Convolutional Neural Network
cnn_model <- keras_model_sequential() %>%
layer_embedding(input_dim = max_words + 1, output_dim = 16,
input_length = max_length) %>%
layer_conv_1d(filter = 32, kernel_size = 5, activation = "relu") %>%
layer_global_max_pooling_1d() %>%
layer_dense(units = 64, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid") %>%
compile_model()
# Fit Models -------------------------------------------------------------------
dnn_res <- fit_wrapper(splits, dl_prep, model = dnn_model)
lstm_res <- fit_wrapper(splits, dl_prep, model = lstm_model)
lstm_bi_res <- fit_wrapper(splits, dl_prep, model = lstm_model)
lstm_bi2_res <- fit_wrapper(splits, dl_prep, model = lstm_bi2_model)
cnn_res <- fit_wrapper(splits, dl_prep, model = cnn_model)
cv_fitted <- folds %>%
mutate(validation = map(splits, fit_split(), dl_prep))