Encode set to EmQL representations. Args: params: a dictionary of hyper-parameters. set_ids: a tf.Tensor of element ids in the set. set_mask: a 0/1 tf.Tensor of mask of element ids. embeddings_mat: embedding matrix of shape [vocab_size, d] all_sketches: precomputed hash ids of
(params,
set_ids, set_mask,
embeddings_mat, all_sketches
)
| 79 | |
| 80 | |
| 81 | def encode_op(params, |
| 82 | set_ids, set_mask, |
| 83 | embeddings_mat, all_sketches |
| 84 | ): |
| 85 | """Encode set to EmQL representations. |
| 86 | |
| 87 | Args: |
| 88 | params: a dictionary of hyper-parameters. |
| 89 | set_ids: a tf.Tensor of element ids in the set. |
| 90 | set_mask: a 0/1 tf.Tensor of mask of element ids. |
| 91 | embeddings_mat: embedding matrix of shape [vocab_size, d] |
| 92 | all_sketches: precomputed hash ids of all candidate elements. |
| 93 | |
| 94 | Returns: |
| 95 | dense embedding and sparse count-min sketch of the EmQL representation. |
| 96 | """ |
| 97 | set_embs = tf.nn.embedding_lookup(embeddings_mat, set_ids) # b, s, d |
| 98 | set_mask = tf.cast(tf.expand_dims(set_mask, axis=2), dtype=tf.float32) |
| 99 | set_embs = tf.reduce_sum(set_embs * set_mask, axis=1) # b, d |
| 100 | set_count = tf.reduce_sum(set_mask, axis=1) # b, 1 |
| 101 | set_embs /= set_count # b, d |
| 102 | |
| 103 | set_softmax = tf.squeeze(set_mask, axis=2) / set_count # b, s |
| 104 | set_sketch = create_cm_sketch( |
| 105 | set_ids, set_softmax, all_sketches, params['cm_width']) |
| 106 | return set_embs, set_sketch |
| 107 | |
| 108 | |
| 109 | def decode_op(params, |
nothing calls this directly
no test coverage detected
searching dependent graphs…