| 54 | self.assertAllClose(entity_weights_from_sketch, entity_weights_np) |
| 55 | |
| 56 | def test_encode_op(self): |
| 57 | params = { |
| 58 | 'cm_width': 100, |
| 59 | 'cm_depth': 5, |
| 60 | 'intermediate_top_k': 2 |
| 61 | } |
| 62 | self.sess.run(tf.global_variables_initializer()) |
| 63 | all_entity_sketches = tf.constant( |
| 64 | [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10], [11, 13, 15, 17, 19], |
| 65 | [12, 14, 16, 18, 20]], |
| 66 | dtype=tf.int32) |
| 67 | set_ids = tf.constant([[0, 3, 0, 0]], dtype=tf.int32) |
| 68 | set_mask = tf.constant([[1, 1, 0, 0]], dtype=tf.int32) |
| 69 | embeddings_mat = tf.constant( |
| 70 | [[0.1, 0.2, 0.3], [0.2, 0.3, 0.4], [-0.1, -0.2, -0.3], |
| 71 | [-0.2, -0.3, -0.4]], dtype=tf.float32) |
| 72 | |
| 73 | set_embs, set_sketch = module.encode_op( |
| 74 | params, set_ids, set_mask, embeddings_mat, all_entity_sketches) |
| 75 | # The value of set_embs should be the centroid of vector embeddings_mat[0] |
| 76 | # ([0.1, 0.2, 0.3]) and embeddings_mat[3] ([-0.2, -0.3, -0.4]). |
| 77 | self.assertAllClose( |
| 78 | set_embs, np.array([[-0.05, -0.05, -0.05]], np.float32)) |
| 79 | _, set_weights_from_sketch = module.check_topk_fact_eligible( |
| 80 | tf.constant([[0, 1, 2, 3]], dtype=tf.int32), set_sketch, |
| 81 | all_entity_sketches, params) |
| 82 | # The returned weights should be the probability distribution of entities |
| 83 | # in the set. Because only entity with id 0 and 3 are in the set, they |
| 84 | # should each have the probability 0.5. |
| 85 | self.assertAllClose( |
| 86 | set_weights_from_sketch, |
| 87 | np.array([[0.5, 0, 0, 0.5]], np.float32)) |
| 88 | |
| 89 | def test_decode_op(self): |
| 90 | params = { |