This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathtrain.sh
executable file
·258 lines (240 loc) · 6.65 KB
/
train.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
#!/bin/bash
#
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script for training a Sempar model from scratch.
#
# It should be run from the top-level folder (i.e. the one which contains the
# 'nlp' and 'frame' subfolders).
# Usage:
# /path/to/this/script <train filepattern> <path to commons> <output folder>
#
# It takes as input:
# - A file pattern of training documents with gold annotations. Each file should
# correspond to one serialized Document frame.
# - Path to the commons store.
# - Name of the output folder. All generated resources e.g. feature lexicons,
# action table, master spec, TF graph, trained model etc. will be dumped here.
# - Word embedding dimension, and optionally a pretrained word embedding.
# - Training parameters, e.g. batch size, training steps, learning rate,
# checkpoint interval etc.
# It performs the following steps:
# - Builds the action table.
# - Builds resources needed by the features.
# - Builds a complete MasterSpec proto.
# - Builds a TF graph using the master spec and default hyperparameters.
# - Trains a model using the graph above.
# - Converts the trained model to a Myelin flow file for use in the runtime.
# Tweaks:
# - The features and component attributes (e.g. hidden layer size) are
# hard-coded in generate-master-spec.cc and can be changed there.
set -eu
readonly COMMAND=`echo $0 $@`
# Input resources and arguments.
SEM=$HOME/sempar_ontonotes
COMMONS=${SEM}/commons
OUTPUT_FOLDER=${SEM}/out
TRAIN_FILEPATTERN=${SEM}/train.zip
DEV_GOLD_FILEPATTERN=${SEM}/dev.gold.zip
DEV_NOGOLD_FILEPATTERN=${SEM}/dev.without-gold.zip
WORD_EMBEDDINGS_DIM=32
PRETRAINED_WORD_EMBEDDINGS=$SEM/word2vec-embedding-bi-true-32.tf.recordio
OOV_FEATURES=true
# Training hyperparameters.
BATCH_SIZE=8
REPORT_EVERY=2000
LEARNING_RATE=0.0005
SEED=2
METHOD=adam
ADAM_BETA1=0.01
ADAM_BETA2=0.999
ADAM_EPS=0.00001
GRAD_CLIP_NORM=1.0
DROPOUT=1.0
TRAIN_STEPS=200000
DECAY_STEPS=500000
MOVING_AVERAGE=true
# Whether we should make the MasterSpec again or not.
MAKE_SPEC=1
# Whether we should train or stop after making the MasterSpec.
DO_TRAINING=1
for i in "$@"
do
case $i in
--commons=*)
COMMONS="${i#*=}"
shift
;;
--output_dir=*|--output=*|--output_folder=*)
OUTPUT_FOLDER="${i#*=}"
shift
;;
--flow=*)
FLOW="${i#*=}"
shift
;;
--train=*|--train_corpus=*)
TRAIN_FILEPATTERN="${i#*=}"
shift
;;
--dev=*|--dev_with_gold=*)
DEV_GOLD_FILEPATTERN="${i#*=}"
shift
;;
--dev_without_gold=*)
DEV_NOGOLD_FILEPATTERN="${i#*=}"
shift
;;
--spec_only|--only_spec)
DO_TRAINING=0
shift
;;
--train_only|--only_train)
MAKE_SPEC=0
shift
;;
--batch=*|--batch_size=*)
BATCH_SIZE="${i#*=}"
shift
;;
--report_every=*|--checkpoint_every=*)
REPORT_EVERY="${i#*=}"
shift
;;
--learning_rate=*|--eta=*)
LEARNING_RATE="${i#*=}"
shift
;;
--train_steps=*|--steps=*|--num_train_steps=*)
TRAIN_STEPS="${i#*=}"
shift
;;
--word_embeddings_dim=*|--word_dim=*|--word_embedding_dim=*)
WORD_EMBEDDINGS_DIM="${i#*=}"
shift
;;
--word_embeddings=*|--pretrained_embeddings=*|--pretrained_word_embeddings=*)
PRETRAINED_WORD_EMBEDDINGS="${i#*=}"
shift
;;
--oov_features=*|--oov_lstm_features=*)
OOV_FEATURES="${i#*=}"
shift
;;
--seed=*)
SEED="${i#*=}"
shift
;;
--method=*|--optimizer=*)
METHOD="${i#*=}"
shift
;;
--adam_beta1=*)
ADAM_BETA1="${i#*=}"
shift
;;
--adam_beta2=*)
ADAM_BETA2="${i#*=}"
shift
;;
--adam_eps=*|--adam_epsilon=*)
ADAM_EPS="${i#*=}"
shift
;;
--grad_clip_norm=*|--gradient_clip_norm=*|--grad_clip=*|--gradient_clip=*)
GRAD_CLIP_NORM="${i#*=}"
shift
;;
--dropout=*|--dropout_rate=*|--dropout_keep_rate=*)
DROPOUT="${i#*=}"
shift
;;
--decay=*|--decay_steps=*)
DECAY="${i#*=}"
shift
;;
--moving_average=*|--use_moving_average=*)
MOVING_AVERAGE="${i#*=}"
shift
;;
*)
echo "Unknown option " $i
exit 1
;;
esac
done
if [ -z "$COMMONS" ];
then
echo "Commons not specified. Use --commons to specify it."
exit 1
fi
if [ -z "$TRAIN_FILEPATTERN" ];
then
echo "Train corpus not specified. Use --train or --train_corpus."
exit 1
fi
if [ -z "$DEV_GOLD_FILEPATTERN" ];
then
echo "Dev gold corpus not specified. Use --dev or --dev_with_gold."
exit 1
fi
if [ -z "$DEV_NOGOLD_FILEPATTERN" ];
then
echo "Dev corpus without gold not specified. Use --dev_without_gold."
exit 1
fi
if [[ "$MAKE_SPEC" -eq 0 ]] && [[ "$DO_TRAINING" -eq 0 ]];
then
echo "Specify at most one of --only_spec and --only_train"
exit 1
fi
HYPERPARAMS="learning_rate:${LEARNING_RATE} decay_steps:${DECAY_STEPS} "
HYPERPARAMS+="seed:${SEED} learning_method:'${METHOD}' "
HYPERPARAMS+="use_moving_average:${MOVING_AVERAGE} dropout_rate:${DROPOUT} "
HYPERPARAMS+="gradient_clip_norm:${GRAD_CLIP_NORM} adam_beta1:${ADAM_BETA1} "
HYPERPARAMS+="adam_beta2:${ADAM_BETA2} adam_eps:${ADAM_EPS}"
FLOW=${OUTPUT_FOLDER}/sempar.flow
mkdir -p "${OUTPUT_FOLDER}"
COMMAND_FILE="${OUTPUT_FOLDER}/command"
echo "Writing command to ${COMMAND_FILE}"
echo $COMMAND > ${COMMAND_FILE}
if [[ "$MAKE_SPEC" -eq 1 ]];
then
bazel build -c opt nlp/parser/trainer:generate-master-spec
bazel-bin/nlp/parser/trainer/generate-master-spec \
--documents=${TRAIN_FILEPATTERN} \
--commons=${COMMONS} \
--output_dir=${OUTPUT_FOLDER} \
--word_embeddings=${PRETRAINED_WORD_EMBEDDINGS} \
--word_embeddings_dim=${WORD_EMBEDDINGS_DIM} \
--oov_lstm_features=${OOV_FEATURES}
fi
if [[ "$DO_TRAINING" -eq 1 ]];
then
bazel build -c opt nlp/parser/tools:evaluate-frames
bazel build -c opt nlp/parser/trainer:sempar.so
python nlp/parser/tools/train.py \
--master_spec="${OUTPUT_FOLDER}/master_spec" \
--hyperparams="${HYPERPARAMS}" \
--output_folder=${OUTPUT_FOLDER} \
--flow=${FLOW} \
--commons=${COMMONS} \
--train_corpus=${TRAIN_FILEPATTERN} \
--dev_corpus=${DEV_GOLD_FILEPATTERN} \
--dev_corpus_without_gold=${DEV_NOGOLD_FILEPATTERN} \
--batch_size=${BATCH_SIZE} \
--report_every=${REPORT_EVERY} \
--train_steps=${TRAIN_STEPS}
fi
echo "Done."