title: In TensorFlow, how can I assert the values of a list are in certain set?
body: <p>I have a one-dimensional <code>tf.uint8</code> tensor <code>x</code> and want to assert that all values inside that tensor are in set <code>s</code> I define. <code>s</code> is fixed at graph definition time, so it's not a dynamically computed tensor.</p>
  
  <p>In plain Python, I want to do sth. like the following:</p>
  
  <pre><code>x = [1, 2, 3, 1, 11, 3, 5]
  s = {1, 2, 3, 11, 12, 13}
  assert all(el in s for el in x), "This should fail, as 5 is not in s"
  </code></pre>
  
  <p>I know that I can use <a href="https://www.tensorflow.org/api_docs/python/tf/Assert" rel="nofollow noreferrer"><code>tf.Assert</code></a> for the assertion part but I'm struggling with defining the condition part (<code>el in s</code>). What's the simplest/most canonical way to do this?</p>
  
  <p>The older answer <a href="https://stackoverflow.com/q/34618207/3924118is">Determining if A Value is in a Set in TensorFlow</a> is not sufficient to me: first of all, it's complex to write down and understand, and, second, it's using a broadcasted <code>tf.equal</code>, which is more expensive computation wise than a proper set-based check.</p>