It is assumed that you’re already familiar with concepts of Recurrent Neural Networks (RNNs) and with Long short-term memory (LSTM) architecture in particular.

In case if these concepts are new to you I would highly recommend taking a Deep Learning Specialization on Coursera by Andrew Ng. It also might be beneficial to go through the Unreasonable Effectiveness of Recurrent Neural Networks article by Andrej Karpathy.

On a high level, Recurrent Neural Network (RNN) is a class of deep neural networks, most commonly applied to sequence-based data like speech, voice, text or music. They are used for machine translation, speech recognition, voice synthesis, etc. The key feature of RNNs is that they are stateful, and they have an internal memory in which some context for the sequence may be stored. For example, if the first word of the sequence was He, the RNN might suggest the next word to _speaks _instead of just _speak _(to form a He speaks phrase) because the prior knowledge about the first word _He _is already inside the internal memory.

_Image source: _Wikipedia

_Image source: _Towards Data Science

The exciting part is that RNN (and LSTM in particular) could memorize not only word-to-word dependencies but also character-to-character dependencies! It doesn’t really matter what sequence consists of: it might be words or it might be characters. What is important is that they form a time-distributed sequence. For example, we have a sequence of characters [‘H’, ‘e’]. If we ask LSTM what may go next, it may suggest a <stop_word> (meaning, that the sequence that forms word _He _is already complete, and we may stop), or it may also suggest a character l (meaning, that it tries to build a _Hello _sequence for us). This type of RNNs are called character-level RNNs (as opposed to word-level RNNs).

In this tutorial, we will rely on this memorization feature of RNN networks, and we will use a character-level version of LSTM to generate cooking recipes.

Exploring the datasets

Let’s go through several available datasets and explore their pros and cons. One of the requirements I want the dataset to meet is that it should have not only a list of ingredients but also a cooking instruction. I also want it to have measures and quantities for each ingredient.

Here are several cooking recipes datasets I’ve found:

  • Recipe Ingredients Dataset (doesn’t have ingredients proportions)
  • Recipe1M+ (a lot of recipes but requires registration to download)
  • Epicurious - Recipes with Rating and Nutrition (~20k recipes only, it would be nice to find more)
  • Recipe box (~125,000 recipes with ingredients proportions, good)

Let’s try to use the “Recipe box” dataset. The number of recipes looks big enough, and it contains both ingredients and cooking instructions. It is interesting to see if RNN will be able to learn a connection between ingredients and instructions.

Setting TensorFlow/Python sandbox for training

There are several options you may follow to experiment with the code in this tutorial:

  1. You may experiment by using GoogleColab right in your browser (no local setup is needed).
  2. You may experiment by using Jupyter notebook in Binder right in your browser (no local setup is needed).
  3. You may set up a Jupyter notebook locally.

I would suggest going with GoogleColab option since it doesn’t require any local setup for you (you may experiment right in your browser), and it also provides a powerful GPU support for training that will make the model to train faster. You will be able to experiment with training parameters as well.

Importing dependencies

Let’s start with importing some packages that we will use afterwards.

# Packages for training the model and working with the dataset.
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import json

# Utility/helper packages.
import platform
import time
import pathlib
import os

#deep learning #lstm #tensorflow

How to Generate Cooking Recipes using TensorFlow and LSTM Recurrent Neural Network
7.40 GEEK