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

optimizing sigmoid function #989

Merged
merged 1 commit into from
Nov 1, 2016
Merged
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
11 changes: 3 additions & 8 deletions gensim/models/word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
double, uint32, seterr, array, uint8, vstack, fromstring, sqrt, newaxis,\
ndarray, empty, sum as np_sum, prod, ones, ascontiguousarray, vstack

from scipy.special import expit

from gensim import utils, matutils # utility fnc for pickling, common scipy operations etc
from gensim.corpora.dictionary import Dictionary
from six import iteritems, itervalues, string_types
Expand Down Expand Up @@ -273,13 +275,6 @@ def train_sg_pair(model, word, context_index, alpha, learn_vectors=True, learn_h
l1 += neu1e * lock_factor # learn input -> hidden (mutates model.syn0[word2.index], if that is l1)
return neu1e

def sigmoid(p):
if p > 0:
return 1. / (1. + exp(-p))
elif p <= 0:
return exp(p) / (1 + exp(p))
else:
raise ValueError

def train_cbow_pair(model, word, input_word_indices, l1, alpha, learn_vectors=True, learn_hidden=True):
neu1e = zeros(l1.shape)
Expand All @@ -300,7 +295,7 @@ def train_cbow_pair(model, word, input_word_indices, l1, alpha, learn_vectors=Tr
if w != word.index:
word_indices.append(w)
l2b = model.syn1neg[word_indices] # 2d matrix, k+1 x layer1_size
fb = sigmoid(dot(l1, l2b.T)) # propagate hidden -> output
fb = expit(dot(l1, l2b.T)) # propagate hidden -> output
Copy link
Owner

@piskvorky piskvorky Nov 1, 2016

Choose a reason for hiding this comment

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

Is this the only place where inverse logit (expit) is used? Probably worth updating all occurrences, consistently.

gb = (model.neg_labels - fb) * alpha # vector of error gradients multiplied by the learning rate
if learn_hidden:
model.syn1neg[word_indices] += outer(gb, l1) # learn hidden -> output
Expand Down