Lookup embedding results, accounting for invalid IDs and empty features. The partitioned embedding in `embedding_weights` must all be the same shape except for the first dimension. The first dimension is allowed to vary as the vocabulary size is not necessarily a multiple of `P`. `embedding_w
(embedding_weights,
sparse_ids,
sparse_weights=None,
combiner="mean",
default_id=None,
name=None,
partition_strategy="div",
max_norm=None,
prune=True)
| 1172 | |
| 1173 | @tf_export(v1=["nn.safe_embedding_lookup_sparse"]) |
| 1174 | def safe_embedding_lookup_sparse(embedding_weights, |
| 1175 | sparse_ids, |
| 1176 | sparse_weights=None, |
| 1177 | combiner="mean", |
| 1178 | default_id=None, |
| 1179 | name=None, |
| 1180 | partition_strategy="div", |
| 1181 | max_norm=None, |
| 1182 | prune=True): |
| 1183 | """Lookup embedding results, accounting for invalid IDs and empty features. |
| 1184 | The partitioned embedding in `embedding_weights` must all be the same shape |
| 1185 | except for the first dimension. The first dimension is allowed to vary as the |
| 1186 | vocabulary size is not necessarily a multiple of `P`. `embedding_weights` |
| 1187 | may be a `PartitionedVariable` as returned by using |
| 1188 | `tf.compat.v1.get_variable()` with a |
| 1189 | partitioner. |
| 1190 | Invalid IDs (< 0) are pruned from input IDs and weights, as well as any IDs |
| 1191 | with non-positive weight. For an entry with no features, the embedding vector |
| 1192 | for `default_id` is returned, or the 0-vector if `default_id` is not supplied. |
| 1193 | The ids and weights may be multi-dimensional. Embeddings are always aggregated |
| 1194 | along the last dimension. |
| 1195 | Args: |
| 1196 | embedding_weights: A list of `P` float `Tensor`s or values representing |
| 1197 | partitioned embedding `Tensor`s. Alternatively, a `PartitionedVariable` |
| 1198 | created by partitioning along dimension 0. The total unpartitioned shape |
| 1199 | should be `[e_0, e_1, ..., e_m]`, where `e_0` represents the vocab size |
| 1200 | and `e_1, ..., e_m` are the embedding dimensions. |
| 1201 | sparse_ids: `SparseTensor` of shape `[d_0, d_1, ..., d_n]` containing the |
| 1202 | ids. `d_0` is typically batch size. |
| 1203 | sparse_weights: `SparseTensor` of same shape as `sparse_ids`, containing |
| 1204 | float weights corresponding to `sparse_ids`, or `None` if all weights are |
| 1205 | be assumed to be 1.0. |
| 1206 | combiner: A string specifying how to combine embedding results for each |
| 1207 | entry. Currently "mean", "sqrtn" and "sum" are supported, with "mean" the |
| 1208 | default. |
| 1209 | default_id: The id to use for an entry with no features. |
| 1210 | name: A name for this operation (optional). |
| 1211 | partition_strategy: A string specifying the partitioning strategy. Currently |
| 1212 | `"div"` and `"mod"` are supported. Default is `"div"`. |
| 1213 | max_norm: If not `None`, all embeddings are l2-normalized to max_norm before |
| 1214 | combining. |
| 1215 | Returns: |
| 1216 | Dense `Tensor` of shape `[d_0, d_1, ..., d_{n-1}, e_1, ..., e_m]`. |
| 1217 | Raises: |
| 1218 | ValueError: if `embedding_weights` is empty. |
| 1219 | """ |
| 1220 | if embedding_weights is None: |
| 1221 | raise ValueError("Missing embedding_weights %s." % embedding_weights) |
| 1222 | if isinstance(embedding_weights, variables.PartitionedVariable): |
| 1223 | embedding_weights = list(embedding_weights) # get underlying Variables. |
| 1224 | if not isinstance(embedding_weights, list): |
| 1225 | embedding_weights = [embedding_weights] |
| 1226 | if len(embedding_weights) < 1: |
| 1227 | raise ValueError("Missing embedding_weights %s." % embedding_weights) |
| 1228 | |
| 1229 | dtype = sparse_weights.dtype if sparse_weights is not None else None |
| 1230 | tmp_embedding_weights = [] |
| 1231 | for w in embedding_weights: |
no test coverage detected