-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsc_performance_test.R
253 lines (231 loc) · 9.22 KB
/
dsc_performance_test.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Description:
#
# Performance tests for the Domain-Specific Classifier implemented in "dsc.R".
source("dsc.R")
library(caTools)
library(caret)
# Set working directory
setwd("~/h/proj/cdmc-2015/R")
# Load datasets
file.train <- "~/h/proj/cdmc-2015/data/task1/EnewsTrain.csv"
file.test <- "~/h/proj/cdmc-2015/data/task1/EnewsTest.csv"
df.train <- read.csv(file.train,
header = FALSE,
colClasses = c("character", "integer"),
col.names = c("text", "class"))
df.test <- read.csv(file.test, header = FALSE,
colClasses = c("character"), col.names=c("text"))
# Convert datasets to lower case
train <- df.train
train$text <- lapply(train$text, tolower)
test <- df.test
test$text <- lapply(test$text, tolower)
# Count frequency of the words in the document
corpus <- Corpus(VectorSource(train$text))
dtm <- DocumentTermMatrix(corpus)
###
### Performance test 1
###
## Setup datasets
set.seed(0)
split <- sample.split(train$class, SplitRatio=0.5)
train1 <- dtm[split==TRUE, ]
test1 <- dtm[split==FALSE, ]
train1.labels <- subset(train$class, split==TRUE)
test1.labels <- subset(train$class, split==FALSE)
## Perform classification and time functions
system.time(model.dsc <- dsc(train1, train1.labels, alpha = 2.0, p = 1))
# elapsed (old) = 2.643 seconds
# elapsed (new) = 0.083 seconds
system.time(pred.dsc <- predict(model.dsc, test1))
# elapsed (old) = 89.677 seconds
# elapsed (new) = 5.621 seconds
cm <- confusionMatrix(test1.labels, pred.dsc)
cm$overall[1] # 0.9089552
###
### Performance test 2
###
## Setup datasets
set.seed(0)
split <- sample.split(train$class, SplitRatio=0.75)
train2 <- dtm[split==TRUE, ]
test2 <- dtm[split==FALSE, ]
train2.labels <- subset(train$class, split==TRUE)
test2.labels <- subset(train$class, split==FALSE)
## Perform classification and time functions
system.time(model.dsc <- dsc(train2, train2.labels, alpha = 2.0, p = 1))
# elapsed (old) = 3.984 seconds
# elapsed (new) = 0.116 seconds
system.time(pred.dsc <- predict(model.dsc, test2))
# elapsed (old) = 41.315 seconds
# elapsed (new) = 2.298 seconds
cm <- confusionMatrix(test2.labels, pred.dsc)
cm$overall[1] # 0.9071856
## Perform same classification again, but this time turn on profiling
Rprof("profile2.out", line.profiling=TRUE)
system.time(model.dsc <- dsc(train2, train2.labels, alpha = 2.0, p = 1))
# elapsed = 3.984 seconds
system.time(pred.dsc <- predict(model.dsc, test2))
# elapsed = 41.315 seconds
cm <- confusionMatrix(test2.labels, pred.dsc)
cm$overall[1]
Rprof(NULL)
summaryRprof("profile2.out", lines = "show")
# $by.self
# self.time self.pct total.time total.pct
# dsc.R#140 35.02 73.20 42.72 89.30
# dsc.R#141 7.70 16.10 7.70 16.10
# dsc.R#56 4.54 9.49 4.54 9.49
# <no location> 0.44 0.92 0.44 0.92
# dsc.R#72 0.08 0.17 0.08 0.17
# dsc.R#101 0.02 0.04 0.02 0.04
# dsc.R#301 0.02 0.04 0.02 0.04
# dsc.R#87 0.02 0.04 0.02 0.04
#
# $by.total
# total.time total.pct self.time self.pct
# dsc.R#140 42.72 89.30 35.02 73.20
# dsc.R#171 42.72 89.30 0.00 0.00
# dsc.R#293 42.72 89.30 0.00 0.00
# dsc.R#141 7.70 16.10 7.70 16.10
# dsc.R#182 4.66 9.74 0.00 0.00
# dsc.R#235 4.62 9.66 0.00 0.00
# dsc.R#56 4.54 9.49 4.54 9.49
# <no location> 0.44 0.92 0.44 0.92
# dsc.R#72 0.08 0.17 0.08 0.17
# dsc.R#101 0.02 0.04 0.02 0.04
# dsc.R#301 0.02 0.04 0.02 0.04
# dsc.R#87 0.02 0.04 0.02 0.04
# dsc.R#236 0.02 0.04 0.00 0.00
# dsc.R#238 0.02 0.04 0.00 0.00
#
# $by.line
# self.time self.pct total.time total.pct
# <no location> 0.44 0.92 0.44 0.92
# dsc.R#56 4.54 9.49 4.54 9.49
# dsc.R#72 0.08 0.17 0.08 0.17
# dsc.R#87 0.02 0.04 0.02 0.04
# dsc.R#101 0.02 0.04 0.02 0.04
# dsc.R#140 35.02 73.20 42.72 89.30
# dsc.R#141 7.70 16.10 7.70 16.10
# dsc.R#171 0.00 0.00 42.72 89.30
# dsc.R#182 0.00 0.00 4.66 9.74
# dsc.R#235 0.00 0.00 4.62 9.66
# dsc.R#236 0.00 0.00 0.02 0.04
# dsc.R#238 0.00 0.00 0.02 0.04
# dsc.R#293 0.00 0.00 42.72 89.30
# dsc.R#301 0.02 0.04 0.02 0.04
#
# $sample.interval
# [1] 0.02
#
# $sampling.time
# [1] 47.84
### Okay, so line 140 was the main bottleneck, by far:
###
### newfreqs <- (1 / newdocs.lengths) * col_sums(apply(newdocs, 1, function(row) { row * CS.j }))
###
### It seems like 'apply' is fairly inefficient over such a large datasets,
### probably because of memory-intensive it is, with a new row being created and
### appended at each step. So, I replaced the line with a straight forward
### element-wise matrix multiplication:
###
### newfreqs <- (1 / newdocs.lengths) * row_sums(newdocs * matrix(rep(CS.j, n.newdata), n.newdata, byrow = TRUE))
###
### and it is much faster now! Here is the new profiler's output:
# $by.self
# self.time self.pct total.time total.pct
# dsc.R#56 4.56 53.15 4.56 53.15
# dsc.R#143 3.42 39.86 3.42 39.86
# <no location> 0.46 5.36 0.46 5.36
# dsc.R#72 0.08 0.93 0.08 0.93
# dsc.R#142 0.02 0.23 0.02 0.23
# dsc.R#305 0.02 0.23 0.02 0.23
# dsc.R#87 0.02 0.23 0.02 0.23
#
# $by.total
# total.time total.pct self.time self.pct
# dsc.R#186 4.66 54.31 0.00 0.00
# dsc.R#239 4.64 54.08 0.00 0.00
# dsc.R#56 4.56 53.15 4.56 53.15
# dsc.R#175 3.44 40.09 0.00 0.00
# dsc.R#297 3.44 40.09 0.00 0.00
# dsc.R#143 3.42 39.86 3.42 39.86
# <no location> 0.46 5.36 0.46 5.36
# dsc.R#72 0.08 0.93 0.08 0.93
# dsc.R#142 0.02 0.23 0.02 0.23
# dsc.R#305 0.02 0.23 0.02 0.23
# dsc.R#87 0.02 0.23 0.02 0.23
# dsc.R#240 0.02 0.23 0.00 0.00
#
# $by.line
# self.time self.pct total.time total.pct
# <no location> 0.46 5.36 0.46 5.36
# dsc.R#56 4.56 53.15 4.56 53.15
# dsc.R#72 0.08 0.93 0.08 0.93
# dsc.R#87 0.02 0.23 0.02 0.23
# dsc.R#142 0.02 0.23 0.02 0.23
# dsc.R#143 3.42 39.86 3.42 39.86
# dsc.R#175 0.00 0.00 3.44 40.09
# dsc.R#186 0.00 0.00 4.66 54.31
# dsc.R#239 0.00 0.00 4.64 54.08
# dsc.R#240 0.00 0.00 0.02 0.23
# dsc.R#297 0.00 0.00 3.44 40.09
# dsc.R#305 0.02 0.23 0.02 0.23
#
# $sample.interval
# [1] 0.02
#
# $sampling.time
# [1] 8.58
### The bottleneck, line 56, is:
###
### apply(docs, 1, sum)
###
### So I replaced it with:
###
### row_sums(docs)
###
### Which again was much faster! The new profiling run gives:
# $by.self
# self.time self.pct total.time total.pct
# dsc.R#143 5.18 90.56 5.18 90.56
# <no location> 0.40 6.99 0.40 6.99
# dsc.R#72 0.08 1.40 0.08 1.40
# dsc.R#314 0.02 0.35 0.02 0.35
# dsc.R#56 0.02 0.35 0.02 0.35
# dsc.R#87 0.02 0.35 0.02 0.35
#
# $by.total
# total.time total.pct self.time self.pct
# dsc.R#143 5.18 90.56 5.18 90.56
# dsc.R#175 5.18 90.56 0.00 0.00
# dsc.R#306 5.18 90.56 0.00 0.00
# <no location> 0.40 6.99 0.40 6.99
# dsc.R#186 0.12 2.10 0.00 0.00
# dsc.R#248 0.10 1.75 0.00 0.00
# dsc.R#72 0.08 1.40 0.08 1.40
# dsc.R#314 0.02 0.35 0.02 0.35
# dsc.R#56 0.02 0.35 0.02 0.35
# dsc.R#87 0.02 0.35 0.02 0.35
# dsc.R#249 0.02 0.35 0.00 0.00
#
# $by.line
# self.time self.pct total.time total.pct
# <no location> 0.40 6.99 0.40 6.99
# dsc.R#56 0.02 0.35 0.02 0.35
# dsc.R#72 0.08 1.40 0.08 1.40
# dsc.R#87 0.02 0.35 0.02 0.35
# dsc.R#143 5.18 90.56 5.18 90.56
# dsc.R#175 0.00 0.00 5.18 90.56
# dsc.R#186 0.00 0.00 0.12 2.10
# dsc.R#248 0.00 0.00 0.10 1.75
# dsc.R#249 0.00 0.00 0.02 0.35
# dsc.R#306 0.00 0.00 5.18 90.56
# dsc.R#314 0.02 0.35 0.02 0.35
#
# $sample.interval
# [1] 0.02
#
# $sampling.time
# [1] 5.72