How to extract train and validation sets in Keras?

2020-04-15 06:10发布

问题:

I implement a neural net in keras, with the following structure:

model = Sequential([... layers ...])
model.compile(optimizer=..., loss=...)
hist=model.fit(x=X,y=Y, validation_split=0.1, epochs=100)

Is there a way to extract from either model or hist the train and validation sets? That is, I want to know which indices in X and Y were used for training and which were used for validation.

回答1:

Keras splits the dataset at

split_at = int(x[0].shape * (1-validation_split))

into the train and validation part. So if you have n samples, the first int(n*(1-validation_split)) samples will be the training sample, the remainder is the validation set.

If you want to have more control, you can split the dataset yourself and pass the validation dataset with the parameter validation_data:

model.fit(train_x, train_y, …, validation_data=(validation_x, validation_y))