-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_cbow.sh
executable file
·178 lines (157 loc) · 5.53 KB
/
train_cbow.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
#!/bin/bash
echo "Running '$0 $1 $2'..."
usage() {
echo "Usage:$0 mode task"
exit 1
}
if [ $# -lt 2 ];then
usage;
fi
. ./const.sh $mode $task
mode=$1
task=$2
root_dir=$(pwd)
src_vocab_size=$(parse_src_vocab_size $mode)
tgt_vocab_size=$(parse_tgt_vocab_size $mode)
is_valid=$(validate_mode $mode $task)
if [ -n "$is_valid" ]; then
echo $is_valid
exit 1
fi
src_domain=$(parse_src_domain $mode)
tgt_domain=$(parse_tgt_domain $mode)
src_lang=$(get_src_lang $tgt_domain $task)
tgt_lang=$(get_tgt_lang $tgt_domain $task)
src_data_dir=$(get_data_dir $mode $src_domain)
tgt_data_dir=$(get_data_dir $mode $tgt_domain)
if [[ $mode =~ \.$outdomain_ext\.v_${tgt_domain} ]]; then
spm_model_dir=$tgt_data_dir
data_dir=$src_data_dir
elif [[ $mode =~ noadapt ]]; then
if [[ $task == translation ]]; then
langs=($src_lang $tgt_lang)
else
langs=($src_lang)
fi
for lang in ${langs[@]}; do
ln -sf \
$root_dir/$src_data_dir/dict.$lang.txt \
$tgt_data_dir/dict.$lang.txt
done
exit 1 # Just make links to the src-domain dictionaries
elif [[ $mode =~ \.$outdomain_ext\. ]]; then
spm_model_dir=$src_data_dir
data_dir=$src_data_dir
elif [[ $mode =~ \.$indomain_ext\. ]]; then
spm_model_dir=$src_data_dir
data_dir=$src_data_dir
elif [[ $mode =~ \.$finetune_ext\.v_${src_domain} ]]; then
spm_model_dir=$src_data_dir
data_dir=$tgt_data_dir
elif [[ $mode =~ \.$finetune_ext\.v_${tgt_domain} ]]; then
spm_model_dir=$tgt_data_dir
data_dir=$tgt_data_dir
elif [[ $mode =~ \.$vocabadapt_ext\. ]]; then
spm_model_dir=$tgt_data_dir
data_dir=$tgt_data_dir
elif [[ $mode =~ .${multidomain_ext}.domainweighting ]]; then
spm_model_dir=$(get_multidomain_data_dir $mode $src_domain $tgt_domain domainweighting)
data_dir=$spm_model_dir
elif [[ $mode =~ .${multidomain_ext}.domainmixing ]]; then
spm_model_dir=$(get_multidomain_data_dir $mode $src_domain $tgt_domain domainmixing)
data_dir=$spm_model_dir
elif [[ $mode =~ .(${backtranslation_ext}_[a-z]+)\. ]]; then
bt_type=${BASH_REMATCH[1]}
spm_model_dir=$(get_multidomain_data_dir $mode $src_domain $tgt_domain $bt_type)
data_dir=$spm_model_dir
else
echo "Invalid mode: $mode"
exit 1
fi
if [ $task != translation ]; then
src_data=$spm_model_dir/train.flat
src_output=$spm_model_dir/word2vec.${src_lang}.${emb_size}d
else
src_data=$spm_model_dir/monolingual.${src_lang}
tgt_data=$spm_model_dir/monolingual.${tgt_lang}
src_output=$spm_model_dir/word2vec.${src_lang}.${emb_size}d
tgt_output=$spm_model_dir/word2vec.${tgt_lang}.${emb_size}d
fi
# word2vec raises an error when the output path is too long...
timestamp=$(date "+%s")
tmp_src_output=/tmp/$timestamp.${src_lang}.${emb_size}d
tmp_tgt_output=/tmp/$timestamp.${tgt_lang}.${emb_size}d
if [ ! -e $src_output ]; then
echo "Training word2vec $src_output from $src_data..."
./tools/word2vec/word2vec -size ${emb_size} \
-train $src_data \
-output $tmp_src_output \
-save-vocab $tmp_src_output.vocab \
-min-count $w2v_mincount &
fi
if [ ! -e $tgt_output ]; then
if [ ! -z $tgt_data ]; then
echo "Training word2vec $tgt_output from $tgt_data..."
./tools/word2vec/word2vec -size ${emb_size} \
-train $tgt_data \
-output $tmp_tgt_output \
-save-vocab $tmp_tgt_output.vocab \
-min-count $w2v_mincount &
fi
fi
wait
if [ ! -e $src_output ]; then
mv $tmp_src_output $src_output
mv $tmp_src_output.vocab $src_output.vocab
fi
if [ ! -e $tgt_output ]; then
mv $tmp_tgt_output $tgt_output
mv $tmp_tgt_output.vocab $tgt_output.vocab
fi
n_special_words=4 # Defined in fairseq/data/dictionary.py .
src_dict=$spm_model_dir/dict.${src_lang}.txt
tgt_dict=$spm_model_dir/dict.${tgt_lang}.txt
if [ -z "$src_vocab_size" ]; then
src_vocab_size=$n_vocab_default
fi
if [ -z "$tgt_vocab_size" ]; then
tgt_vocab_size=$n_vocab_default
fi
if [ ! -e $src_dict ] && [ -e $src_output.vocab ]; then
n_words=$(($src_vocab_size-$n_special_words)) # tgt_vocab_size indicates the vocabulary size in the **target domain** (TODO: two meanings of 'src' are misleading...).
echo "Creating vocabulary file (#tokens=$n_words) from '${src_output}.vocab' to '$src_dict'."
sed -n "2,$((n_words+1))p" $src_output.vocab > $src_dict
fi
if [ ! -e $tgt_dict ] && [ ! -z ${tgt_lang} ] && [ -e $tgt_output.vocab ]; then
n_words=$(($tgt_vocab_size-$n_special_words))
echo "Creating vocabulary file (#tokens=$n_words) from '${tgt_output}.vocab' to '$tgt_dict'."
sed -n "2,$((n_words+1))p" $tgt_output.vocab > $tgt_dict
fi
if [ $spm_model_dir != $data_dir ]; then
if [ ! -e $data_dir/word2vec.$src_lang ]; then
ln -sf \
$root_dir/$spm_model_dir/word2vec.$src_lang.${emb_size}d \
$data_dir/word2vec.$src_lang.${emb_size}d
ln -sf \
$root_dir/$spm_model_dir/word2vec.$src_lang.${emb_size}d.vocab \
$data_dir/word2vec.$src_lang.${emb_size}d.vocab
fi
if [ ! -e $tgt_data_dir/word2vec.$tgt_lang ] && [ ! -z $tgt_data ]; then
ln -sf \
$root_dir/$spm_model_dir/word2vec.$tgt_lang.${emb_size}d \
$data_dir/word2vec.$tgt_lang.${emb_size}d
ln -sf \
$root_dir/$spm_model_dir/word2vec.$tgt_lang.${emb_size}d.vocab \
$data_dir/word2vec.$tgt_lang.${emb_size}d.vocab
fi
if [ ! -e $data_dir/dict.$src_lang.txt ]; then
ln -sf \
$root_dir/$spm_model_dir/dict.$src_lang.txt \
$data_dir/dict.$src_lang.txt
fi
if [ ! -e $tgt_data_dir/dict.$tgt_lang ] && [ ! -z $tgt_data ]; then
ln -sf \
$root_dir/$spm_model_dir/dict.$tgt_lang.txt \
$data_dir/dict.$tgt_lang.txt
fi
fi