def test_empirical_baseline_construction(self): """Tests that EmpiricalBaseline construction is correct.""" domain = domains.FixedLengthDiscreteDomain(vocab=domains.Vocabulary( tokens=range(3), include_bos=True), length=2) train_data = np.array([[0, 1], [1, 0]]) train_ds = tf.data.Dataset.from_tensor_slices((train_data, )) eb = evaluation.EmpiricalBaseline(domain, train_ds, alpha=0) self.assertAllEqual(eb._empirical_dist, [0.5, 0.5, 0])
def test_empirical_baseline_evaluation(self): """Tests that EmpiricalBaseline evaluation is correct.""" domain = domains.FixedLengthDiscreteDomain(vocab=domains.Vocabulary( tokens=range(2), include_bos=True), length=1) train_data = np.array([[0, 1], [1, 0]]) train_ds = tf.data.Dataset.from_tensor_slices((train_data, )) eval_data = np.array([[0, 1], [1, 0]]) eval_ds = tf.data.Dataset.from_tensor_slices((eval_data, )) eb = evaluation.EmpiricalBaseline(domain, train_ds) metrics = evaluation.evaluate(eb, eval_ds) self.assertAllEqual(np.asarray(metrics['accuracy']), 0.5) self.assertAllClose(np.asarray(metrics['perplexity']), 2) self.assertAllClose(np.asarray(metrics['loss']), 0.69, atol=0.1)