MCPcopy Create free account
hub / github.com/apache/singa / numpy_train_one_batch

Method numpy_train_one_batch

test/python/test_model.py:377–412  ·  view source on GitHub ↗
(self, inputs, y)

Source from the content-addressed store, hash-verified

375 return self.x5
376
377 def numpy_train_one_batch(self, inputs, y):
378 # forward propagation
379 out = self.numpy_forward(inputs)
380
381 # softmax cross entropy loss
382 exp_out = np.exp(out - np.max(out, axis=-1, keepdims=True))
383 self.softmax = exp_out / np.sum(exp_out, axis=-1, keepdims=True)
384 loss = np.sum(y * np.log(self.softmax)) / -self.softmax.shape[0]
385
386 # optimize
387 # calculate gradients
388 label_sum = np.sum(self.label, axis=-1)
389 dloss = self.softmax - self.label / label_sum.reshape(
390 label_sum.shape[0], 1)
391 dloss /= self.softmax.shape[0]
392
393 dx5 = dloss
394 db1 = np.sum(dloss, 0)
395
396 dx4 = np.matmul(dx5, self.W1.T)
397 dw1 = np.matmul(self.x3.T, dx5)
398
399 dx3 = dx4 * (self.x3 > 0)
400
401 dx2 = dx3
402 db0 = np.sum(dx3, 0)
403
404 dx1 = np.matmul(dx2, self.W0.T)
405 dw0 = np.matmul(self.data.T, dx2)
406
407 # update all the params
408 self.W0 -= 0.05 * dw0
409 self.B0 -= 0.05 * db0
410 self.W1 -= 0.05 * dw1
411 self.B1 -= 0.05 * db1
412 return out, loss
413
414 def setUp(self):
415 self.sgd = opt.SGD(lr=0.05)

Callers 1

Calls 2

numpy_forwardMethod · 0.95
reshapeMethod · 0.45

Tested by

no test coverage detected