votes up 0

`salt can only be a tuple of size 2 integers, or a single integer, given (param1)

Package:
keras
github stars 51164
Exception Class:
ValueError

Raise code

.strong_hash = True if salt is not None else False
    if salt is not None:
      if isinstance(salt, (tuple, list)) and len(salt) == 2:
        self.salt = salt
      elif isinstance(salt, int):
        self.salt = [salt, salt]
      else:
        raise ValueError('`salt can only be a tuple of size 2 integers, or a '
                         'single integer, given {}'.format(salt))
    else:
      self.salt = _DEFAULT_SALT_KEY

  def _preprocess_single_input(self, inp):
    if isinstance(inp, (list, tuple, np.ndarray)):
      inp = tf.convert_to_tensor(inp)
    retu

Ways to fix

votes up 2 votes down

The salt parameter is expected to be a single integer or a tuple of 2 elements. Any other value causes this error:

Reproducing the error:

$ pipenv install tensorflow 

import tensorflow as tf
layer = tf.keras.layers.experimental.preprocessing.Hashing(
    num_bins = 3,
    salt=(133,123,12) # this is the cause of the error;
    )
inp = [['A'], ['B'], ['C'], ['D'], ['E']]
layer(inp)

Fix: Set salt either a single integer number or a tuple of two integers. The documentation clearly specifies the possible valid values of salt

import tensorflow as tf
layer = tf.keras.layers.experimental.preprocessing.Hashing(
    num_bins = 3,
    salt=123 # valid value
)
inp = [['A'], ['B'], ['C'], ['D'], ['E']]
layer(inp)
Jun 11, 2021 kellemnegasi answer
kellemnegasi 2.7k

Add a possible fix

Please authorize to post fix