Skip to content
Open
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
14 changes: 10 additions & 4 deletions bilm/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,17 @@ def __init__(self, lm_vocab_file: str, max_token_length: int):
)
self._max_token_length = max_token_length

def batch_sentences(self, sentences: List[List[str]]):
def batch_sentences(self, sentences: List[List[str]], max_length=None):
'''
Batch the sentences as character ids
Each sentence is a list of tokens without <s> or </s>, e.g.
[['The', 'first', 'sentence', '.'], ['Second', '.']]
'''
n_sentences = len(sentences)
max_length = max(len(sentence) for sentence in sentences) + 2
if max_length == None:
max_length = max(len(sentence) for sentence in sentences) + 2
else:
max_length += 2

X_char_ids = np.zeros(
(n_sentences, max_length, self._max_token_length),
Expand Down Expand Up @@ -240,14 +243,17 @@ def __init__(self, lm_vocab_file: str):
'''
self._lm_vocab = Vocabulary(lm_vocab_file)

def batch_sentences(self, sentences: List[List[str]]):
def batch_sentences(self, sentences: List[List[str]], max_length=None):
'''
Batch the sentences as character ids
Each sentence is a list of tokens without <s> or </s>, e.g.
[['The', 'first', 'sentence', '.'], ['Second', '.']]
'''
n_sentences = len(sentences)
max_length = max(len(sentence) for sentence in sentences) + 2
if max_length == None:
max_length = max(len(sentence) for sentence in sentences) + 2
else:
max_length += 2

X_ids = np.zeros((n_sentences, max_length), dtype=np.int64)

Expand Down
4 changes: 4 additions & 0 deletions bilm/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np

from tensorflow.python.ops.init_ops import glorot_uniform_initializer
from tqdm import tqdm

from .data import Vocabulary, UnicodeCharsVocabulary, InvalidNumberOfCharacters

Expand Down Expand Up @@ -835,6 +836,7 @@ def train(options, data, n_gpus, tf_save_dir, tf_log_dir,

t1 = time.time()
data_gen = data.iter_batches(batch_size * n_gpus, unroll_steps)
pbar = tqdm(total=n_batches_total)
for batch_no, batch in enumerate(data_gen, start=1):

# slice the input in the batch for the feed_dict
Expand Down Expand Up @@ -893,7 +895,9 @@ def train(options, data, n_gpus, tf_save_dir, tf_log_dir,

if batch_no == n_batches_total:
# done training!
pbar.close()
break
pbar.update(1)


def clip_by_global_norm_summary(t_list, clip_norm, norm_name, variables):
Expand Down