votes up 8

combiner must be one of 'mean', 'sqrtn' or 'sum'

Package:
Exception Class:
ValueError

Raise code

""" 
    TypeError: If `sp_ids` is not a `SparseTensor`, or if `sp_weights` is
      neither `None` nor `SparseTensor`.
    ValueError: If `combiner` is not one of {"mean", "sqrtn", "sum"}.
  """
  if combiner is None:
    combiner = "mean"
  if combiner not in ("mean", "sqrtn", "sum"):
    raise ValueError("combiner must be one of 'mean', 'sqrtn' or 'sum'")
  if isinstance(params, variables.PartitionedVariable):
    params = list(params)  # Iterate to get the underlying Variables.
  if not isinstance(params, list):
    params = [params]
  if not isinstance(sp_ids, sparse_tensor.SparseTensor):
    raise TypeError("sp_ids must be SparseTensor")
  ignore_weights = sp_weights is None

Ways to fix

votes up 2 votes down

The valid values of the combiner parameters are:

"sum" ,"mean", "sqrtn"

If any other value is given it is invalid and causes an error:

Reproducing the error:

import tensorflow as tf
import numpy as np


example = tf.SparseTensor(indices=[[0], [1], [2]], values=[3, 6, 9], dense_shape=[3])


vocabulary_size = 10
embedding_size = 1
var = np.array([0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0])
embeddings = tf.Variable(var)


embed = tf.nn.embedding_lookup_sparse(embeddings, example, None,combiner="sqrt")
print(embed)

The error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-7bd9e512c941> in <module>()
      9 embeddings = tf.Variable(var)
     10 
---> 11 embed = tf.nn.embedding_lookup_sparse(embeddings, example, None,combiner="sqrt")
     12 print(embed)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/embedding_ops.py in embedding_lookup_sparse_v2(params, sp_ids, sp_weights, combiner, max_norm, name)
    668   """
    669   return embedding_lookup_sparse(params, sp_ids, sp_weights, "div", name,
--> 670                                  combiner, max_norm)
    671 
    672 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/embedding_ops.py in embedding_lookup_sparse(params, sp_ids, sp_weights, partition_strategy, name, combiner, max_norm)
    484     combiner = "mean"
    485   if combiner not in ("mean", "sqrtn", "sum"):
--> 486     raise ValueError("combiner must be one of 'mean', 'sqrtn' or 'sum'")
    487   if isinstance(params, variables.PartitionedVariable):
    488     params = list(params)  # Iterate to get the underlying Variables.

ValueError: combiner must be one of 'mean', 'sqrtn' or 'sum'

The fix:

Make sure the combiner is set to a valid value.

import tensorflow as tf
import numpy as np


example = tf.SparseTensor(indices=[[0], [1], [2]], values=[3, 6, 9], dense_shape=[3])


vocabulary_size = 10
embedding_size = 1
var = np.array([0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0])
embeddings = tf.Variable(var)


embed = tf.nn.embedding_lookup_sparse(embeddings, example, None,combiner="sqrtn")
print(embed)

Output:

tf.Tensor([ 9. 36. 81.], shape=(3,), dtype=float64)

Jul 10, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix