title: Tensorflow TypeError: Can not convert a NoneType into a Tensor or Operation
body: <p>I am learning TensorFlow and was going through <a href="https://machinelearningmastery.com/use-word-embedding-layers-deep-learning-keras/" rel="nofollow noreferrer">this</a> step-by-step guide. The below code is the exact same as on the website. However, when running it, I get an error when trying to fit the model. The full traceback I get is as follows:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\users\name\desktop\python ml tutorial\embedding.py&quot;, line 49, in &lt;module&gt;
      model.fit(x=padded_docs, y=labels, epochs=50, verbose=0)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py&quot;, line 1213, in fit
      self._make_train_function()
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py&quot;, line 316, in _make_train_function
      loss=self.total_loss)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py&quot;, line 506, in get_updates
      return [self.apply_gradients(grads_and_vars)]
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py&quot;, line 441, in apply_gradients
      kwargs={&quot;name&quot;: name})
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py&quot;, line 1917, in merge_call
      return self._merge_call(merge_fn, args, kwargs)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py&quot;, line 1924, in _merge_call
      return merge_fn(self._strategy, *args, **kwargs)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py&quot;, line 494, in _distributed_apply
      with ops.control_dependencies(update_ops):
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py&quot;, line 5257, in control_dependencies
      return get_default_graph().control_dependencies(control_inputs)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\func_graph.py&quot;, line 356, in control_dependencies
      return super(FuncGraph, self).control_dependencies(filtered_control_inputs)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py&quot;, line 4691, in control_dependencies
      c = self.as_graph_element(c)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py&quot;, line 3610, in as_graph_element
      return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
    File &quot;C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py&quot;, line 3699, in _as_graph_element_locked
      (type(obj).__name__, types_str))
  TypeError: Can not convert a NoneType into a Tensor or Operation.
  </code></pre>
  <p>And the full code is below:</p>
  <pre><code>from numpy import array
  from keras.preprocessing.text import one_hot
  from keras.preprocessing.sequence import pad_sequences
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.layers import Flatten
  from keras.layers.embeddings import Embedding
  
  # define document
  docs = ['Well done!',
          'Good work',
          'Great effort',
          'nice work',
          'Excellent!',
          'Weak',
          'Poor effort!',
          'not good',
          'poor work',
          'Could have done better.']
  
  # define class labels
  labels = array([1,1,1,1,1,0,0,0,0,0])
  
  # integer-encode the documents
  vocab_size = 50
  encoded_docs = [one_hot(d, vocab_size) for d in docs]
  print(encoded_docs)
  
  # padding
  max_length = 4
  padded_docs = pad_sequences(encoded_docs, maxlen = max_length, padding = 'post')
  print(padded_docs)
  
  # define model
  model = Sequential()
  model.add(Embedding(vocab_size, 8, input_length=max_length))
  model.add(Flatten())
  model.add(Dense(1, activation='sigmoid'))
  
  # compile the model
  model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics=['accuracy'])
  
  # summarize
  print(model.summary())
  
  # fit the model
  model.fit(x=padded_docs, y=labels, epochs=50, verbose=0)
  
  # evaluate the model
  loss, accuracy = model.evaluate(x=padded_docs, y=labels, verbose=0)
  print(&quot;Accuracy: {}&quot;.format(accuracy))
  
  </code></pre>
  <p>What's going on here? The article was originally written back in 2017 but its last revision and update was just a week ago. I imagine they are constantly tweaking TensorFlow since it is still state-of-the-art and needs a lot of improvement.</p>
  <p>Any ideas on how to circumvent this?</p>
  <p><strong>Edit:</strong>
  I began trying to figure out where the script could have gone wrong. I will be listing what I found here, hopefully it will help us spot something:</p>
  <ul>
  <li>I found that in <code>ops.py</code>'s <code>control_dependencies()</code> function, <code>control_inputs</code> parameter has the following values: <code>Tensor(&quot;Adam/gradients/gradients/loss/dense_1_loss/binary_crossentropy/logistic_loss_grad/Reshape_1:0&quot;, shape=(None, 1), dtype=float32)</code>, <code>Tensor(&quot;Adam/gradients/gradients/loss/dense_1_loss/binary_crossentropy/logistic_loss/Log1p_grad/mul:0&quot;, shape=(None, 1), dtype=float32)</code>, and <code>None</code>. When it becomes <code>None</code>, the program crashes.</li>
  </ul>