TensorFlow Tutorial 8 - Model Subclassing with Keras

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024
  • In this video we learn how to build much more flexible models using keras subclassing. Previously we've seen how to use Sequential and Functional API and hopefully the example demonstrated in the video shows the power of subclassing. I show how to build a ResNet-like model with skip connections which wouldn't even be possible using the Sequential API.
    Watch this video to understand ResNet:
    • C4W2L03 Resnets
    ❤️ Support the channel ❤️
    / @aladdinpersson
    Paid Courses I recommend for learning (affiliate links, no extra cost for you):
    ⭐ Machine Learning Specialization bit.ly/3hjTBBt
    ⭐ Deep Learning Specialization bit.ly/3YcUkoI
    ⭐ TensorFlow Specialization bit.ly/3FmEVK1
    📘 MLOps Specialization bit.ly/3wibaWy
    📘 GAN Specialization bit.ly/3FmnZDl
    📘 NLP Specialization bit.ly/3GXoQuP
    ✨ Free Resources that are great:
    NLP: web.stanford.e...
    CV: cs231n.stanford...
    Deployment: fullstackdeepl...
    FastAI: www.fast.ai/
    💻 My Deep Learning Setup and Recording Setup:
    www.amazon.com...
    GitHub Repository:
    github.com/ala...
    ✅ One-Time Donations:
    Paypal: bit.ly/3buoRYH
    ▶️ You Can Connect with me on:
    Twitter - / aladdinpersson
    LinkedIn - / aladdin-persson-a95384153
    Github - github.com/ala...

