Skip to content

Commit 8047fc8

Browse files
committed
moved l2 to first step of backstitch
1 parent 91576d2 commit 8047fc8

File tree

3 files changed

+128
-13
lines changed

3 files changed

+128
-13
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
# Copyright 2012 Johns Hopkins University (author: Daniel Povey) Tony Robinson
4+
# 2017 Hainan Xu
5+
# 2017 Ke Li
6+
# 2018 Yiming Wang
7+
8+
# same as lstm_tdnn_1b, but with backstitch training.
9+
10+
# rnnlm/train_rnnlm.sh: best iteration (out of 160) was 156, linking it to final iteration.
11+
# rnnlm/train_rnnlm.sh: train/dev perplexity was 40.2 / 47.8.
12+
# Train objf: -6.47 -5.36 -5.02 -4.85 -4.73 -4.65 -4.59 -4.54 -4.49 -4.45 -4.41 -4.38 -4.35 -4.33 -4.30 -4.29 -4.27 -4.25 -4.23 -4.22 -4.20 -4.19 -4.18 -4.17 -4.16 -4.14 -4.14 -4.13 -4.11 -4.11 -4.10 -4.09 -4.09 -4.07 -4.07 -4.06 -4.05 -4.05 -4.04 -4.04 -4.03 -4.00 -3.98 -3.96 -3.98 -3.96 -3.95 -3.94 -3.95 -3.94 -3.92 -3.92 -3.92 -3.91 -3.90 -3.90 -3.91 -3.90 -3.88 -3.88 -3.89 -3.88 -3.87 -3.86 -3.87 -3.86 -3.85 -3.85 -3.85 -3.85 -3.84 -3.83 -3.84 -3.83 -3.82 -3.82 -3.83 -3.82 -3.81 -3.81 -3.82 -3.81 -3.80 -3.80 -3.80 -3.80 -3.79 -3.79 -3.79 -3.79 -3.78 -3.77 -3.78 -3.77 -3.77 -3.76 -3.77 -3.76 -3.75 -3.75 -3.75 -3.75 -3.75 -3.74 -3.74 -3.74 -3.73 -3.73 -3.73 -3.73 -3.72 -3.73 -3.73 -3.72 -3.71 -3.71 -3.71 -3.71 -3.71 -3.71 -3.72 -3.71 -3.69 -3.70 -3.69 -3.69 -3.69 -3.68 -3.68 -3.68 -3.67 -3.67 -3.67 -3.67 -3.67 -3.66 -3.66 -3.66 -3.65 -3.65 -3.65 -3.65 -3.64 -3.64 -3.64 -3.64 -3.63 -3.63 -3.63 -3.63 -3.63 -3.62 -3.63 -3.62 -3.62 -3.62 -3.62 -3.62 -3.61 -3.61
13+
# Dev objf: -11.73 -5.72 -5.18 -4.95 -4.81 -4.72 -4.65 -4.59 -4.55 -4.50 -4.47 -4.44 -4.41 -4.38 -4.36 -4.34 -4.33 -4.31 -4.30 -4.28 -4.26 -4.26 -4.25 -4.23 -4.22 -4.22 -4.22 -4.19 -4.20 -4.18 -4.19 -4.17 -4.16 -4.16 -4.16 -4.14 -4.14 -4.14 -4.13 -4.12 -4.12 -4.07 -4.06 -4.05 -4.04 -4.04 -4.04 -4.03 -4.03 -4.02 -4.02 -4.02 -4.01 -4.01 -4.01 -4.00 -4.00 -4.00 -3.99 -3.99 -3.99 -3.99 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.97 -3.97 -3.97 -3.97 -3.96 -3.96 -3.96 -3.96 -3.96 -3.96 -3.95 -3.95 -3.95 -3.95 -3.95 -3.95 -3.94 -3.94 -3.94 -3.94 -3.94 -3.94 -3.94 -3.94 -3.94 -3.93 -3.93 -3.93 -3.93 -3.93 -3.92 -3.93 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.92 -3.91 -3.91 -3.91 -3.91 -3.91 -3.91 -3.90 -3.90 -3.91 -3.89 -3.89 -3.89 -3.89 -3.89 -3.89 -3.89 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.88 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87 -3.87
14+
15+
# Begin configuration section.
16+
affix=1a
17+
embedding_dim=800
18+
lstm_rpd=200
19+
lstm_nrpd=200
20+
embedding_l2=0.001 # embedding layer l2 regularize
21+
comp_l2=0.001 # component-level l2 regularize
22+
output_l2=0.001 # output-layer l2 regularize
23+
epochs=40
24+
stage=-10
25+
train_stage=-10
26+
# backstitch options
27+
alpha=0.2 # backstitch training scale
28+
back_interval=1 # backstitch training interval
29+
30+
. ./cmd.sh
31+
. ./utils/parse_options.sh
32+
[ -z "$cmd" ] && cmd=$train_cmd
33+
34+
35+
dir=exp/rnnlm_lstm_tdnn_bs_$affix
36+
text=data/local/dict_nosp_larger/cleaned.gz
37+
wordlist=data/lang_nosp/words.txt
38+
text_dir=data/rnnlm/text_nosp
39+
mkdir -p $dir/config
40+
set -e
41+
42+
for f in $text $wordlist; do
43+
[ ! -f $f ] && \
44+
echo "$0: expected file $f to exist; search for local/wsj_extend_dict.sh in run.sh" && exit 1
45+
done
46+
47+
if [ $stage -le 0 ]; then
48+
mkdir -p $text_dir
49+
echo -n >$text_dir/dev.txt
50+
# hold out one in every 500 lines as dev data.
51+
gunzip -c $text | awk -v text_dir=$text_dir '{if(NR%500 == 0) { print >text_dir"/dev.txt"; } else {print;}}' >$text_dir/wsj.txt
52+
fi
53+
54+
if [ $stage -le 1 ]; then
55+
# the training scripts require that <s>, </s> and <brk> be present in a particular
56+
# order.
57+
cp $wordlist $dir/config/
58+
n=`cat $dir/config/words.txt | wc -l`
59+
echo "<brk> $n" >> $dir/config/words.txt
60+
61+
# words that are not present in words.txt but are in the training or dev data, will be
62+
# mapped to <SPOKEN_NOISE> during training.
63+
echo "<SPOKEN_NOISE>" >$dir/config/oov.txt
64+
65+
cat > $dir/config/data_weights.txt <<EOF
66+
wsj 1 1.0
67+
EOF
68+
69+
rnnlm/get_unigram_probs.py --vocab-file=$dir/config/words.txt \
70+
--unk-word="<SPOKEN_NOISE>" \
71+
--data-weights-file=$dir/config/data_weights.txt \
72+
$text_dir | awk 'NF==2' >$dir/config/unigram_probs.txt
73+
74+
# choose features
75+
rnnlm/choose_features.py --unigram-probs=$dir/config/unigram_probs.txt \
76+
--use-constant-feature=true \
77+
--top-word-features=50000 \
78+
--min-frequency 1.0e-03 \
79+
--special-words='<s>,</s>,<brk>,<SPOKEN_NOISE>' \
80+
$dir/config/words.txt > $dir/config/features.txt
81+
82+
lstm_opts="l2-regularize=$comp_l2"
83+
tdnn_opts="l2-regularize=$comp_l2"
84+
output_opts="l2-regularize=$output_l2"
85+
86+
cat >$dir/config/xconfig <<EOF
87+
input dim=$embedding_dim name=input
88+
relu-renorm-layer name=tdnn1 dim=$embedding_dim $tdnn_opts input=Append(0, IfDefined(-1))
89+
fast-lstmp-layer name=lstm1 cell-dim=$embedding_dim recurrent-projection-dim=$lstm_rpd non-recurrent-projection-dim=$lstm_nrpd $lstm_opts
90+
relu-renorm-layer name=tdnn2 dim=$embedding_dim $tdnn_opts input=Append(0, IfDefined(-2))
91+
fast-lstmp-layer name=lstm2 cell-dim=$embedding_dim recurrent-projection-dim=$lstm_rpd non-recurrent-projection-dim=$lstm_nrpd $lstm_opts
92+
relu-renorm-layer name=tdnn3 dim=$embedding_dim $tdnn_opts input=Append(0, IfDefined(-1))
93+
output-layer name=output $output_opts include-log-softmax=false dim=$embedding_dim
94+
EOF
95+
rnnlm/validate_config_dir.sh $text_dir $dir/config
96+
fi
97+
98+
if [ $stage -le 2 ]; then
99+
# the --unigram-factor option is set larger than the default (100)
100+
# in order to reduce the size of the sampling LM, because rnnlm-get-egs
101+
# was taking up too much CPU (as much as 10 cores).
102+
rnnlm/prepare_rnnlm_dir.sh --unigram-factor 200.0 \
103+
$text_dir $dir/config $dir
104+
fi
105+
106+
if [ $stage -le 3 ]; then
107+
backstitch_opt="--backstitch-training-scale $alpha \
108+
--backstitch-training-interval $back_interval"
109+
rnnlm/train_rnnlm.sh --num-jobs-initial 1 --num-jobs-final 3 \
110+
--embedding_l2 $embedding_l2 \
111+
--stage $train_stage --num-epochs $epochs --cmd "$cmd" $backstitch_opt $dir
112+
fi
113+
114+
exit 0

src/rnnlm/rnnlm-core-training.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,6 @@ void RnnlmCoreTrainer::TrainBackstitch(
239239
word_embedding_deriv->AddSmatMat(1.0, derived.input_words_smat, kNoTrans,
240240
input_deriv, 1.0);
241241
}
242-
// If relevant, add in the part of the gradient that comes from L2
243-
// regularization.
244-
ApplyL2Regularization(*nnet_,
245-
minibatch.num_chunks * config_.l2_regularize_factor,
246-
delta_nnet_);
247242

248243
BaseFloat max_change_scale, scale_adding;
249244
if (is_backstitch_step1) {
@@ -257,6 +252,12 @@ void RnnlmCoreTrainer::TrainBackstitch(
257252
max_change_scale = 1.0 + config_.backstitch_training_scale;
258253
scale_adding = 1.0 + config_.backstitch_training_scale;
259254
num_minibatches_processed_++;
255+
// If relevant, add in the part of the gradient that comes from L2
256+
// regularization.
257+
ApplyL2Regularization(*nnet_,
258+
1.0 / scale_adding *
259+
minibatch.num_chunks * config_.l2_regularize_factor,
260+
delta_nnet_);
260261
}
261262

262263
UpdateNnetWithMaxChange(*delta_nnet_, config_.max_param_change,

src/rnnlm/rnnlm-embedding-training.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,18 @@ void RnnlmEmbeddingTrainer::TrainBackstitch(
124124
// "embedding_deriv += - 2 * l2_regularize * embedding_mat_"
125125
// This is an approximate to the regular l2 regularization (add l2 regularization
126126
// to the objective function).
127-
if (config_.l2_regularize > 0.0) {
127+
if (config_.l2_regularize > 0.0 && !is_backstitch_step1) {
128128
BaseFloat l2_term = -2 * config_.l2_regularize;
129129
if (l2_term != 0.0) {
130-
embedding_deriv->AddMat(l2_term, *embedding_mat_);
130+
embedding_deriv->AddMat(1.0 / (1.0 + config_.backstitch_training_scale) *
131+
l2_term, *embedding_mat_);
131132
}
132133
}
133134

134135
BaseFloat scale = 1.0;
135136
if (config_.use_natural_gradient) {
136137
if (is_backstitch_step1) preconditioner_.Freeze(true);
137-
preconditioner_.PreconditionDirections(embedding_deriv, NULL,
138-
&scale);
138+
preconditioner_.PreconditionDirections(embedding_deriv, &scale);
139139
}
140140
scale *= config_.learning_rate;
141141
num_minibatches_++;
@@ -226,17 +226,17 @@ void RnnlmEmbeddingTrainer::TrainBackstitch(
226226
// "embedding_deriv += - 2 * l2_regularize * embedding_mat_"
227227
// This is an approximate to the regular l2 regularization (add l2 regularization
228228
// to the objective function).
229-
if (config_.l2_regularize > 0.0) {
229+
if (config_.l2_regularize > 0.0 && !is_backstitch_step1) {
230230
BaseFloat l2_term = -2 * config_.l2_regularize;
231231
if (l2_term != 0.0) {
232-
embedding_deriv->AddMat(l2_term, *embedding_mat_);
232+
embedding_deriv->AddMat(1.0 / (1.0 + config_.backstitch_training_scale) *
233+
l2_term, *embedding_mat_);
233234
}
234235
}
235236
BaseFloat scale = 1.0;
236237
if (config_.use_natural_gradient) {
237238
if (is_backstitch_step1) preconditioner_.Freeze(true);
238-
preconditioner_.PreconditionDirections(embedding_deriv, NULL,
239-
&scale);
239+
preconditioner_.PreconditionDirections(embedding_deriv, &scale);
240240
}
241241
scale *= config_.learning_rate;
242242
if (config_.max_param_change > 0.0) {

0 commit comments

Comments
 (0)