def testKerasLossVsNonKerasLoss(self): features = np.random.uniform(0., 1., size=(12, 2, 20)) labels = np.eye(12, 15, dtype=np.int32) loss_keras = losses.ContrastiveLoss()(labels, features) loss_direct = tf.reduce_mean( losses.contrastive_loss(features, labels=labels)) self.assertFalse(np.isnan(loss_direct.numpy())) self.assertFalse(np.isnan(loss_keras.numpy())) self.assertEqual(loss_direct.numpy(), loss_keras.numpy())
def testKerasImplementation(self): features = np.random.uniform(0., 1., (12, 2, 20)) labels = np.eye(12, 15, dtype=np.int32) loss = losses.ContrastiveLoss()(labels, features) self.assertEqual(loss.shape, ()) self.assertFalse(np.isnan(loss.numpy()))