MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / sgd

Function sgd

nlp_class2/word2vec.py:246–263  ·  view source on GitHub ↗
(input_, targets, label, learning_rate, W, V)

Source from the content-addressed store, hash-verified

244
245
246def sgd(input_, targets, label, learning_rate, W, V):
247 # W[input_] shape: D
248 # V[:,targets] shape: D x N
249 # activation shape: N
250 # print("input_:", input_, "targets:", targets)
251 activation = W[input_].dot(V[:,targets])
252 prob = sigmoid(activation)
253
254 # gradients
255 gV = np.outer(W[input_], prob - label) # D x N
256 gW = np.sum((prob - label)*V[:,targets], axis=1) # D
257
258 V[:,targets] -= learning_rate*gV # D x N
259 W[input_] -= learning_rate*gW # D
260
261 # return cost (binary cross entropy)
262 cost = label * np.log(prob + 1e-10) + (1 - label) * np.log(1 - prob + 1e-10)
263 return cost.sum()
264
265
266def load_model(savedir):

Callers 1

train_modelFunction · 0.85

Calls 1

sigmoidFunction · 0.50

Tested by

no test coverage detected