Комментарии • 63

  • @AladdinPersson
    @AladdinPersson  2 года назад +4

    I was inspired and learned the basics of TensorFlow after I completed the TensorFlow specialization on coursera. Personally I think these videos I created give a similar understanding but if you wanna check it out you can. Below you'll find both affiliate and non-affiliate links, the pricing for you is the same but a small commission goes back to the channel if you buy it through the affiliate link which helps me create more future videos.
    affiliate: bit.ly/3JyvdVK
    non-affiliate: bit.ly/3qtrK39

  • @slouma1998
    @slouma1998 4 года назад +15

    Lucky that I bumped on this video, wish to see more code oriented deep learning material like this one. Great job!

  • @HeyFaheem
    @HeyFaheem Год назад +1

    I Searched a lot fot Subclassing like pytorch in tensorflow..
    I Finally Found the Best..

  • @aymensekhri2133
    @aymensekhri2133 3 года назад +1

    Amazing, i hated tensorflow before but now i really like it. it looks like pytroch in logic

  • @nikitakrasnytskyi
    @nikitakrasnytskyi Год назад

    Thank you very much for the content! It is better than some articles in the net!

  • @AliRashidi97
    @AliRashidi97 2 года назад

    This Tutorial was AWESOME!!!! TNX A TON!!!

  • @SuilujChannel
    @SuilujChannel 3 года назад +3

    to get a summary when using subclassing you can use the build command and specify the shape in the parameter:
    # example:
    dtc2vec.build(input_shape=(BATCH_SIZE,2))
    dtc2vec.summary()
    for some reason i still get the output "multiple" for embedding layers but the other layers show the output shape

  • @hariomhudiya8263
    @hariomhudiya8263 4 года назад +1

    Just what I was looking for Thanks ✌

  • @Champignon1000
    @Champignon1000 3 года назад

    Man, i can't believe you don't have more followers, i love your videos and ill be here untill you reach 3.000.000 subs

  • @ningquanwang142
    @ningquanwang142 3 года назад +9

    Thanks for the great video! I am wondering is the call() method in a class a built-in function to convert a class instance into a function? I know there is a __call__() method, but is the call() method the same with the __call__() method?

    • @egolkar
      @egolkar Год назад

      does anyone can answer this quetion? how Call method automatically called without calling outside of the Class?

  • @wolfisraging
    @wolfisraging 4 года назад +1

    I remember when I first saw tf allowing this class sub classing like pytorch I immediately fell in love it after first usage. Still lovin it, the only complaint I have is when we summarize the model we don't get to see the shapes of intermediate blocks :} that's why sometimes I prefer functional api. Anyways, great video. 🙂

    • @AladdinPersson
      @AladdinPersson  4 года назад +2

      Yeah it reminds me so much of PyTorch.. so I love it 😅 Thank you for the comment, let me know if you have any feedback, what I can improve in making these videos, etc.

    • @wolfisraging
      @wolfisraging 4 года назад

      @@AladdinPersson Your videos are exceptionally excellent and unique already, even I planned to create such channel, but kinda busy for now, will see. :)

  • @beizhou2488
    @beizhou2488 4 года назад +1

    I never thought that the MNIST datasets could also be used this way. Haha. Taking it to the next level.

    • @AladdinPersson
      @AladdinPersson  4 года назад

      Haha I guess it's bit of an overkill :)

    • @tsegaamanuel5907
      @tsegaamanuel5907 3 года назад

      @@AladdinPersson That's an understatement lol

  • @filippoguerranti
    @filippoguerranti 2 года назад +1

    Thanks for the amazing videos of this playlist!
    May I ask you which IDE are you using and what theme is that?

  • @legacies9041
    @legacies9041 3 года назад +2

    What is the use of call function? Please explain

    • @Roscovanul2
      @Roscovanul2 3 года назад +3

      I found the answer here.
      stackoverflow.com/questions/59473267/why-in-keras-subclassing-api-the-call-method-is-never-called-and-as-an-alternat
      __call__ method is implemented in the Layer class, which is inherited by CNNBlock class
      So actually what we do is overriding the inherited call method, which new call method will be then called from the inherited __call__ method.
      So when we call our model instance, the inherited __call__ method will be executed automatically, which calls our OWN call method.

  • @RanDuan-dp6oz
    @RanDuan-dp6oz Год назад +1

    Thanks!

  • @coding10yearold
    @coding10yearold 3 года назад +1

    Newbie question:
    @Aladdin Persson
    why are they called 3 times in a row?
    I was guessing
    (None, 28, 28, 32)
    (None, 28, 28, 64)
    (None, 28, 28, 128)
    would be called once every Epoch

  • @anmarali2189
    @anmarali2189 2 года назад

    Can you please make a video on "custom padding" like adding a single row above the image and a single col. to the left of the image of 28 X 28 X 1 since keras does not give such flexibility, thanks in advance.

  • @eliashossain4327
    @eliashossain4327 2 года назад

    Could you please specify why you have considered channels[1] rather than channels [0] and channels[2] ?
    For example,
    self.identity_mapping= layers.Conv2D(channels[1], 3, padding='same')

  • @DanielWeikert
    @DanielWeikert 4 года назад +1

    Thanks for the video. Just one question. Normally we always need to specify the input shape/input_dim of the data we throw into the model. Here we did not do it and it still ran. Can you elaborate on that

    • @AladdinPersson
      @AladdinPersson  4 года назад +1

      From my understanding we do not need to specify the input shape for the model to run, for example if we create a simple sequential model it is still able to run without specifying the input with keras.Input. However if you do specify the input you're able to run (for example) model.summary() and I'm assuming other functionality that I'm not familiar with

    • @DanielWeikert
      @DanielWeikert 4 года назад

      @@AladdinPersson Thanks, do you think this true for all networks layers when we use them as the first layer, e.g. Embedding, LSTM, GRU,...)

  • @coding10yearold
    @coding10yearold 3 года назад +1

    @7:05 Why is it rerun again?

  • @shaikrasool1316
    @shaikrasool1316 4 года назад +1

    Sir, make videos on question answering in pytorch and seq to seq in keras

    • @AladdinPersson
      @AladdinPersson  4 года назад +1

      Will look into it!

    • @bharath5666
      @bharath5666 4 года назад

      Aladdin Persson sequence to sequence from scratch in Keras.

  • @AshishKumar-uj7ox
    @AshishKumar-uj7ox 3 года назад

    Thanks a ton for this excellent series of tutorial. I have a question. Can we make execution of some layers in the subclass conditional? For example if i added maxpool layer to subclass , and I didn't want to run it for the last CNNBlock, then how can I do that.

  • @maddoo23
    @maddoo23 2 года назад

    Hi, I constructed the subclassed layer/block and tried using it in a functional API (like you have used in sequential API). It throws an error for me (graph discnnected). Basically, its not accepting the output of the block as an input for the next layer. Can you please help me with it or better still, make a video on it?

  • @vishalsingh-yj8bk
    @vishalsingh-yj8bk 3 года назад

    Hi Aladdin! I was going through the GitHub repo so that I could go over the code that you have written in this tutorial; it seems the code is missing for this tutorial. Could you please post it on Github as well ?. Thank you.

  • @rachidelfermi6759
    @rachidelfermi6759 3 года назад

    thank you

  • @chadwinters4285
    @chadwinters4285 3 года назад +1

    How does the call method work ? I know that you can call a class with MyClass(), but how does it know to run the call method ?

    • @AladdinPersson
      @AladdinPersson  3 года назад +2

      The __call__ method of the parent class layers.Layer calls the call() function, hopefully that wasn't confusing lol

  • @mushahid9537
    @mushahid9537 3 года назад

    I was pretty confuse when he calls for base class constructor by passing arguments the name of subclass and self in super methods, this method is used only for Python 2.x versions, we don't need to pass the parameters for super method in Python 3.X anymore.

  • @god-son-love
    @god-son-love 3 года назад

    How to train with mixed inputs and outputs ?

  • @artabalt4288
    @artabalt4288 3 года назад

    Just what i was looking for. I was wondering, maybe you could make a video about how to use
    M = tf.keras.Model()
    from tensorflow.keras.utils import plot_model
    plot_model(M, to_file='Example.png')
    maybe that would show the dimensions?

  • @milak.9189
    @milak.9189 3 года назад

    Please Please, can you explain how to export Keras subclassed model with 2 args as a SavedModel? I didn't can't save my subclass model ,can you help me?

  • @riis08
    @riis08 4 года назад

    @Aladdin Persson: amazing video once again... thanks for this great tensorflow playlist. One question, what you think when we subclassing the tf.keras.Model, can we use tf.keras.Sequential() inside of that class? And second question is can we use __call__ instead of call()??

    • @AladdinPersson
      @AladdinPersson  4 года назад +3

      Yes from my understanding and experiments you can mix between sequential, functional and subclassing seemlessly and that's really powerful in my opinion. Just trying the second part of replacing __call__ instead of call it doesn't work, but I guess it's more interesting to ask why and scimming through the source code for layers. Layer does a lot of things under the hood, such as creating the graph, context managers and a bunch of things. You can read some more about it's __call__ here: github.com/tensorflow/tensorflow/blob/b36436b087bd8e8701ef51718179037cccdfc26e/tensorflow/python/keras/engine/base_layer.py#L875
      But so self.call() (that we've implemented) is just a small part of the __call__ of layers.Layer that we're inheriting and in that sense I guess it would make sense that we cannot simply replace call with __call__

  • @unamattina6023
    @unamattina6023 2 года назад

    what is the meaning of super(CNNBlock, self).__init__() ?
    how do we know our CNNBlock class has conv and bn attributes, where is the base CNNBlock class?

    • @Zimzode
      @Zimzode 5 месяцев назад

      The class CNNBlock inherits all the attributes of layers.Layer from the line:
      class CNNBlock(layers.Layer)
      and then when you call super, you're initializing each instance of CNNBlock with all the attributes of layers.Layer

  • @rordic.y5947
    @rordic.y5947 3 года назад

    The adobe premiere all of a sudden pops up makes me very shifting about the focus of the latest study aims

  • @donfeto7636
    @donfeto7636 2 года назад

    def call(self,input_tensor,training=False):
    y=self.cnn1(input_tensor,training=training)#CNN Layer and will output x as output from relu CONV,BatchNorm,Relu (this is call)
    x=self.cnn2(y,training=training)
    x=self.cnn3(x+self.identity_mapping(y),training=training)
    i think in Reset would be like that as i understand --not self.identity_mapping(tensor_input) as this would be the a[0] not a[1] as we have 3 Layers

  • @muckvix
    @muckvix 3 года назад

    So what would be the problem with writing this in functional API?

    • @AladdinPersson
      @AladdinPersson  3 года назад

      The skip connections for example

    • @muckvix
      @muckvix 3 года назад

      @@AladdinPersson that should be ok: stackoverflow.com/a/42391339/336527

  • @pedrogalvez6399
    @pedrogalvez6399 3 года назад

    Hi! thank you for this amazing series. However, when applying the ResNet_Like model, I get the following warning :
    WARNING:tensorflow:AutoGraph could not transform and will run it as-is.
    Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
    Any idea what would be causeing it? Should I ignore it?
    I'm running TF2.1
    Thank you!
    Pedro

    • @2009feedback
      @2009feedback 2 года назад

      I think you need to install your tensorflow-estimator from Anaconda 3 Navigator

    • @hunterwarick5135
      @hunterwarick5135 Год назад

      Did you ever figure out the issue? because I am having the same problem

  • @pankajshinde475
    @pankajshinde475 4 года назад

    Can you suggest some udemy courses?

    • @AladdinPersson
      @AladdinPersson  4 года назад +6

      I haven't done any Udemy courses so I don't have any suggestions there. I don't know this might just be me, but I don't like the feeling I get from Udemy. Feels scammy in my opinion. Just this "5 hours left at this price!!!, 99% off, BUY now!" shit makes me super annoyed. But I should probably get some experience with it before I judge it :)

    • @hariomhudiya8263
      @hariomhudiya8263 4 года назад +2

      Books + Internet >> Udemy course

  • @HallockW
    @HallockW 2 года назад

    i copy your code from the beginning part(CNNBlock), goes to [
    E:/pythonProject1/subclass.py:20 call *
    x = self.conv(inputs)
    AttributeError: 'CNNBlock' object has no attribute 'conv']
    cant figure out whats wrong in the Class part.

    • @HallockW
      @HallockW 2 года назад

      nevermind just find out that i mistake __int__ as __init__, and it take me half a day to know, fml

    • @Zimzode
      @Zimzode 5 месяцев назад

      @@HallockW all part of the process. That half a day figuring out how to find that mistake will save you tons of time finding similar mistakes in the future