In this article, we will explore 3 ways to code an AI with TensorFlow. As you will see, all 3 of them are really powerful in their own ways. In most tutorials, only the first one, the sequential model, is talked about and the other two are mostly overlooked. However, many technologies that we use today including Tesla’s Autopilot and the Full Self-Driving capabilities would not be possible with the sequential model, which is taught in most tutorials.
The 3 ways to code an AI with TensorFlow are the Sequential Model, Functional API and Subclassing. If this your first time with machine learning and you have no idea what TensorFlow is, do not worry, I will do my best to keep things simple. For now, you should know that TensorFlow is a very popular machine learning tool. And you don’t have to understand everything in full detail just yet, as you keep reading this article, things will get more clear and I will also post many more tutorials and projects on machine learning and artificial intelligence that will make things understandable one article at a time.
Sequential model is the most beginner friendly way to code your AI. It is also the one being talked about in most of the tutorials. You can use this one, when you have a single input, single output systems. Single input means that you can have a single source of input such as a camera input, or a single microphone input, but not both of them simultaneously. If you are just getting started with machine learning and artificial intelligence, I actually recommend getting started with this one. With the Sequential Model, you can start creating your machine learning models within 15 minutes and you can focus on the machine learning and artificial intelligence concepts rather than dealing with complexity in your machine learning code.
The Functional API is somewhat similar to the Sequential Model in many ways. However, with the Functional API, you can create more complex models that have things like: Multiple inputs and multiple outputs to and from your machine learning models, as well as some complex structures inside those models. With the Functional API, you can create more efficient and effective machine learning models.
Now that you can feed the model multiple inputs, you can now give the model images from multiple sensors, such as cameras, microphones and other sensors. And your model can consider the input of all of those sensors to generate an output. Having multiple outputs from your machine learning model also means that your model can make more complex decisions that are related to each other.
One example to this, would be using a camera and a microphone input to recognize people in a building from a video, in order to design an elevator access system. If both the face ID and the voice ID of a person match in the model, the model can then decide to generate two outputs, one is to unlock the elevator and the second is to unlock only the floor levels that person has access to. This would be an example for a multi input, multi output model. It is multi input because we are feeding the model both a camera and a microphone input. It is also a multi output model because the model decides two things, one is whether to unlock the elevator for a given person, and the second is what floor levels that person has access to.
Subclassing gives you the most flexibility on your model. With Subclassing, you can have the most flexibility in both how you construct the model, and how your model is executed as well. Subclassing is also highly used in machine learning research. And it is for a good reason.
One major reason for its popularity among researchers is that you can create new types of neural network layers that didn’t exist before. With the two methods to code an AI that we explored previously, we are able to use existing neural network layers, provided by TensorFlow, to create our machine learning models. However, with Subclassing, we can still use those layers, but we can also modify those neural network layers and experiment with them to create our own novel neural network layers.
Another reason for its popularity is that, with Subclassing you can run your models in something called “eager execution”. With the previous two models, when you create a model, what happens is that your model first creates a computational graph, which is a blueprint for your model, and then you compile the model for your actual model to be created and run. But with Subclassing, you can create models more dynamically with the eager execution. What eager execution allows you is to do is to run your machine learning model code like a regular Python code. This allows you to handle your model with everthing you know in Python, including all the data structures, loops as well as executing your code line by line. That’s why it is also called a more “Pythonic” way to write machine learning code. This way, it is easier to experiment and debug your machine learning models.
What Tesla uses is most closely related to this one. Tesla actually uses PyTorch, instead of TensorFlow, to develop their machine learning models that power Autopilot as well as their Full Self-Driving feature. If you don’t know what PyTorch is by the way, TensorFlow and PyTorch are the most popular machine learning tools as of right now. There used to be many major differences between TensorFlow and PyTorch. However, recently, you can achieve very similar results with either one, as both of them implemented features from each other over the course of couple years. So if you just start with learning either one, you can learn the other one within a matter of days.
All 3 models have different strengths and different capabilities. The Sequential Model allows you to get up and running with machine learning in as fast as 15 minutes (I also have a tutorial on how to do this in another article). This way, if you are just getting started with machine learning and artificial intelligence, you can learn different machine learning concepts and apply most of them relatively easily with the Sequential model. But I definitely don’t recommend you to stop there. There are so many other things you can do, when you leverage the other two ways as well, namely Functional API and Subclassing. Once you get the hang of the Sequential model, I actually recommend learning the Functional API, which will allow you to create more complex models that can be more useful in real life. The next best step would be to learn how to use subclassing, which will give you the most control over your machine learning models, in both the creation and the execution of your models.
I hope you got some value out of this article. If you have any questions, leave them as a comment and I will get back to you as fast as I can. If your question requires a more extensive explanation, I can explain that in another article or a video, addressing your question. If you are a visual learner by the way, I also have this article in the video format here: