Skip to content

added init_epoch to Glove.fit #89

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 12 additions & 7 deletions glove/glove.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def __init__(self, no_components=30, learning_rate=0.05,
self.inverse_dictionary = None

self.random_state = random_state
self.current_epoch = 0

def fit(self, matrix, epochs=5, no_threads=2, verbose=False):
def fit(self, matrix, epochs=5, no_threads=2, verbose=False, initial_epoch=0):
"""
Estimate the word embeddings.

Expand All @@ -81,6 +82,8 @@ def fit(self, matrix, epochs=5, no_threads=2, verbose=False):
- int epochs: number of training epochs
- int no_threads: number of training threads
- bool verbose: print progress messages if True
- int initial_epoch: Epoch at which to start training (useful for
resuming a previous training run).
"""

shape = matrix.shape
Expand All @@ -93,11 +96,12 @@ def fit(self, matrix, epochs=5, no_threads=2, verbose=False):
raise Exception('Coocurrence matrix must be in the COO format')

random_state = check_random_state(self.random_state)
self.word_vectors = ((random_state.rand(shape[0],
self.no_components) - 0.5)
/ self.no_components)
self.word_biases = np.zeros(shape[0],
dtype=np.float64)
if initial_epoch == 0:
self.word_vectors = ((random_state.rand(shape[0],
self.no_components) - 0.5)
/ self.no_components)
self.word_biases = np.zeros(shape[0],
dtype=np.float64)

self.vectors_sum_gradients = np.ones_like(self.word_vectors)
self.biases_sum_gradients = np.ones_like(self.word_biases)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please vectors_sum_gradients and biases_sum_gradients put in the if statement

if initial_epoch == 0:
        ...
        self.vectors_sum_gradients = np.ones_like(self.word_vectors)
        self.biases_sum_gradients = np.ones_like(self.word_biases)

Because using it to update learning rate in fit_vectors

Expand All @@ -111,7 +115,7 @@ def fit(self, matrix, epochs=5, no_threads=2, verbose=False):
for epoch in range(epochs):

if verbose:
print('Epoch %s' % epoch)
print('Epoch %s' % self.current_epoch)

# Shuffle the coocurrence matrix
random_state.shuffle(shuffle_indices)
Expand All @@ -134,6 +138,7 @@ def fit(self, matrix, epochs=5, no_threads=2, verbose=False):
raise Exception('Non-finite values in word vectors. '
'Try reducing the learning rate or the '
'max_loss parameter.')
self.current_epoch += 1

def transform_paragraph(self, paragraph, epochs=50, ignore_missing=False):
"""
Expand Down