def test_uniformly_weighted(self): # Uniform weights of size batch size w1 = np.ones(self.y.shape[0], dtype='f') loss_w1 = compute_weighted_value_loss( self.eltwise_losses, self.y.shape[0], w1, batch_accumulator=self.batch_accumulator).array if self.batch_accumulator == 'mean': eltwise_loss = self.eltwise_losses.sum(axis=1).mean() else: eltwise_loss = self.eltwise_losses.sum() self.assertAlmostEqual(loss_w1, eltwise_loss, places=5)
def test_randomly_weighted(self): # Random weights wu = np.random.uniform(low=0, high=2, size=self.y.shape[0]).astype('f') loss_wu = compute_weighted_value_loss( self.eltwise_losses, self.y.shape[0], wu, batch_accumulator=self.batch_accumulator).array if self.batch_accumulator == 'mean': eltwise_loss = (self.eltwise_losses.sum(axis=1) * wu).mean() else: eltwise_loss = (self.eltwise_losses * wu[:, None]).sum() self.assertAlmostEqual(loss_wu, eltwise_loss, places=5)