Flatten, Reshape, and Squeeze Explained - Tensors for Deep Learning with PyTorch

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

ความคิดเห็น • 115

  • @deeplizard
    @deeplizard  6 ปีที่แล้ว +9

    Check out the corresponding blog and other resources for this video at: deeplizard.com/learn/video/fCVuiW9AFzY

    • @reb5642
      @reb5642 5 ปีที่แล้ว

      thank you so much

  • @prakhardixit2597
    @prakhardixit2597 5 ปีที่แล้ว +10

    thanks deeplizard for making pytorch so easy to understand , an excellent series!!!

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว +3

      Hey Prakhar - You are welcome!

  • @Murmur1131
    @Murmur1131 3 ปีที่แล้ว +1

    Gratulations on your 100.000 followers here! :)

  • @philtrem
    @philtrem 6 ปีที่แล้ว +5

    Really love the analogy ! And I'm really enjoying the course so far.

  • @deeplizard
    @deeplizard  6 ปีที่แล้ว +1

    Check out the corresponding blog and other resources for this video at: deeplizard.com/learn/video/fCVuiW9AFzY

  • @philtrem
    @philtrem 6 ปีที่แล้ว +47

    def flatten(t):
    return t.reshape(-1)

  • @luis.barragan
    @luis.barragan 6 ปีที่แล้ว +2

    When you're writing code, it sounds in the background like airplane cabin white sound and it's relaxing. Nice tutorial! I'm a PyTorch Scholar and since the resources in Udacity aren't good enough for me, I'm watching your very helpful tutorials. Thanks.

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +1

      Hey Felipe - You are welcome! Definitely appreciate your feedback. It helps a lot to hear weather subtle things make a difference. 🙏

    • @DivyaThakur
      @DivyaThakur 6 ปีที่แล้ว +1

      @@deeplizard I'm a Pytorch Scholar too! And share the same feelings as Felipe. Thanks a ton deeplizard :)

  • @AIRoboticsCreator
    @AIRoboticsCreator 5 ปีที่แล้ว +1

    Great video! I’m halfway thru the playlist!

  • @rohtashbeniwal9202
    @rohtashbeniwal9202 4 ปีที่แล้ว +2

    best work u guys are doing, lot of hard work u both did ,to get this knowledge , love from India

  • @hosseinaboutalebi9998
    @hosseinaboutalebi9998 6 ปีที่แล้ว +2

    Great videos. Best content. Nice animations.
    The fact that you make your videos like a meme is exceptional and make watching them enjoyable.
    Keep up the good work! I am your fan :)

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว

      Thank you Hossein!

  • @sethatkins3731
    @sethatkins3731 6 ปีที่แล้ว +6

    This was very helpful. I came here looking for what the squeeze operation does. Was happy to find answers to some other questions in the back of my mind. (what does the -1 mean in resize operations) I will say though, that sometimes the visuals can be a little distracting. (Mainly the pancake one. I was having a hard time listening to what you were saying.) Other than that, this was very helpful. I might watch more of your videos in the future!

  • @SaiKrishna-tr1dz
    @SaiKrishna-tr1dz 5 ปีที่แล้ว +2

    awesome content and the way you deliver hats off!

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว +1

      Thank you! 🎩

  • @amaytrivedi
    @amaytrivedi 5 ปีที่แล้ว +1

    Hi there ,
    i followed your tutorials and they seem great!!
    I implemented in the following manner (Squeeze function in reshape)
    def flatten_by_reshaping():
    rdata = data.reshape(1,-1)
    rd = rdata.reshape(rdata.shape[1] , )
    return rd
    print(flatten_by_reshaping() , len(flatten_by_reshaping()))

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว +1

      Make sure you add the "data" parameter to the function signature.

  • @M0481
    @M0481 6 ปีที่แล้ว +9

    I haven’t installed PyTorch yet, but assuming that it works the same as a list, would the answer be: reshape(1,-1)[0]?

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +7

      Hey Mick - Nice! I didn't think of this one. It works!

    • @roros2512
      @roros2512 6 ปีที่แล้ว +2

      I did the same and it worked =B

    • @philtrem
      @philtrem 6 ปีที่แล้ว +2

      It works but it wouldn't be the most straightforward approach. But it's good you came up with this, the more ways the merrier.

    • @Alexey_Pronin
      @Alexey_Pronin 3 ปีที่แล้ว +2

      t.reshape(t.numel()) or t.reshape(-1)

  • @thak456
    @thak456 5 ปีที่แล้ว +1

    @deeplizard thank you very much for this video and the full playlist. Can you make a series on NLP with Pytorch ?

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Currently working out the details about which direction we'll go in terms of content. Thank you for the suggestion.

  • @mohammadsadilkhan1875
    @mohammadsadilkhan1875 5 ปีที่แล้ว +1

    What to do if I want to append a tensor with another tensor?

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      We have cat and stack options. See this one: deeplizard.com/learn/video/kF2AlpykJGY

  • @TerragonDE
    @TerragonDE 6 ปีที่แล้ว +1

    If you only reshape there is a bracket inside the bracket so that it is not a real one Dimensional Tensor but it stays the two-dimensional Tensor that has only the first Zero Index filled?

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +1

      Hey Terragon - You are correct! 🚀

    • @TerragonDE
      @TerragonDE 6 ปีที่แล้ว +1

      Thanks to your great tutorial series with best style of explanation i found on the web! cant wait for the next episodes ;-)

  • @Sikuq
    @Sikuq 4 ปีที่แล้ว +1

    Very good.

  • @Aditya-ne4lk
    @Aditya-ne4lk 5 ปีที่แล้ว +25

    i was waiting for the pancakes to be squished 'flat'...

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว +2

      Haha! 🤣

    • @salvindsouza7053
      @salvindsouza7053 5 ปีที่แล้ว

      same here , hahahaha

    • @utkar1
      @utkar1 4 ปีที่แล้ว

      me too .. lol

    • @13RedCorpse
      @13RedCorpse 4 ปีที่แล้ว

      I couldn't concentrate on what he was talking about, I was thinking about how delicious these pancakes look. I guess I should go get something to eat before I continue.

  • @robosergTV
    @robosergTV 6 ปีที่แล้ว +4

    link for the ted talk? Come on, its expected to have a link.

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +2

      Agree. Added to the description. Link: th-cam.com/video/aR5N2Jl8k14/w-d-xo.html

  • @thepresistence5935
    @thepresistence5935 2 ปีที่แล้ว +1

    6:00 why you are showing food, I am hungry 😅. But it look like delicious :)

  • @Akiiii009
    @Akiiii009 5 ปีที่แล้ว +1

    Why not to use directly t.flatten() instead of t.reshape(-1) OR t.reshape(1,-1).squeeze() ?

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Hey Akash - It's good to use flatten directly. The method also comes with the start_dim parameter. It is important to understand that flatten is a special case of the more general idea of reshaping. The presentation here was meant to explore the various concepts.

    • @Akiiii009
      @Akiiii009 5 ปีที่แล้ว +1

      @@deeplizard Okay got it, thanks

  • @DanielWeikert
    @DanielWeikert 6 ปีที่แล้ว +1

    Thanks a lot! Great video

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว

      Hey Daniel - You are welcome!

  • @aamirmustafa7731
    @aamirmustafa7731 6 ปีที่แล้ว +25

    t=t.reshape(-1)

    • @TerragonDE
      @TerragonDE 6 ปีที่แล้ว +2

      @Phil Ad why?

    • @gabrielemazzola9652
      @gabrielemazzola9652 5 ปีที่แล้ว +5

      @Phil Ad I am flattened by it.

    • @MohammedNoureldin
      @MohammedNoureldin 4 ปีที่แล้ว

      Please explain how does this work.

    • @ViralFormofLife
      @ViralFormofLife 4 ปีที่แล้ว

      @@MohammedNoureldin a little late answer but 7:06 basically tells it.
      I think reshape() determines the number of individual elements in the tensor and reshapes it as one axis with 12 elements in it. Sorry if i'm wrong.

  • @kafaayari
    @kafaayari 5 ปีที่แล้ว +1

    What's your keyboard? loved the sound too much!

  • @Normalizing-polyamory
    @Normalizing-polyamory 6 ปีที่แล้ว +7

    def flatten(t):
    numel = t.numel()
    return t.reshape(numel)

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +1

      Hey Erik - I like. 🚀

  • @philtrem
    @philtrem 6 ปีที่แล้ว +2

    When you have a sentence like this: "The primary ingredient we use to produce our product, a function that maps inputs to correct outputs, is data.", it's better to write it as: "The primary ingredient we use to produce our product - a function that maps inputs to correct outputs - is data." (ie. using dashes instead of commas). Otherwise it's confusing and difficult to make out that it's a 'parenthetical statement'.

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +1

      Hey Philippe - I like your point here. The sentence is a bit hard to interpret. I'll keep this in mind going forward. Appreciate your input!

  • @srikarvalluri8173
    @srikarvalluri8173 5 ปีที่แล้ว +4

    def flatten(t):
    t = t.reshape(1, t.size()[0] * t.size()[1])
    t = t.squeeze()
    return t
    I know this is terribly ineffective but it's something different then all the other ones out there. ;)

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว +1

      I like it. It's good practice for us to see different approaches and be able to reason about their validity. Thanks for adding it.

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Created a couple of quiz questions that use your example:
      deeplizard.com/learn/video/fCVuiW9AFzY

  • @heller4196
    @heller4196 5 ปีที่แล้ว +12

    5:45 why you distracting me with food !

  • @narroric
    @narroric 6 ปีที่แล้ว +3

    I don't know what the low rumbling sound is, but it is ridiculously soothing. Like doing coding on a star trek starship.

  • @robosergTV
    @robosergTV 6 ปีที่แล้ว +1

    ted talk www.ted.com/talks/maurice_conti_the_incredible_inventions_of_intuitive_ai?language=en

  • @muayas9602
    @muayas9602 2 ปีที่แล้ว

    The other way to flatten the tensor is by using t = t.reshape( -1 ) in flatten(t) function

  • @Livakivi
    @Livakivi 6 ปีที่แล้ว +1

    t.view(t.numel())
    or
    t.view(-1)

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว +2

      Hey Ulm - Nice! I didn't think of passing t.numel(). Passing -1 was on my mind though. :)
      Also, thanks for introducing the view() alternative. I didn't mention it in the video, but I did include it in the blog post.

    • @Livakivi
      @Livakivi 6 ปีที่แล้ว +2

      Keep up the great work! :)

  • @golangshorts
    @golangshorts 5 ปีที่แล้ว +2

    t.reshape(-1)
    or
    t.reshape(t.numel())

    • @golangshorts
      @golangshorts 5 ปีที่แล้ว +1

      your tutorials really worth than the paid courses. Huge thanks from my heart.

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Thank you! Glad these videos are helpful. You are welcome!

  • @3bdo3id
    @3bdo3id ปีที่แล้ว

    t.reshape(12) or t.reshape(-1)

  • @prathameshdinkar2966
    @prathameshdinkar2966 2 ปีที่แล้ว

    Answer for flattening with reshaping : t.reshape([-1])

  • @yogeshrampariya8346
    @yogeshrampariya8346 3 ปีที่แล้ว

    t.reshape(t.numel()) or t.reshape(tensor(t.shape).prod().... Either of these should work

  • @keyurparalkar9535
    @keyurparalkar9535 6 ปีที่แล้ว

    t.reshape(3,4)

  • @Makwayne
    @Makwayne 5 ปีที่แล้ว +2

    you made me hungry around the 6th minute

  • @nazstreamsini4870
    @nazstreamsini4870 ปีที่แล้ว +1

    Oof, i think the bing chat can take that picture and be able to tell that the bridge is dangerous to cross right now meaning the future is now🎉

  • @tallwaters9708
    @tallwaters9708 5 ปีที่แล้ว +2

    OMG I wish people wouldn't use "rank" in this way. Rank refers to the number of linearly independant columns in a matrix...

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Do you have a typo?

    • @tallwaters9708
      @tallwaters9708 5 ปีที่แล้ว +2

      @@deeplizard Yes! Thanks, corrected :) It is strange that they'd pick that word 'rank' though, pretty confusing in my opinion. Nevertheless, great videos!

  • @tomgt428
    @tomgt428 5 ปีที่แล้ว +1

    cool

  • @manuelbevilacqua2490
    @manuelbevilacqua2490 6 ปีที่แล้ว +1

    numel = t.numel()
    nt = t.reshape(numel)

    • @deeplizard
      @deeplizard  6 ปีที่แล้ว

      Nice! Moving fast! 🤖

  • @utkar1
    @utkar1 4 ปีที่แล้ว

    t.reshape(12)
    t.reshape(-1)

  • @EunYeoWoo
    @EunYeoWoo 5 ปีที่แล้ว

    t = t.reshape(12) works fine but the other ppl's t = t.reshape(-1) seems way more elegant

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Thanks MinJoong! Yes. Agree.

  • @DeltaSleepy
    @DeltaSleepy 5 ปีที่แล้ว +2

    eazy peezy...
    t.reshape(-1)

  • @themelankolis1922
    @themelankolis1922 4 ปีที่แล้ว

    Nice tutorial! It would be really great if you change the backsound while you write the code. Because it is not that comfortable to hear, especially when you hear with earphone.

  • @prabhatale1135
    @prabhatale1135 4 ปีที่แล้ว

    def flatten():
    return t.reshape(-1)

  • @bharadwajreddy7840
    @bharadwajreddy7840 4 ปีที่แล้ว +1

    t.reshape(1,-1)[0], better late than never :p

  • @ViralFormofLife
    @ViralFormofLife 4 ปีที่แล้ว

    t = t.reshape(12)

  • @akshooter_5010
    @akshooter_5010 4 ปีที่แล้ว

    print(t.reshape(t.numel()))

  • @believeit5450
    @believeit5450 5 ปีที่แล้ว +1

    Thanks the video for the t.reshape(1,-1) is It this the same explanation for numpy using reshape ?
    By Julu Ahamed : stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว +1

      You are welcome! Yes. That's right.

  • @andresalmodovar3473
    @andresalmodovar3473 3 ปีที่แล้ว

    my answer (t.reshape(-1)). Without looking, I swear

  • @yuntaozhang666
    @yuntaozhang666 4 ปีที่แล้ว

    t.reshape(-1)

  • @janmichaelbesinga3867
    @janmichaelbesinga3867 4 ปีที่แล้ว

    t.reshape(12)

  • @GeNiaaz
    @GeNiaaz 4 ปีที่แล้ว

    I'm sorry, zucchini on a pizza ????

  • @baqerghezi1342
    @baqerghezi1342 4 ปีที่แล้ว

    reshape(-1,)

  • @acidtears
    @acidtears 4 ปีที่แล้ว

    Great series but wow that sped up keyboard typing makes it very annoying to go back and go through the video again. Not sure if silence would be any better but it sounds like a rat on amphetamines.

  • @ca7ax_xinyu
    @ca7ax_xinyu 4 ปีที่แล้ว

    t = t.reshape(t.numel()) :)

  • @nikosmparoutis221
    @nikosmparoutis221 5 ปีที่แล้ว

    i do not like the comment and the tone "Bakers flat to make goods instead we do it to make intelligence". The snobbish tone of programmers who think they know it all but they are scared because they do not. The analogy is good the way it is said is not

  • @snippletrap
    @snippletrap 4 ปีที่แล้ว

    Now do scatter()

  • @amarnathreddy9906
    @amarnathreddy9906 4 ปีที่แล้ว

    r.reshape(1,torch.tensor(r.shape).prod()).squeeze() for a tensor r

  • @CoolDude911
    @CoolDude911 5 ปีที่แล้ว +1

    t.reshape(t.numel())

    • @deeplizard
      @deeplizard  5 ปีที่แล้ว

      Looks good Barry!

  • @muthukamalan.m6316
    @muthukamalan.m6316 ปีที่แล้ว +1

    t.flattern() makes my life simpler😅😂

  • @trananswers
    @trananswers 3 ปีที่แล้ว

    def flatten(t):
    return t.reshape(-1)

  • @jorgec7028
    @jorgec7028 5 ปีที่แล้ว +1

    t=t.reshape(-1)

  • @Muhammad_Al-Barham
    @Muhammad_Al-Barham 4 ปีที่แล้ว

    t.reshape(-1)

  • @usmabhatt1768
    @usmabhatt1768 4 ปีที่แล้ว

    t.reshape(-1)