title: Cross Validation in Keras
body: <p>I'm implementing a Multilayer Perceptron in Keras and using scikit-learn to perform cross-validation. For this, I was inspired by the code found in the issue <a href="https://github.com/keras-team/keras/issues/1711#issuecomment-185801662" rel="noreferrer">Cross Validation in Keras</a></p>
  
  <pre class="lang-python prettyprint-override"><code>from sklearn.cross_validation import StratifiedKFold
  
  def load_data():
      # load your data using this function
  
  def create model():
      # create your model using this function
  
  def train_and_evaluate__model(model, data[train], labels[train], data[test], labels[test)):
      # fit and evaluate here.
  
  if __name__ == &quot;__main__&quot;:
      X, Y = load_model()
      kFold = StratifiedKFold(n_splits=10)
      for train, test in kFold.split(X, Y):
          model = None
          model = create_model()
          train_evaluate(model, X[train], Y[train], X[test], Y[test])
  </code></pre>
  <p>In my studies on neural networks, I learned that the knowledge representation of the neural network is in the synaptic weights and during the network tracing process, the weights that are updated to thereby reduce the network error rate and improve its performance. (In my case, I'm using Supervised Learning)</p>
  <p>For better training and assessment of neural network performance, a common method of being used is cross-validation that returns partitions of the data set for training and evaluation of the model.</p>
  <p>My doubt is...</p>
  <p>In this code snippet:</p>
  <pre class="lang-python prettyprint-override"><code>for train, test in kFold.split(X, Y):
      model = None
      model = create_model()
      train_evaluate(model, X[train], Y[train], X[test], Y[test])
  </code></pre>
  <p>We define, train and evaluate a new neural net for each of the generated partitions?</p>
  <p>If my goal is to fine-tune the network for the entire dataset, why is it not correct to define a single neural network and train it with the generated partitions?</p>
  <p>That is, why is this piece of code like this?</p>
  <pre class="lang-python prettyprint-override"><code>for train, test in kFold.split(X, Y):
      model = None
      model = create_model()
      train_evaluate(model, X[train], Y[train], X[test], Y[test])
  </code></pre>
  <p>and not so?</p>
  <pre class="lang-python prettyprint-override"><code>model = None
  model = create_model()
  for train, test in kFold.split(X, Y):
      train_evaluate(model, X[train], Y[train], X[test], Y[test])
  </code></pre>
  <p>Is my understanding of how the code works wrong? Or my theory?</p>