Search code examples
pythontensorflowkerastalos

TensorFlow / Keras splitting training and validation data


I'm learning how to use TensorFlow and have been given a working model, built in the Keras structure. It runs but the results are a bit of mystery to me. I'm attempting to copy this and simplify it to its bare essence and then build it back up again. The part I can not understand at all is how/where it splits the training data input into training and validation sets? I've checked the model code, initial parameters, etc. Is there a built-in function in TensorFlow convolutional neural networks that does this automatically?

The call to Talos looks like this, the first two values are x-training and y-training values nowhere is x_val or y_val passed to the Talos function. Could Talos have an automatic way of producing x_val and y_val?

jam1 = talos.Scan(features3,
                  label2[0,],
                  model = DLAt,
                  params = ParamsJam1,
                  experiment_name = "toy1",
                  fraction_limit=.2)

def DLAt(x_train, y_train, x_val, y_val, params):
    model = Sequential()
    convLayer = Conv1D(filters=params['numFilters'],
                     kernel_size=params['kernalLen'], strides=1, activation='relu',
                     input_shape=(300,4), use_bias=True)
    model.add(convLayer)
    model.add(MaxPooling1D(pool_size=params['maxpool']))
    model.add(Flatten())
    firstHidden = Dense(params['neuronsInLayerOne'], activation='relu',
                  kernel_regularizer=regularizers.l1_l2(l1=params['l1'], l2=0))
    model.add(firstHidden)
    model.add(Dropout(params['dropoutLevel']))
    model.add(Dense(params['neuronsInLayerTwo'], activation='relu'))
    model.add(Dropout(params['dropoutLevel']))
    model.add(Dense(1, activation='sigmoid')) 
    opt = keras.optimizers.Adam(lr=params['lr'])
    model.compile(optimizer = opt, loss = 'loss', metrics = ['mse'])
    out = model.fit(x_train, y_train, epochs = params['epoch'],
                  batch_size =params['batches'],
                  validation_data =(x_val,  y_val))
    return out, model

Solution

  • It is not splitting the training data at all and you are explicitly passing validation data to model.fit via the parameter validation_data:

    out = model.fit(x_train, y_train, epochs = params['epoch'],
                      batch_size =params['batches'],
                      validation_data =(x_val,  y_val))
    

    If you want to split your training data and do not want to provide validation data, you can use the validation_split parameter in model.fit(...), which is the fraction of the training data to be used as validation data.. By default, it is set to 0.0.

    Update 1: Check the source code of talos.Scan, it uses a validation_split of 0.3 by default. Also, check this. It should then be self-explanatory.