title: Normalize the Validation Set for a Neural Network in Keras
body: <p>So, I understand that normalization is important to train a neural network. </p>
  
  <p>I also understand that I have to normalize validation- and test-set with the parameters from the training set (see e.g. this discussion: <a href="https://stats.stackexchange.com/questions/77350/perform-feature-normalization-before-or-within-model-validation">https://stats.stackexchange.com/questions/77350/perform-feature-normalization-before-or-within-model-validation</a>)</p>
  
  <p>My question is: How do I do this in Keras?</p>
  
  <p>What I'm currently doing is:</p>
  
  <pre><code>import numpy as np
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.callbacks import EarlyStopping
  
  def Normalize(data):
      mean_data = np.mean(data)
      std_data = np.std(data)
      norm_data = (data-mean_data)/std_data
      return norm_data
  
  input_data, targets = np.loadtxt(fname='data', delimiter=';')
  norm_input = Normalize(input_data)
  
  model = Sequential()
  model.add(Dense(25, input_dim=20, activation='relu'))
  model.add(Dense(1, activation='sigmoid'))
  
  model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  
  early_stopping = EarlyStopping(monitor='val_acc', patience=50) 
  model.fit(norm_input, targets, validation_split=0.2, batch_size=15, callbacks=[early_stopping], verbose=1)
  </code></pre>
  
  <p>But here, I <strong>first</strong> normalize the data w.r.t. the whole data set and <strong>then</strong> split up the validation set, which is wrong according to the above mentioned discussion. </p>
  
  <p>It wouldn't be a big deal to save the mean and standard deviation from the training set(training_mean and training_std), but how can I apply the normalization with the training_mean and training_std on the validation set separately?</p>