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

Method _gradient_check_cossim

test/python/test_operation.py:493–515  ·  view source on GitHub ↗
(self, dev=gpu_dev)

Source from the content-addressed store, hash-verified

491
492 # Cos Sim Gradient Check
493 def _gradient_check_cossim(self, dev=gpu_dev):
494 bs = 2
495 vec = 3
496 ta = tensor.random((bs, vec), dev)
497 tb = tensor.random((bs, vec), dev)
498 # treat ta, tb as params
499 ta.stores_grad = True
500 tb.stores_grad = True
501 ty = tensor.random((bs,), dev)
502
503 def _forward():
504 out = autograd.cossim(ta, tb)
505 loss = autograd.mse_loss(out, ty)
506 return loss
507
508 loss = _forward()
509 auto_grads = autograd.gradients(loss)
510
511 params = {id(ta): ta, id(tb): tb}
512
513 for key, param in params.items():
514 auto_grad = tensor.to_numpy(auto_grads[id(param)])
515 self.gradients_check(_forward, param, auto_grad, dev=dev)
516
517 @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
518 def test_gradient_check_cossim_gpu(self):

Calls 1

gradients_checkMethod · 0.95

Tested by

no test coverage detected