How to extract train and validation sets in Keras?

2020-04-15 05:52发布

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条回答
家丑人穷心不美
2楼-- · 2020-04-15 06:17

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))
查看更多
登录 后发表回答