Skip to content

Instantly share code, notes, and snippets.

@ashunigion
Created June 10, 2019 01:27
Show Gist options
  • Save ashunigion/abac55dea75c113089e7796ee0c25515 to your computer and use it in GitHub Desktop.
Save ashunigion/abac55dea75c113089e7796ee0c25515 to your computer and use it in GitHub Desktop.
creating the batch of words and and their corresponding context words.
def get_batches(words, batch_size, window_size=5):
''' Create a generator of word batches as a tuple (inputs, targets) '''
n_batches = len(words)//batch_size
# only full batches
words = words[:n_batches*batch_size]
for idx in range(0, len(words), batch_size):
x, y = [], []
batch = words[idx:idx+batch_size]
for ii in range(len(batch)):
batch_x = batch[ii]
batch_y = get_target(batch, ii, window_size)
y.extend(batch_y)
x.extend([batch_x]*len(batch_y))
yield x, y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment