Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source/Target length distinction #1

Merged
merged 2 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
help="Path to an existing target vocabulary")


parser.add_argument('-seq_length', type=int, default=50,
help="Maximum sequence length")
parser.add_argument('-src_seq_length', type=int, default=50,
help="Maximum source sequence length")
parser.add_argument('-tgt_seq_length', type=int, default=50,
help="Maximum target sequence length")
parser.add_argument('-shuffle', type=int, default=1,
help="Shuffle data")
parser.add_argument('-seed', type=int, default=3435,
Expand Down Expand Up @@ -108,7 +110,7 @@ def makeData(srcFile, tgtFile, srcDicts, tgtDicts):
print('WARNING: source and target do not have the same number of sentences')
break

if len(srcWords) <= opt.seq_length and len(tgtWords) <= opt.seq_length:
if len(srcWords) <= opt.src_seq_length and len(tgtWords) <= opt.tgt_seq_length:

src += [srcDicts.convertToIdx(srcWords,
onmt.Constants.UNK_WORD)]
Expand Down Expand Up @@ -141,8 +143,8 @@ def makeData(srcFile, tgtFile, srcDicts, tgtDicts):
src = [src[idx] for idx in perm]
tgt = [tgt[idx] for idx in perm]

print('Prepared %d sentences (%d ignored due to length == 0 or > %d)' %
(len(src), ignored, opt.seq_length))
print('Prepared %d sentences (%d ignored due to length == 0 or source length > %d or target length > %d)' %
(len(src), ignored, opt.src_seq_length, opt.tgt_seq_length))

return src, tgt

Expand Down
8 changes: 5 additions & 3 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def trainEpoch(epoch):

total_loss, report_loss = 0, 0
total_words, report_words = 0, 0
report_src_words = 0
start = time.time()
for i in range(len(trainData)):

Expand All @@ -183,16 +184,17 @@ def trainEpoch(epoch):

report_loss += loss
total_loss += loss
report_src_words += batch[0].data.ne(onmt.Constants.PAD).sum()
num_words = targets.data.ne(onmt.Constants.PAD).sum()
total_words += num_words
report_words += num_words
if i % opt.log_interval == 0 and i > 0:
print("Epoch %2d, %5d/%5d batches; perplexity: %6.2f; %3.0f tokens/s" %
print("Epoch %2d, %5d/%5d batches; perplexity: %6.2f; %3.0f Source tokens/s" %
(epoch, i, len(trainData),
math.exp(report_loss / report_words),
report_words/(time.time()-start)))
report_src_words/(time.time()-start)))

report_loss = report_words = 0
report_loss = report_words = report_src_words = 0
start = time.time()

return total_loss / total_words
Expand Down