title: Error after importing layers weights from CSV to Keras model
body: <p>No errors when setting weights with data from CSV's but having problems when evaluating the model with the Keras loaded weights.</p>
  
  <p>I know that I can save the weights in a .h5 file but I'm restricted to only work with CSV files.</p>
  
  <p>I've tried calling:<br>
      <strong>keras.backend.clear_session()</strong><br>
      <strong>keras.backend.set_learning_phase(0)</strong>  </p>
  
  <p>after setting the weights but before evaluating the model.</p>
  
  <p>Clearing session seemed to work a little better, the error message changed after.</p>
  
  <h1>Model architecture</h1>
  
  <pre><code>import tensorflow as tf  
  from tensorflow import keras  
  import numpy as np  
  from keras import models  
  from keras import layers  
  from keras import regularizers  
  
  model = models.Sequential()  
  model.add(layers.Embedding(512, 64))  
  model.add(layers.Conv1D(64, 5, activation='elu'))  
  
  model.add(layers.MaxPooling1D(3))  
  model.add(layers.Conv1D(32, 3, activation='elu'))  
  model.add(layers.Bidirectional(layers.LSTM(32, kernel_regularizer=regularizers.l2(0.001)), input_shape=(None, 32)))  
  model.add(layers.Dense(3, activation='softmax'))  
  
  model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])  
  
  model.summary()  
  </code></pre>
  
  <h1>Loading weights</h1>
  
  <p>After importing CSV files and reshaped the data into numpy arrays with the shape it had after training the model (in a different notebook)<br>
  <strong>L1</strong>: numpy array shape (1, 512, 64)<br>
  <strong>L2</strong>: numpy array shape ((5, 64, 64), (64,))<br>
  <strong>L3</strong>: numpy array shape (0,)<br>
  <strong>L4</strong>: numpy array shape ((3, 64, 32), (32,))<br>
  <strong>L5</strong>: numpy array shape ((32, 128), (32, 128), (128,), (32, 128), (32, 128), (128,))<br>
  <strong>L6</strong>: numpy array from importing CSV files. Shape ((64, 3), (3,))  </p>
  
  <pre><code>W = [L1, L2, L3, L4, L5, L6]  
  
  x = 0  
  for layer in model.layers:  
      layer.set_weights(W[x])  
      x += 1  
  </code></pre>
  
  <h1>Running the model</h1>
  
  <p>So far, no error when running the code.<br>
  Now I upload the same data that I used for training the model to test if the weights loaded correctly:<br>
  <strong>labels2</strong>: vectorized labes (one hot encoded) shape (85261, 3)<br>
  <strong>features</strong>: numpy array with shape (85261, 80)  </p>
  
  <pre><code>keras.backend.clear_session()  
  keras.backend.set_learning_phase(0)  
  
  scores = model.evaluate(features, labels2, batch_size=512, verbose=1, steps=None)  
  </code></pre>
  
  <p>I expected to get the same results when trained the original model. The data (features, labels) is the same. But can't run evaluate or predict commands.</p>
  
  <p>I get the following error:</p>
  
  <pre><code>InvalidArgumentErrorTraceback (most recent call last)
  &lt;ipython-input-6-bb913425426a&gt; in &lt;module&gt;()
        1 
  ----&gt; 2 scores = model.evaluate(features, labels2, batch_size=512, verbose=1, steps=None)
        3 print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
  
  /usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in evaluate(self, x, y, batch_size, verbose, sample_weight, steps)
     1111                                          batch_size=batch_size,
     1112                                          verbose=verbose,
  -&gt; 1113                                          steps=steps)
     1114 
     1115     def predict(self, x,
  
  /usr/local/lib/python2.7/dist-packages/keras/engine/training_arrays.pyc in test_loop(model, f, ins, batch_size, verbose, steps)
      390                 ins_batch[i] = ins_batch[i].toarray()
      391 
  --&gt; 392             batch_outs = f(ins_batch)
      393             if isinstance(batch_outs, list):
      394                 if batch_index == 0:
  
  /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
     2713                 return self._legacy_call(inputs)
     2714 
  -&gt; 2715             return self._call(inputs)
     2716         else:
     2717             if py_any(is_tensor(x) for x in inputs):
  
  /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in _call(self, inputs)
     2673             fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
     2674         else:
  -&gt; 2675             fetched = self._callable_fn(*array_vals)
     2676         return fetched[:len(self.outputs)]
     2677 
  
  /usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in __call__(self, *args, **kwargs)
     1437           ret = tf_session.TF_SessionRunCallable(
     1438               self._session._session, self._handle, args, status,
  -&gt; 1439               run_metadata_ptr)
     1440         if run_metadata:
     1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
  
  /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/errors_impl.pyc in __exit__(self, type_arg, value_arg, traceback_arg)
      526             None, None,
      527             compat.as_text(c_api.TF_Message(self.status.status)),
  --&gt; 528             c_api.TF_GetCode(self.status.status))
      529     # Delete the underlying status object from memory otherwise it stays alive
      530     # as there is a reference to status from this from the traceback due to
  
  InvalidArgumentError: indices[192,4] = -1 is not in [0, 512)
       [[{{node embedding_1/embedding_lookup}} = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_1/embeddings/read, embedding_1/Cast, embedding_1/embedding_lookup/axis)]]  
  </code></pre>
  
  <p>If I add <strong>keras.backend.clear_session()</strong> before evaluating the model, I get the folling error message:</p>
  
  <pre><code>ValueError: Tensor Tensor("loss/add_1:0", shape=(), dtype=float32) is not an element of this graph.  
  </code></pre>