Keras - Python Deep Learning Neural Network API

Deep Learning Course 2 of 4 - Level: Beginner

Code Update for CNN Training with TensorFlow's Keras API

video

expand_more chevron_left

text

expand_more chevron_left

Code Update for CNN Training with TensorFlow's Keras API

In this episode, we’ll discuss an update that we’ll need to know regarding the upcoming code before building and training our first convolutional neural network (CNN).

In the upcoming episodes, we’ll demonstrate how to train a CNN using the image data we organized and processed previously.

Recall that we stored the image data in a Keras Sequence, specifically a DirectoryIterator, using the ImageDataGenerator.flow_from_directory() function. This function generates batches of image data from the specified location on disk.

As you’ve seen in a previous episode, when we train a model, we call the fit() function on the model and pass in the training data. We’ve seen how this was done when our training data was stored in a simple numpy array, but in the upcoming CNN episodes, we’ll see how this is done specifically for our training data that we stored in a DirectoryIterator.

Recently, there was a change introduced by TensorFlow that now requires us to pass in another parameter to the fit() function when our data is stored in an infinitely repeating data set, like a DirectoryIterator.

Note that a DirectoryIterator is indeed an infinitely repeating data set, as the batches of data generated by the iterator will continue to come infinitely as long as we want them to. You can see this by passing the iterator to the built-in Python next() function and running it over and over again to infinitely generate new batches of data.

Required steps_per_epoch Parameter

Now, back to the parameter that is required to be passed to the fit() function for this type of data. This parameter is called steps_per_epoch and should be set to the number of steps (batches of samples) to yield from the training set before declaring one epoch finished and starting the next epoch.

This is typically set to be equal to the number of samples in our training set divided by the batch size. For example, if we have 100 training images, and our batch size is 5, then we would set steps_per_epoch=20.

This parameter actually isn’t new, however, in previous TensorFlow versions, it was not required to be specified when our data was stored in a Keras Sequence, like the DirectoryIterator we’ve stored our data in. Instead TensorFlow would default to using the size of the data set divided by the batch size as the number of steps_per_epoch.

Depending on which version of TensorFlow you’re running, if you don’t specify this parameter, then model.fit() will run infinitely on the first epoch and never complete.

Additional Required Parameters

Note that in addition to steps_per_epoch that we specify regarding the training data when we call model.fit(), we also need to specify a parameter called validation_steps if we are also passing in validation data to the model. This parameter acts in the exact same way as steps_per_epoch, except for on our validation set.

Lastly, when we use the model for inference by calling predict() on the model and passing in the test set, we need to also specify the parameter called steps here. In this case, this is the number of steps (batches of samples) to yield from the test set before declaring the prediction round finished.

Tracking the Issue

It’s unclear whether this parameter will continue to be required or not, as it was initially required for this type of infinitely repeating data ste, and then not required, and now required again. You can track this issue on TensorFlow’s Github if you’re interested.

In the upcoming episodes when we call fit() or predict(), you will see that in the video portion of the episodes, steps_per_epoch, validation_steps, and steps have not been specified. However, the corresponding blogs will have updated code for whatever TensorFlow requires at the time. So, if the parameters are required, they will be set in the blog. If the parameters are no longer required, they will not be set in the blog.

Now we’re ready to begin building and training our first convolutional neural network in the next episode.

quiz

expand_more chevron_left

resources

expand_more chevron_left
In this episode, we’ll discuss an update that we’ll need to know regarding the upcoming code before building and training our first convolutional neural network (CNN). 🕒🦎 VIDEO SECTIONS 🦎🕒 00:00 Welcome to DEEPLIZARD - Go to deeplizard.com for learning resources 00:30 Review Keras fit function 02:04 TensorFlow code change 03:57 Info for moving forward in the course 04:40 More code changes 06:33 Collective Intelligence and the DEEPLIZARD HIVEMIND 💥🦎 DEEPLIZARD COMMUNITY RESOURCES 🦎💥 👋 Hey, we're Chris and Mandy, the creators of deeplizard! 👀 CHECK OUT OUR VLOG: 🔗 https://youtube.com/deeplizardvlog 👉 Check out the blog post and other resources for this video: 🔗 https://deeplizard.com/learn/video/k63bRldLxGI 💻 DOWNLOAD ACCESS TO CODE FILES 🤖 Available for members of the deeplizard hivemind: 🔗 https://deeplizard.com/resources 🧠 Support collective intelligence, join the deeplizard hivemind: 🔗 https://deeplizard.com/hivemind 🤜 Support collective intelligence, create a quiz question for this video: 🔗 https://deeplizard.com/create-quiz-question 🚀 Boost collective intelligence by sharing this video on social media! ❤️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Tammy Prash Guy Payeur Christian Sikuq 👀 Follow deeplizard: Our vlog: https://youtube.com/deeplizardvlog Facebook: https://facebook.com/deeplizard Instagram: https://instagram.com/deeplizard Twitter: https://twitter.com/deeplizard Patreon: https://patreon.com/deeplizard YouTube: https://youtube.com/deeplizard 🎓 Deep Learning with deeplizard: Fundamental Concepts - https://deeplizard.com/learn/video/gZmobeGL0Yg Beginner Code - https://deeplizard.com/learn/video/RznKVRTFkBY Intermediate Code - https://deeplizard.com/learn/video/v5cngxo4mIg Advanced Deep RL - https://deeplizard.com/learn/video/nyjbcRQ-uQ8 🎓 Other Courses: Data Science - https://deeplizard.com/learn/video/d11chG7Z-xk Trading - https://deeplizard.com/learn/video/ZpfCK_uHL9Y 🛒 Check out products deeplizard recommends on Amazon: 🔗 https://amazon.com/shop/deeplizard 📕 Get a FREE 30-day Audible trial and 2 FREE audio books using deeplizard’s link: 🔗 https://amzn.to/2yoqWRn 🎵 deeplizard uses music by Kevin MacLeod 🔗 https://youtube.com/channel/UCSZXFhRIx6b0dFX3xS8L1yQ 🔗 http://incompetech.com/ ❤️ Please use the knowledge gained from deeplizard content for good, not evil.

updates

expand_more chevron_left

Updates to the information on this page!

Did you know you that deeplizard content is regularly updated and maintained?

  • Updated
  • Maintained

Spot something that needs to be updated? Don't hesitate to let us know. We'll fix it!


All relevant updates for the content on this page are listed below.

So far, so good! The content on this page hasn't required any updates thus far.