title: ValueError when loading a previously saved retrained VGG16 model using Keras
body: <p>I am using the VGG16 architecture within Keras, which I have retrained to fit my needs in the following way:</p>
  
  <pre><code>vgg16_model = keras.applications.vgg16.VGG16()
  model = Sequential()
  for layer in vgg16_model.layers:
      model.add(layer)
  
  
  model.layers.pop()
  for layer in model.layers:
      layer.trainable = False
  
  model.add(Dense(3, activation='softmax'))
  
  model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])
  </code></pre>
  
  <p>Next I train the model and then I am saving the entire model the way it is suggested within the keras documentation:</p>
  
  <pre><code>from keras.models import load_model
  
  model.save('my_model_vgg16.h5')  # creates a HDF5 file
  </code></pre>
  
  <p>while loading the model like so:</p>
  
  <pre><code>model = load_model('my_model_vgg16.h5')
  </code></pre>
  
  <p>Using the trained model in JupyterNotebook works like a charm. However, when I am trying to load the saved model after restarting the kernel I get the following error:</p>
  
  <pre><code>ValueError: Dimension 0 in both shapes must be equal, but are 4096 and 1000 for 'Assign_30' (op: 'Assign') with input shapes: [4096,3], [1000,3].
  </code></pre>
  
  <p>I can't figure out why this error occurs since I am neither changing the input nor the output size of the model / layers during saving and loading.</p>
  
  <p>For testing purpose I have tried using a much simpler sequential model which I have build from scratch in the same pipleline (i.e. same saving and loading procedures) and this gives me no error. Hence I wonder if there's something I am missing when using a pretrained model (saving and loading it).</p>
  
  <p>For reference, the entire console error-log looks like this:</p>
  
  <pre><code>---------------------------------------------------------------------------
  InvalidArgumentError                      Traceback (most recent call last)
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
      685           graph_def_version, node_def_str, input_shapes, input_tensors,
  --&gt; 686           input_tensors_as_shapes, status)
      687   except errors.InvalidArgumentError as err:
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
      472             compat.as_text(c_api.TF_Message(self.status.status)),
  --&gt; 473             c_api.TF_GetCode(self.status.status))
      474     # Delete the underlying status object from memory otherwise it stays alive
  
  InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 4096 and 1000 for 'Assign_30' (op: 'Assign') with input shapes: [4096,3], [1000,3].
  
  During handling of the above exception, another exception occurred:
  
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-5-a2d2e98db4b6&gt; in &lt;module&gt;()
        1 from keras.models import load_model
  ----&gt; 2 loaded_model = load_model('my_model_vgg16.h5')
        3 print("Loaded Model from disk")
        4 
        5 #compile and evaluate loaded model
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\keras\models.py in load_model(filepath, custom_objects, compile)
      244 
      245         # set weights
  --&gt; 246         topology.load_weights_from_hdf5_group(f['model_weights'], model.layers)
      247 
      248         # Early return if compilation is not required.
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\keras\engine\topology.py in load_weights_from_hdf5_group(f, layers)
     3164                              ' elements.')
     3165         weight_value_tuples += zip(symbolic_weights, weight_values)
  -&gt; 3166     K.batch_set_value(weight_value_tuples)
     3167 
     3168 
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\keras\backend\tensorflow_backend.py in batch_set_value(tuples)
     2363                 assign_placeholder = tf.placeholder(tf_dtype,
     2364                                                     shape=value.shape)
  -&gt; 2365                 assign_op = x.assign(assign_placeholder)
     2366                 x._assign_placeholder = assign_placeholder
     2367                 x._assign_op = assign_op
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\ops\variables.py in assign(self, value, use_locking)
      571       the assignment has completed.
      572     """
  --&gt; 573     return state_ops.assign(self._variable, value, use_locking=use_locking)
      574 
      575   def assign_add(self, delta, use_locking=False):
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\ops\state_ops.py in assign(ref, value, validate_shape, use_locking, name)
      274     return gen_state_ops.assign(
      275         ref, value, use_locking=use_locking, name=name,
  --&gt; 276         validate_shape=validate_shape)
      277   return ref.assign(value)
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\ops\gen_state_ops.py in assign(ref, value, validate_shape, use_locking, name)
       54     _, _, _op = _op_def_lib._apply_op_helper(
       55         "Assign", ref=ref, value=value, validate_shape=validate_shape,
  ---&gt; 56         use_locking=use_locking, name=name)
       57     _result = _op.outputs[:]
       58     _inputs_flat = _op.inputs
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
      785         op = g.create_op(op_type_name, inputs, output_types, name=scope,
      786                          input_types=input_types, attrs=attr_protos,
  --&gt; 787                          op_def=op_def)
      788       return output_structure, op_def.is_stateful, op
      789 
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
     2956         op_def=op_def)
     2957     if compute_shapes:
  -&gt; 2958       set_shapes_for_outputs(ret)
     2959     self._add_op(ret)
     2960     self._record_op_seen_by_control_dependencies(ret)
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\ops.py in set_shapes_for_outputs(op)
     2207       shape_func = _call_cpp_shape_fn_and_require_op
     2208 
  -&gt; 2209   shapes = shape_func(op)
     2210   if shapes is None:
     2211     raise RuntimeError(
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\ops.py in call_with_requiring(op)
     2157 
     2158   def call_with_requiring(op):
  -&gt; 2159     return call_cpp_shape_fn(op, require_shape_fn=True)
     2160 
     2161   _call_cpp_shape_fn_and_require_op = call_with_requiring
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\common_shapes.py in call_cpp_shape_fn(op, require_shape_fn)
      625     res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
      626                                   input_tensors_as_shapes_needed,
  --&gt; 627                                   require_shape_fn)
      628     if not isinstance(res, dict):
      629       # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).
  
  ~\Anaconda2\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
      689       missing_shape_fn = True
      690     else:
  --&gt; 691       raise ValueError(err.message)
      692 
      693   if missing_shape_fn:
  
  ValueError: Dimension 0 in both shapes must be equal, but are 4096 and 1000 for 'Assign_30' (op: 'Assign') with input shapes: [4096,3], [1000,3].
  </code></pre>