Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Keras + tensorflow datasets and custom callbacks with validation datasets #8287

Closed
kmkolasinski opened this issue Oct 29, 2017 · 2 comments

Comments

@kmkolasinski
Copy link

Hello,
recently I've started to work with Keras together with tensorflow datasets.
The API I use is following:

  • I create net which inputs are tf.Tensors generated by iterator
  • Then I compile the model, by providing target tensors: target_tensors
model.compile(Adam(lr=lr), loss=losses, metrics=metrics, target_tensors=target_tensors)
  • Then fit the model by calling model.fit(...)

However, it's not clear to me how could I add validation datasets into training loop, like it was done in previous API. I understand that datasets are quite new in Keras, but maybe there are some examples which could be useful.
Thanks in advance.

@zotroneneis
Copy link

I'm not sure whether I understand your question correctly. There are two easy ways in which you can include validation data in the training process:

  1. Tell the model.fit() function to use part of your training data for validation, e.g.
    model.fit(x, y, validation_split=0.3)

  2. Tell the model.fit() function which validation data to use
    model.fit(x, y, validation_data=(x_val, y_val))

@kmkolasinski
Copy link
Author

Hi, thanks for answer, however my question was about how to add properly evaluation on validation data when working with tensorflow datasets API, which reads data directly from tfrecords. I mean this. Normally, I read data from e.g. *.tfrecords files and create model which inputs tensors take data from tf dataset. So basically for training data I run:

...
iterator = dataset.make_initializable_iterator()
training_filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
sess.run(iterator.initializer, feed_dict={filenames: training_filenames})

The workaround for my problem would be to write special callback, which evaluates model after each epoch on other dataset which reads from validation_filenames and then switch back to training_filenames. This works, but maybe there is a simpler one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants