title: Fitting Keras Sequential Model gives ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
body: <p>I have the following array of lists (actors per movie):</p>
  <pre class="lang-py prettyprint-override"><code>partial_x_train_actors=array([list([b'victor mclaglen', b'jon hall', b'frances farmer', b'olympe bradna', b'gene lockhart', b'douglass dumbrille', b'francis ford', b'ben welden', b'abner biberman', b'pedro de cordoba', b'rudy robles', b'bobby stone', b'nellie duran', b'james flavin', b'nina campana']),
         list([b'jessica biel', b'ben barnes', b'kristin scott thomas', b'colin firth', b'kimberley nixon', b'katherine parkinson', b'kris marshall', b'christian brassington', b'charlotte riley', b'jim mcmanus', b'pip torrens', b'jeremy hooton', b'joanna bacon', b'maggie hickey', b'georgie glen']),
         list([b'gr\xc3\xa9gori derang\xc3\xa8re', b'anouk grinberg', b'aur\xc3\xa9lien recoing', b'niels arestrup', b'yann collette', b'laure duthilleul', b'david assaraf', b'pascal demolon', b'jean-baptiste iera', b'richard sammel', b'vincent crouzet', b'fred epaud', b'pascal elso', b'nicolas giraud', b'micha\xc3\xabl abiteboul']),
         ...,
         list([b'jason schwartzman', b'mickey rourke', b'brittany murphy', b'john leguizamo', b'patrick fugit', b'mena suvari', b'chloe hunter', b'elisa bocanegra', b'julia mendoza', b'china chow', b'nicholas gonzalez', b'debbie harry', b'josh peck', b'charlotte ayanna', b'eric roberts']),
         list([b'fred kirschenmann', b'daniel salatin', b'joel salatin', b'paul willis', b'chuck wirtz']),
         list([b'jan sebastian', b'tray loren', b'paul muzzcat', b'brad koepenick', b'jerry armstrong', b'ben sebastian', b'reyn hubbard', b'levita gros', b'betty flemming', b'randolph parro', b'susan serigny', b'keith gros', b'rocky dugas', b'sid larrwiere', b'jocelyn boudreaux'])],
        dtype=object)
  </code></pre>
  <p>Since I want to use that as an input to a Keras model I have to convert the array of lists to an array of arrays. To do this I am running the following code, taken from <a href="https://stackoverflow.com/questions/57760510/converting-array-of-lists-to-keras-input">this SO question</a></p>
  <pre class="lang-py prettyprint-override"><code>partial_x_train_actors_array=[]
  
  for i in range(len(partial_x_train_actors)):
      
      partial_x_train_actors_array.append(np.array(list(x for x in partial_x_train_actors[i])))
  
  partial_x_train_actors_array = np.asarray(partial_x_train_actors_array)=
  type(partial_x_train_actors_array[0])
  </code></pre>
  <p>So now I get this:</p>
  <pre class="lang-py prettyprint-override"><code>array([array([b'victor mclaglen', b'jon hall', b'frances farmer',
         b'olympe bradna', b'gene lockhart', b'douglass dumbrille',
         b'francis ford', b'ben welden', b'abner biberman',
         b'pedro de cordoba', b'rudy robles', b'bobby stone',
         b'nellie duran', b'james flavin', b'nina campana'], dtype='|S18'),
         array([b'jessica biel', b'ben barnes', b'kristin scott thomas',
         b'colin firth', b'kimberley nixon', b'katherine parkinson',
         b'kris marshall', b'christian brassington', b'charlotte riley',
         b'jim mcmanus', b'pip torrens', b'jeremy hooton', b'joanna bacon',
         b'maggie hickey', b'georgie glen'], dtype='|S21'),
         array([b'gr\xc3\xa9gori derang\xc3\xa8re', b'anouk grinberg',
         b'aur\xc3\xa9lien recoing', b'niels arestrup', b'yann collette',
         b'laure duthilleul', b'david assaraf', b'pascal demolon',
         b'jean-baptiste iera', b'richard sammel', b'vincent crouzet',
         b'fred epaud', b'pascal elso', b'nicolas giraud',
         b'micha\xc3\xabl abiteboul'], dtype='|S19'),
         ...,
         array([b'jason schwartzman', b'mickey rourke', b'brittany murphy',
         b'john leguizamo', b'patrick fugit', b'mena suvari',
         b'chloe hunter', b'elisa bocanegra', b'julia mendoza',
         b'china chow', b'nicholas gonzalez', b'debbie harry', b'josh peck',
         b'charlotte ayanna', b'eric roberts'], dtype='|S17'),
         array([b'fred kirschenmann', b'daniel salatin', b'joel salatin',
         b'paul willis', b'chuck wirtz'], dtype='|S17'),
         array([b'jan sebastian', b'tray loren', b'paul muzzcat',
         b'brad koepenick', b'jerry armstrong', b'ben sebastian',
         b'reyn hubbard', b'levita gros', b'betty flemming',
         b'randolph parro', b'susan serigny', b'keith gros', b'rocky dugas',
         b'sid larrwiere', b'jocelyn boudreaux'], dtype='|S17')],
        dtype=object)
  </code></pre>
  <p>But neither this is sufficient to get away from the type of the input Tensor, since I get this error:</p>
  <pre class="lang-py prettyprint-override"><code>ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
  </code></pre>
  <p><strong>My model fit process</strong></p>
  <pre class="lang-py prettyprint-override"><code># import the pre-trained model
  model = &quot;https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1&quot;
  hub_layer = hub.KerasLayer(model, output_shape=[20], input_shape=[], dtype=tf.string, trainable=True)
  
  # create the neural network structure
  model = tf.keras.Sequential(name=&quot;English_Google_News_130GB_witout_OOV_tokens&quot;)
  model.add(hub_layer)
  model.add(tf.keras.layers.Flatten())
  model.add(tf.keras.layers.Dense(i, kernel_regularizer=regularizers.l2(neural_network_parameters['l2_regularization']),
                                          activation=neural_network_parameters['dense_activation']))
  model.add(tf.keras.layers.Dropout(neural_network_parameters['dropout_rate']))
  model.add(tf.keras.layers.Dense(y_val.shape[1], 
  
  activation=neural_network_parameters['output_activation']))
          
  #model.name(&quot;English Google News 130GB witout OOV tokens&quot;)
  print(model.summary())
          
  #instantiate Optimizer
  optimizer = optimizer_adam_v2(len(partial_x_train_actors_array), validation_split_ratio, i)
  
  model.compile(optimizer=optimizer,
                loss=neural_network_parameters['model_loss'],
                metrics=[neural_network_parameters['model_metric']])
  
  plot_model(model, to_file=os.path.join(os.getcwd(), 'model_three\\network_structure_english_google_news_without_OOV_model_{0}.png'.format(version_data_control)))
  
  history = model.fit([partial_x_train_features, partial_x_train_plot, partial_x_train_actors_array, partial_x_train_reviews],
                          partial_y_train,
                          steps_per_epoch=int(np.ceil((len(partial_x_train_actors_array)*0.8)//16)),
                          epochs=100,
                          batch_size=16,
                          validation_split=0.2
                          verbose=0,
                          callbacks=callback(&quot;english_google_news_without_oovtokens&quot;, model))
  </code></pre>
  <p>[EDIT] - 04.07.2020</p>
  <p>I would like to add that I have done the padding on sequences for another experiment and the actor's list that presented above is transformed to list below</p>
  <pre class="lang-py prettyprint-override"><code>partial_x_train_actors=array([[ 2024,  3228,   451, ..., 18119,     0,     0],
         [ 3230,  7889, 12357, ...,     0,     0,     0],
         [20001, 20001, 20001, ...,     0,     0,     0],
         ...,
         [ 6887, 20001, 15352, ..., 20001, 20001, 20001],
         [10206, 20001,  3426, ..., 20001,     0,     0],
         [ 2969,  5903,   447, ...,     0,     0,     0]])
  </code></pre>
  <p>However, when I apply this list in the .fit() of the neural network I get the following error</p>
  <pre class="lang-py prettyprint-override"><code>ValueError: Error when checking input: expected keras_layer_4_input to have 1 dimensions, but got array with shape (39192, 17)
  </code></pre>
  <p>(39192, 17) is the shape of the actors array</p>
  <p>[EDIT 2] - 05.07.2020</p>
  <p><strong>Trial 1</strong> (failed)</p>
  <p>Based on some proposals on the answers provided I tried to change input shape of the hub.Keraslayer:</p>
  <pre><code>hub_layer = hub.KerasLayer(model, output_shape=[20], input_shape=[len(y_train)], dtype=tf.string, trainable=True)
  </code></pre>
  <p>I made it equal to my training_input length #39192 data per actor, plot, features, reviews.</p>
  <p>Error produced:
  <a href="https://i.stack.imgur.com/33KHF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/33KHF.png" alt="enter image description here" /></a>
  From the error, I can guess that the input_shape should be []?</p>
  <p><strong>Trial 2</strong> (failed)</p>
  <pre class="lang-py prettyprint-override"><code>#list of actors (training data) tensors
  actors_training_tensors=np.array([tf.convert_to_tensor(partial_x_train_actors[i]) for i in range(len(partial_x_train_actors))])
  actors_testing_tensors=np.array([tf.convert_to_tensor(x_val_actors[i]) for i in range(len(x_val_actors))])
  </code></pre>
  <p>Error again:</p>
  <pre><code>ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor).
  </code></pre>
  <p>I transformed the input list of actors to tensor. Note that only the actor's list has problems because they are stored as names in a list [[name1, name2, name3]]. I had no problem neither with plot, features, or reviews inputs because they are saved as a list of corpus.</p>
  <p><strong>Trial 3</strong> (failed)</p>
  <p>Based on the comments I used the data API likewise:</p>
  <pre class="lang-py prettyprint-override"><code>data_tf=tf.data.Dataset.from_tensor_slices([partial_x_train_features, partial_x_train_plot, partial_x_train_actors_array, partial_x_train_reviews])
  </code></pre>
  <p>Again I got an error:</p>
  <pre><code>ValueError: Can't convert Python sequence with mixed types to Tensor.
  </code></pre>
  <p>So I searched about it, and I figured out this <a href="https://stackoverflow.com/questions/49824872/convert-python-sequence-with-multiple-datatypes-to-tensor">question</a> and the <a href="https://www.tensorflow.org/guide/tensor#data_types" rel="nofollow noreferrer">documentation</a>,
  I did the following change, (added tf.constant):</p>
  <pre class="lang-py prettyprint-override"><code>data_tf=tf.data.Dataset.from_tensor_slices([tf.constant(partial_x_train_features), tf.constant(partial_x_train_plot), tf.constant(partial_x_train_actors_array), tf.constant(partial_x_train_reviews)])
  </code></pre>
  <p>Moreover, it seems that I cannot convert a NumPy array of strings to a Tensor of floats. Probably here is where the padding of sequences plays an important role. However, if you follow <a href="https://www.tensorflow.org/hub/tutorials/tf2_text_classification" rel="nofollow noreferrer">this link</a> of a tensorflow article from which I got the idea, you will notice that the user gives as input only byte strings and not padded sequences.</p>
  <p><em>Please note that a solution to all of this, is just to flatten the list of actors by using the &quot; &quot;.join() command. However, the actors would be just a text of names and not a separate name. Even though it works, I think that for better results the actors should be given as separate names because a Neural Network cannot distinguish names on its own.</em></p>
  <p>[INPUT DATA FOR DEBUGGING - ISSUE REPLICATION]</p>
  <p>In case someone wants to replicate and debug the problem, below I represents my 4 input layers (sample of data) and <a href="https://www.tensorflow.org/hub/tutorials/tf2_text_classification" rel="nofollow noreferrer">the article from Tensorflow</a> that I have followed.</p>
  <p>Here is the Issue's <a href="https://github.com/tensorflow/tensorflow/issues/41109" rel="nofollow noreferrer">GitHub link</a> with the question posted. As it seems when I run the code locally everything looks fine apart from the EarlyStopping error presented in the GitHub issue attached. I will re-check the data that I use, because the data provided in the GitHub link are the proper data to be used.</p>