Hey @Goudout,
I literally went over the documentations of various versions to write as much of an inclusive answer as possible. But before starting, I would like to mention a few points.
- First, I am not a Tensorflow expert (unfortunately ), so, I might miss out on some of the things.
- Second, Tensorflow is a humongous framework, so trying to find a one-size-fits-all solution to any concept or idea may not be practically possible.
- I am mentioning this because whenever you are using Tensorflow (or any framework for that matter), I want you to keep an open mind, so whenever a piece of code that was working in all the scenarios till now doesn’t work any more, there may be a multitude of reasons.
- For instance, Tensorflow might change the version, and when it does, a lot of things change usually.
- Tensorflow might even incorporate other libraries such as it incorporated Keras, and when it does, both Tensorflow and the other library will naturally undergo a lot of changes.
Keeping these points in mind, let’s start with the answer I will be using 2 versions of Tensorflow in this answer of mine, v2.3.0 and v.2.9.1, so make sure to look out for the differences. This will not help me to answer your query more suitably, but will also tell you how much Tensorflow changes from one version to another (sub-versions to be more precise).
As a programmer, in order to know how a new thing works, I try to find some tutorials and examples first instead of diving straight in the documentation. 2 reasons, helps to save time and avoids dealing with extensive documentation of these frameworks. Since these frameworks are written by thousands of programmers collectively, hence, it’s natural to be unfamiliar with a lot of things used in the documentation, which will appear daunting to any newbie. So, let’s see how we can adopt this strategy with the 2 different versions.
P.S. - You can find tutorials and examples on the entire web but for the sake of this answer, let’s say that we are only considering tutorials and examples available in the docs of Tensorflow.
v.2.3.0
So, consider the docs of v2.3.0. I want to know how to use tf.keras.applications.MobileNetv2
. I open it’s doc, no tutorial, no examples. I see that it inherits from keras.model
, I open it’s doc, no tutorial. So, I take a look at the examples. However, I want to use this model as a layer in a larger model (our transfer learning based model), and there are no examples for that. I see that it inherits from Layer
, I open it’s doc, and before I could look for tutorials or examples, I find this
We recommend that descendants of Layer
implement the following methods:
call(self, *args, **kwargs)
: Called in __call__
after making sure build()
has been called. call()
performs the logic of applying the layer to the input tensors (which should be passed in as argument). Two reserved keyword arguments you can optionally use in call()
are:
training
(boolean, whether the call is in inference mode or training mode)
mask
(boolean tensor encoding masked timesteps in the input, used in RNN layers)
I have shrunk the above recommendation to it’s bare essentials for this post. Now, since we know that keras.Model
is a descendant of Layer
, keras.Model
should implement a call
method, and it does indeed, which you can view in the source code, and voila I am done.
v.2.9.1.
Now, consider the docs of v2.9.1. This takes a complete different approach. Once again, I want to know how to use tf.keras.applications.mobilenet_v2.MobileNetV2
. I open it’s doc, and here only, I find a tutorial entitled Transfer learning and fine-tuning, and that’s it, I am done. In the tutorial, it has been clearly depicted how to use this model as the part of a larger model.
Conclusions
- Now, you might ask why did I choose v.2.3.0 in the first place. This is because it is a nice analogy to what you mentioned in your post, i.e.,
- Even though
keras.Model
implements a call
method, this is not mentioned in the docs of keras.Model
. It is only mentioned in the source code of the class.
- However, a programmer might prefer the attractive docs instead of the daunting code. So, it is only after visiting the docs of
Layer
, I got to know that it’s descendants should implement a call
method, and hence, I went back to the docs of keras.Model
(a descendant of Layer
), took a look at it’s source code and found the call
method.
- Now, this is only one of the ways (as I mentioned before) in which a programmer could have figured out how to use an instance of
tf.keras.applications.MobileNetv2
.
- An experienced Tensorflow programmer perhaps could have seen that
tf.keras.applications.MobileNetv2
returns an instance of keras.Model
, which further inherits from Layer
, and could have simply tried to use the base_model
as a layer without taking a look at the source code or even the docs, and it would have worked out.
- So, the answer to the question;
There is no single way out. It could have been one of the above mentioned ways, or it could have been one of the 100 other ways. I hope this helps.
Regards,
Elemento