python - Normalize the Validation Set for a Neural Network in Keras -


so, understand normalization important train neural network.

i understand have normalize validation- , test-set parameters training set (see e.g. discussion: https://stats.stackexchange.com/questions/77350/perform-feature-normalization-before-or-within-model-validation)

my question is: how do in keras?

what i'm doing is:

import numpy np keras.models import sequential keras.layers import dense 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) 

but here, first normalize data w.r.t. whole data set , then split validation set, wrong according above mentioned discussion.

it wouldn't big deal save mean , standard deviation training set(training_mean , training_std), how can apply normalization training_mean , training_std on validation set separately?

you can split data training , testing dataset manually before fitting model sklearn.model_selection.train_test_split. afterwards, normalize training , testing data separately , call model.fit validation_data argument.

code example

import numpy np sklearn.model_selection import train_test_split  data = np.random.randint(0,100,200).reshape(20,10) target = np.random.randint(0,1,20)  x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2)  x_train = normalize(x_train) x_test = normalize(x_test)  model.fit(x_train, y_train, validation_data=(x_test,y_test), batch_size=15, callbacks=[early_stopping], verbose=1) 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -