-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunctions.sh
400 lines (315 loc) · 7.61 KB
/
functions.sh
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env bash
set -e
export MPLBACKEND="Agg"
#
# Boilerplate.
#
# Most of the functions below were copied from https://github.com/cvangysel/cvangysel-common.
#
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#
# Software management functions.
#
check_installed() {
command -v $1 >/dev/null 2>&1 || { echo >&2 "Required tool '$1' is not installed. Please make sure that the executable '$1' is in your PATH. Aborting."; exit 1; }
}
package_root() {
git rev-parse --show-toplevel
}
#
# Array-related functions.
#
# From http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value.
#
# Usage: if [[ $(contains ${HAYSTACK[@]} ${NEEDLE}) == "y" ]]; then ...
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
# Applies predictate $1 to array $2..
function apply_array() {
PREDICATE=${1}
shift
for ARG in $@; do
${PREDICATE} $ARG
done
}
#
# Value comparison.
#
check_eq() {
check_not_empty "${1:-}" "first argument"
check_not_empty "${2:-}" "second argument"
if [[ "${1}" != "${2}" ]]; then
1>&2 echo "Value '${1}' is not equal to '${2}'."
exit -1
fi
}
check_ne() {
check_not_empty "${1:-}" "first argument"
check_not_empty "${2:-}" "second argument"
if [[ "${1}" == "${2}" ]]; then
1>&2 echo "Value '${1}' is equal to '${2}'."
exit -1
fi
}
check_not_empty() {
check_not_empty "${1:-}" "first argument"
check_not_empty "${2:-}" "second argument"
if [[ "${1}" == "${2}" ]]; then
1>&2 echo "Value '${1}' is equal to '${2}'."
exit -1
fi
}
#
# Integral.
#
check_int() {
if [[ ! "$1" =~ ^-?[0-9]+?$ ]] ; then
1>&2 echo "String $1 is not a float."
exit -1
fi
}
int_compare() {
if [[ "$1" < "$2" ]]; then
1>&2 echo "$1 is smaller than $2."
exit -1
fi
}
check_pos_int() {
check_int "$1"
int_compare "$1" "0"
}
#
# Floating point.
#
check_float() {
if [[ ! "$1" =~ ^-?[0-9]+([.][0-9]+)?(e-?[0-9]+)?$ ]] ; then
1>&2 echo "String $1 is not a float."
exit -1
fi
}
float_compare() {
awk -v n1=$1 -v n2=$2 'BEGIN{ if (n1<n2) print 0; print 1}'
}
max() {
check_float "${1:-}"
check_float "${2:-}"
echo "${1:-}" | awk "{if (\$0 > ${2:-}) {print} else {print ${2:-}}}"
}
#
# String validation.
#
check_not_empty() {
if [[ -z "$1" ]]; then
1>&2 echo "Received empty string instead of $2."
exit -1
fi
}
check_valid_option() {
if [[ $(contains "$@") != "y" ]]; then
local n=$#
local value=${!n}
1>&2 printf "Option '%s' is not valid (allowed:" "${value}"
for ((i=1;i < $#;i++)) {
1>&2 printf " %s" "${!i}"
}
1>&2 printf ").\n"
exit -1
fi
}
#
# String manipulation
#
join_strings() {
SEPARATOR="${1:-}"
check_not_empty "${SEPARATOR}" "separator"
shift
if [[ "$#" -eq "0" ]]; then
return
fi
echo -n "$1"
shift
for ELEMENT in $@; do
echo -n "${SEPARATOR}"
echo -n "${ELEMENT}"
done
echo
}
#
# File system.
#
check_multiple() {
INVARIANT="${1:-}"
shift
for ARG in $@; do
${INVARIANT} ${ARG}
done
}
check_file() {
if [[ ! -f "$1" ]]; then
1>&2 echo "File $1 does not exist."
exit -1
fi
}
check_file_not_exists() {
if [[ -f "$1" ]]; then
1>&2 echo "File $1 already exists."
exit -1
fi
}
check_directory() {
if [[ ! -d "$1" ]]; then
1>&2 echo "Directory $1 does not exist."
exit -1
fi
}
check_directory_not_exists() {
if [[ -d "$1" ]]; then
1>&2 echo "Directory $1 already exists."
exit -1
fi
}
directory_md5sum() {
DIRECTORY="${1:-}"
check_not_empty "${DIRECTORY}" "m5sum directory"
check_directory "${DIRECTORY}"
CURRENT_DIR=$(pwd)
cd "${DIRECTORY}" && find . -type f -exec md5sum {} \; \
| awk '{print $2 " " $1}' \
| sort -k 1
cd "${CURRENT_DIR}"
}
#
# Time.
#
timestamp() {
date +%s
}
check_installed "IndriBuildIndex"
check_installed "curl"
check_installed "tar"
check_installed "trec_eval"
check_installed "PyndriQuery"
check_installed "PyndriStatistics"
#
# Constants.
#
NVSM_MODELS=( "LSE" "NVSM" )
declare -A NVSM_TRAIN_ARGS
NVSM_TRAIN_ARGS[NVSM]="--batch_size 51200 --nonlinearity hard_tanh --batch_normalization"
NVSM_TRAIN_ARGS[LSE]="--batch_size 4096 --nonlinearity tanh --bias_negative_samples"
declare -A NVSM_QUERY_ARGS
NVSM_QUERY_ARGS[NVSM]="--linear"
NVSM_QUERY_ARGS[LSE]=""
#
# Helper functions.
#
function compute_retrieval_effectiveness() {
RUN_PATH="${1:-}"
check_not_empty "${RUN_PATH}"
check_file "${RUN_PATH}"
trec_eval -m all_trec "${QREL_PATH}" "${RUN_PATH}" | awk '/^map_cut_1000\s+all/{print $3}'
}
function print_section_title() {
TITLE="${1:-}"
check_not_empty "${TITLE}" "TITLE"
1>&2 echo
for IDX in $(seq 1 $(( ${#TITLE} + 4 )) ); do
1>&2 echo -n "#"
done
1>&2 echo
1>&2 echo "# ${TITLE} #"
for IDX in $(seq 1 $(( ${#TITLE} + 4 )) ); do
1>&2 echo -n "#"
done
1>&2 echo
1>&2 echo
}
function print_subsection_title() {
TITLE="${1:-}"
check_not_empty "${TITLE}" "TITLE"
1>&2 echo "${TITLE}"
for IDX in $(seq 1 $(( ${#TITLE} )) ); do
1>&2 echo -n "-"
done
1>&2 echo
}
function print_columns() {
FIRST_COLUMN="${1:-}"
SECOND_COLUMN="${2:-}"
1>&2 echo -n "${FIRST_COLUMN}"
for IDX in $(seq 1 $(( 80 - ${#FIRST_COLUMN} )) ); do
1>&2 echo -n " "
done
1>&2 echo -n "${SECOND_COLUMN}"
1>&2 echo
}
function build_index() {
INDEX_PARAMS="${1:-}"
check_not_empty "${INDEX_PARAMS}" "INDEX_PARAMS"
STOPWORD_LIST="${2:-}"
check_not_empty "${STOPWORD_LIST}"
INDEX_DIR="${3:-}"
check_not_empty "${INDEX_DIR}" "INDEX_DIR"
check_directory_not_exists "${INDEX_DIR}" "INDEX_DIR"
TRECTEXT_PATH="${4:-}"
check_not_empty "${INDEX_DIR}" "INDEX_DIR"
check_file "${TRECTEXT_PATH}" "TRECTEXT_PATH"
if [[ ! -f "${STOPWORD_LIST}" ]]; then
1>&2 echo "Downloading Indri stoplist.dft."
curl -s http://www.lemurproject.org/stopwords/stoplist.dft \
> "${STOPWORD_LIST}"
fi
if [[ ! -f "${INDEX_PARAMS}" ]]; then
cat > "${INDEX_PARAMS}" <<EOF
<parameters>
<memory>1024MB</memory>
<storeDocs>false</storeDocs>
<index>${INDEX_DIR}</index>
<corpus>
<path>${TRECTEXT_PATH}</path>
<class>trectext</class>
</corpus>"
</parameters>
EOF
fi
IndriBuildIndex "${INDEX_PARAMS}" "${STOPWORD_LIST}"
}
function train_nvsm() {
NVSM_MODEL_="${1:-}"
check_valid_option ${NVSM_MODELS[@]} "${NVSM_MODEL_}"
OUTPUT_PREFIX_="${2:-}"
check_not_empty "${OUTPUT_PREFIX_}"
INDEX_DIR_="${3:-}"
check_not_empty "${INDEX_DIR_}" "INDEX_DIR"
check_directory "${INDEX_DIR_}"
GLOG_logtostderr=1 \
${SCRIPT_DIR}/build/cpp/cuNVSMTrainModel \
--num_epochs "${NUM_EPOCHS}" \
--max_vocabulary_size 65536 \
--min_document_frequency 0 \
--regularization_lambda 1e-2 \
--learning_rate 1e-3 \
--window_size 10 \
--word_repr_size 300 \
--entity_repr_size 256 \
--num_random_entities 10 \
${NVSM_TRAIN_ARGS[${NVSM_MODEL_}]} \
--weighting uniform \
--seed 1 \
--update_method full_adam \
--compute_initial_cost \
--dump_initial_model \
--dump_every 1000000 \
--output "${OUTPUT_PREFIX_}" \
"${INDEX_DIR_}"
}