title: How to load a model from an HDF5 file in Keras?
body: <p>How to load a model from an HDF5 file in Keras?</p>
  
  <p>What I tried:</p>
  
  <pre><code>model = Sequential()
  
  model.add(Dense(64, input_dim=14, init='uniform'))
  model.add(LeakyReLU(alpha=0.3))
  model.add(BatchNormalization(epsilon=1e-06, mode=0, momentum=0.9, weights=None))
  model.add(Dropout(0.5))
  
  model.add(Dense(64, init='uniform'))
  model.add(LeakyReLU(alpha=0.3))
  model.add(BatchNormalization(epsilon=1e-06, mode=0, momentum=0.9, weights=None))
  model.add(Dropout(0.5))
  
  model.add(Dense(2, init='uniform'))
  model.add(Activation('softmax'))
  
  
  sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
  model.compile(loss='binary_crossentropy', optimizer=sgd)
  
  checkpointer = ModelCheckpoint(filepath="/weights.hdf5", verbose=1, save_best_only=True)
  model.fit(X_train, y_train, nb_epoch=20, batch_size=16, show_accuracy=True, validation_split=0.2, verbose = 2, callbacks=[checkpointer])
  </code></pre>
  
  <p>The above code successfully saves the best model to a file named weights.hdf5. What I want to do is then load that model. The below code shows how I tried to do so:</p>
  
  <pre><code>model2 = Sequential()
  model2.load_weights("/Users/Desktop/SquareSpace/weights.hdf5")
  </code></pre>
  
  <p>This is the error I get:</p>
  
  <pre><code>IndexError                                Traceback (most recent call last)
  &lt;ipython-input-101-ec968f9e95c5&gt; in &lt;module&gt;()
        1 model2 = Sequential()
  ----&gt; 2 model2.load_weights("/Users/Desktop/SquareSpace/weights.hdf5")
  
  /Applications/anaconda/lib/python2.7/site-packages/keras/models.pyc in load_weights(self, filepath)
      582             g = f['layer_{}'.format(k)]
      583             weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
  --&gt; 584             self.layers[k].set_weights(weights)
      585         f.close()
      586 
  
  IndexError: list index out of range
  </code></pre>