Coding Challenge

In this challenge, I use the TensorFlow.js library to create an interactive demonstration of linear regression (with stochastic gradient descent!) Code: thecodingtrain.com/challenges...
🕹️ p5.js Web Editor Sketch: editor.p5js.org/codingtrain/s...
🎥 Previous video: • Coding Challenge #103:...
🎥 Next video: • Coding Challenge #105:...
🎥 All videos: • Coding Challenges
References:
💾 TensorFlow.js: js.tensorflow.org/
🐦 Kaustubh Olpadkar's tweet: / 997836558131650561
Videos:
🚂 Linear Regression with Gradient Descent: • 3.4: Linear Regression...
🚂 TensorFlow.js playlist: • Session 6 - TensorFlow...
🚂 Intelligence and Learning playlist: • Intelligence and Learning
🚂 ES6 Arrow Function: • 16.3: ES6 Arrow Functi...
🚂 JavaScript Promises: • 16.11: Promises Part 1...
Live Stream Archive:
🔴 Coding Train Live 139: • Live Stream #139: Line...
Related Coding Challenges:
🚂 #70 Nearest Neighbors Recommendation Engine: • Coding Challenge #70: ...
🚂 #105 Polynomial Regression with TensorFlow.js: • Coding Challenge #105:...
🚂 #106 XOR Problem with TensorFlow.js: • Coding Challenge #106:...
Timestamps:
0:00 Introduction
1:21 What is linear regression?
3:16 What do we need?
3:41 Loss function
4:48 Optimizer
5:26 Formula for line
8:02 Let's Code!
8:23 Add data set -- mouse clicks
9:16 Remap x, y to 0, 1
12:12 Randomly initialize m and b
13:10 tf.variable()
13:39 predict()
16:13 Optimizer & learning rate
17:25 Loss function
19:24 Minimize loss
22:53 What is the optimizer doing?
27:42 Visualize regression results
32.49 dataSync()
35:00 Adjust learning rate
35:35 Memory management
41:01 Improvements
Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound
🚂 Website: thecodingtrain.com/
👾 Share Your Creation! thecodingtrain.com/guides/pas...
🚩 Suggest Topics: github.com/CodingTrain/Sugges...
💡 GitHub: github.com/CodingTrain
💬 Discord: thecodingtrain.com/discord
💖 Membership: kzread.infojoin
🛒 Store: standard.tv/codingtrain
🖋️ Twitter: / thecodingtrain
📸 Instagram: / the.coding.train
🎥 Coding Challenges: • Coding Challenges
🎥 Intro to Programming: • Start learning here!
🔗 p5.js: p5js.org
🔗 p5.js Web Editor: editor.p5js.org/
🔗 Processing: processing.org
📄 Code of Conduct: github.com/CodingTrain/Code-o...
This description was auto-generated. If you see a problem, please open an issue: github.com/CodingTrain/thecod...
#tensorflowjs #linearregression #stochasticgradientdescent #p5js #tensorflowjs

Пікірлер: 82

  • @israrawan
    @israrawan6 жыл бұрын

    At 7:23, line (linear function) represents the prediction. A point belonging to a line must be labelled as "guess", whereas the original label of every example should be labelled as "y". [Slip of 'marker' maybe] Nice video though. 'Desperately' waiting for Layers API video. :)

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    Ah, shoot, you are absolutely right. Pinning this comment!

  • @BrettCooper4702

    @BrettCooper4702

    6 жыл бұрын

    Visualising that whiteboard at 7:23 ish, as coding challenge using in P5 to animate whiteboard drawings as an interactive data visual. #bounty :)

  • @cashel1111
    @cashel11116 жыл бұрын

    that 'dan from the future' correction was done in good taste, and was very useful (plus entertaining) very very good way of making sure you cover topics properly :)

  • @nicholascanova4250

    @nicholascanova4250

    5 жыл бұрын

    these videos are incredible

  • @MatthewBishop64
    @MatthewBishop645 жыл бұрын

    This is a million times better than commercial courses like Udacity's AI "nanodegree" where they drive around in a self-driving car trying to show you how amazing and successful they are.

  • @MajorMandyKitten
    @MajorMandyKitten6 жыл бұрын

    Your videos keep getting better. I generally use c++ and c#, but your videos are good for finding fun ideas and useful ideas. Keep it up, man!

  • @muhammedshameel5684
    @muhammedshameel56846 жыл бұрын

    I love this channel because it has time travels. love it when he does that

  • @israrawan
    @israrawan6 жыл бұрын

    There is this pretty cool 'syntax' to define a variable in tensorflow.js. Instead of wrapping the tf.scalar around the tf.variable (13:00), we can actually use chaining. For example: tf.variable(tf.scalar(random (1))) is same as tf.scalar(random(1)).variable(). [As I am reading documentation while watching this video, I thought I should share this "syntactic sugar"]

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    Oh, awesome, I did not know this!

  • @caiovitullo4584
    @caiovitullo45845 жыл бұрын

    I've done many ML courses, but yours way to explain is being the best! Congratiulations!!!

  • @stevegalili355
    @stevegalili3556 жыл бұрын

    Yayyy! Keep up the good work Daniel

  • @HamzaAli-nc5fx
    @HamzaAli-nc5fx5 жыл бұрын

    I love you sir. You have my respects. I never took interest in machine learning but after watching the previous videos and this one, I am surely gonna dive more into it. Thank you again.

  • @Osmosphere
    @Osmosphere3 жыл бұрын

    Wow - thank you so much! Finally found an great introduction to TensorFlow.js and machine learning that I understand and makes fun! I added the loss value as additional line to the graph :-)

  • @acorad30
    @acorad306 жыл бұрын

    omg didn't notice it lasted 44 minutes! It was a blast, good video!

  • @terjeoseberg990
    @terjeoseberg990 Жыл бұрын

    SGD changes m and b in small steps in the direction of the gradient. The step size is determined by the learning rate. It’s not random, but the direction of the gradient can change as it learns, so it needs to be recalculated after each time m and b are updated.

  • @edujbb83
    @edujbb836 жыл бұрын

    Thanks for your video tutorial, they are great.

  • @braidata
    @braidata2 жыл бұрын

    Antes de ver el video, el solo hecho de que sea de Daniel, me hace sentir que por fin desbloquearé este tema del todo y en javascript. Gracias Daniel, en mi caso el mejor profesor!

  • @RingoDemichet
    @RingoDemichet6 жыл бұрын

    Hooray ! A coding challenge with tensorflow !

  • @laikesagores3258
    @laikesagores3258 Жыл бұрын

    This is out of my range!!!

  • @carllee6422
    @carllee64226 жыл бұрын

    finally get the real example with tensorflow js coool

  • @christianjt7018
    @christianjt70185 жыл бұрын

    awesome project :)

  • @applebanana6149
    @applebanana61496 жыл бұрын

    Excellent! Now we're coding with Power!

  • @allaheadflank
    @allaheadflank5 жыл бұрын

    Thanks!!

  • @madebymate4870
    @madebymate48702 жыл бұрын

    great tutorial bro

  • @oswaldd3110
    @oswaldd31103 жыл бұрын

    the future thing was awesome :D

  • @fluencygod1045
    @fluencygod10456 жыл бұрын

    I first noticed that a tensor flow was possible with js.It's very very amazing!!

  • @shashankmadan
    @shashankmadan6 жыл бұрын

    THE FUTURE THING WAS JUST AWESOME!!!!!!!!!!!!!!!!!!!

  • @blasttrash
    @blasttrash6 жыл бұрын

    the future dan was awesome lol. i felt like i was really watching a movie. :P

  • @psylocke1517
    @psylocke15176 жыл бұрын

    do you ever get bored coding? im 17 and i think u have the years of experience same as my age. i'm taking the first baby-steps of coding and i love it but can't do it for long. how do you keep yourself motivated and curious?

  • @rubix4716
    @rubix47166 жыл бұрын

    Do you have a tutorial on how you create your coding environment?

  • @ramseshendriks2445
    @ramseshendriks24456 жыл бұрын

    2:25 in fact that is not a linear relation in reality. because mass grows with volume and volume grows with the third power of lenght

  • @diegofernandorueda9160
    @diegofernandorueda91604 ай бұрын

    at minute 18 the Sub() function, what is the use? and could it be done in other ways for this linear regression?

  • @eltitanthanos6568
    @eltitanthanos65686 жыл бұрын

    Pro, your ability to coding is amazing, you must let the virtual come out to reality, you should program arduino and create a incredible project genetic algorithm neuronal net evolution robot

  • @JosephHenryDrawing
    @JosephHenryDrawing5 жыл бұрын

    What are the only five Tensors left with tf.memory().numTensors ? (there's m,b and ...)

  • @Grynjolf
    @Grynjolf4 ай бұрын

    I don't know anything about tensor flow, but are you not able to just get the trained m and b values and draw the line with those?

  • @marcosrios3626
    @marcosrios36266 жыл бұрын

    5:28 when my teacher asks me a question

  • @Eddytorial
    @Eddytorial5 жыл бұрын

    u should do a tutorial about that how future of you breaking in the middle :) that was some Hollywood material :) like it :))

  • @wojtek4351
    @wojtek43515 жыл бұрын

    I don't really understand the point of storing x and y positions of points as numbers from 0 - 1 if we are gonna convert them back to their initial form? As in what is the point of mapping mouseX from 0 - 1 if we are later just converting it back to 0 - width?

  • @leanobajio
    @leanobajio6 жыл бұрын

    I'm entertained.

  • @darogajee3286
    @darogajee32866 жыл бұрын

    Unable to draw the line..... After writing plotted point function

  • @OneShot_cest_mieux
    @OneShot_cest_mieux6 жыл бұрын

    Why is there 5 tensors instead of just 2 at the end ?

  • @username42
    @username425 жыл бұрын

    where is the codes he used in this video ? in which github repo?

  • @forloop7713
    @forloop77136 жыл бұрын

    but a height vs weight function isn't typically linear, and shouldn't be represented with a line, since weight scales with the cube of height.

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    Great feedback, thank you!

  • @rained23JMTi
    @rained23JMTi5 жыл бұрын

    optimizer.minimize(() => loss(predict(x_vals), ys)); what does this code actually do ? as I dont see it related to below code as const ys are re-declared at the bottom of draw function. Thx.

  • @TheWordBecameFleshJ114

    @TheWordBecameFleshJ114

    5 жыл бұрын

    it minimizes the loss function using the optimizer's algorithm

  • @puipuiboi6386
    @puipuiboi63866 жыл бұрын

    why you taking width and height of the canvas from 0 to 1? how it makes it better? what if i take it as the total width and height of the canvas

  • @aaron7c

    @aaron7c

    6 жыл бұрын

    WhatTheHACK! I think that the minimize function may take in a normalized (between 0 and 1) number or something like that. I’m not exactly sure why they normalize numbers on a canvas but I think it has to do with how the answer to a neural network usually is a Boolean (yes or no) which is between 0 and 1 (0 being no and 1 being yes.. and in-between being how sure it is of the answer )

  • @puipuiboi6386
    @puipuiboi63866 жыл бұрын

    is it wrong to take distance of the point to the line as perpendicular distance? will it affect the solution in any way? please answer :)

  • @iLavaRF

    @iLavaRF

    6 жыл бұрын

    It is not wrong, it is actually a better approximation but the math and coding is just harder.

  • @souvikkhan9494
    @souvikkhan949410 ай бұрын

    can anyone send me the complete code

  • @mikelezhnin8601
    @mikelezhnin86016 жыл бұрын

    Where is the github page for this? Or am I just blind and it is right there in the description?

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    I need to do a better job of being more clear where to find the code. thecodingtrain.com/CodingChallenges/104-linear-regression-tfjs.html github.com/CodingTrain/website/tree/master/CodingChallenges/CC_104_tf_linear_regression

  • @mikelezhnin8601

    @mikelezhnin8601

    6 жыл бұрын

    Thanks Daniel!

  • @rained23JMTi
    @rained23JMTi5 жыл бұрын

    why do you create m and b with random 1 tensor ?

  • @scepto43

    @scepto43

    4 жыл бұрын

    like in any project, you need to start somewhere, so he uses some random variable as a starting point

  • @melvinlijiabraham4165
    @melvinlijiabraham41656 жыл бұрын

    With "rmsprop" optimizer, I got 2 lines at the same time...

  • @minhnhattranle3392
    @minhnhattranle33924 жыл бұрын

    Daniel code a time machine...

  • @JimishFotariya97
    @JimishFotariya976 жыл бұрын

    tf.variable( obj ) also works as obj.variable()

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    Thanks for the tip!

  • @rutgerdegraaf5277
    @rutgerdegraaf52776 жыл бұрын

    lol do you have a green laptop on your left? =) 12:20

  • @RingoDemichet

    @RingoDemichet

    6 жыл бұрын

    Yep he does, he uses it to manage his stream, the chat and so on I believe :)

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    yes, this is correct!

  • @mickolesmana5899
    @mickolesmana58996 жыл бұрын

    i am still confused about why do it need to be squared? can someone explain to me

  • @blasttrash

    @blasttrash

    6 жыл бұрын

    are you referring to the squared error? if so in most of the math applications you use that to avoid polarity. Like you just want the difference between two numbers and you don't care about their sign. For example take two numbers 2 and 5. The difference between them is 3. However depending on which comes first, your subtraction may give you different results. That is 2 - 5 = -3 while 5 - 2 = 3 In linear regression, you are only concerned with 3(which would be your error for that data point). You are not concerned about the negative sign or positive sign.So to avoid having to worry about negative values, you usually square them. So both -3 and 3 would become 9. Eventually you would still be reducing the error that way. Hope it explains.

  • @rained23JMTi

    @rained23JMTi

    5 жыл бұрын

    @@blasttrash thanks for explanation, so by squaring even the value become 9, it doesnt affect the output that we want ? like it should be 3 not -3 or 9

  • @blasttrash

    @blasttrash

    5 жыл бұрын

    @@rained23JMTi True, I guess it does effect the output a bit, but it only effects the speed at which we find the answer I guess. I mean if we took 3, we would probably reduce that error 3 to being 0(or close to 0) within say 2 iterations. On the other hand, if we took it as 9, it would probably take 5 iterations. But in the end, it doesnt matter since we will be solving or reducing the error down to zero in either of those cases. Now I am pretty sure there are some other advantages of squared error(although idk what they are). If you check this wiki link, it talks about some bias and variance, I guess thats where the answer lies. However I do not fully understand those concepts, so I will wait until someone more knowledgeable answers it. en.wikipedia.org/wiki/Mean_squared_error

  • @matpetics

    @matpetics

    5 жыл бұрын

    @blasttrash haha...look at 5-2=3 and 9-12=-3, and if we add them we get 0. But 2 is not a good approximation to 5 nor 12 is to 9. That's just one reson. The other one is that square function is smooth, and when we want to find a (local) min or max it's easier to work with differentiable functions. And the third reason is that square function is also convex, which makes optimizatiom much much easier. The local optimum is global!

  • @aleksandermagda8412
    @aleksandermagda84126 жыл бұрын

    0:39 Have you liked your own video ? :D

  • @TheCodingTrain

    @TheCodingTrain

    6 жыл бұрын

    always 😂

  • @blasttrash

    @blasttrash

    6 жыл бұрын

    that reminds me of Snape's always from Harry Potter. lol

  • @lmmartinez97
    @lmmartinez976 жыл бұрын

    There's something i don't really understand. I've made a program similar to this, but on Fortran. When I was taught how to do it, all they said was "you just throw in two vectors with your data, and make the program return two coefficients that plot the line". How is this any faster or more efficient than just making the computer operate with arrays? I don't understand why you specifically need to make them tensors -i assume it is because tf functions only accept tensors as arguments-. Does the implementation of the optimizer as a native function make the execution any faster?

  • @oscarmejia8306
    @oscarmejia83065 жыл бұрын

    im confused af

  • @PietroYT
    @PietroYT5 жыл бұрын

    Thank you for your great work. It really helps. I just wanted to share an implementation done with React.js and SVG. github.com/gpietro/reactflow-linear-regression

  • @TheCodingTrain

    @TheCodingTrain

    5 жыл бұрын

    Nice work! You can submit a link to the coding train website if you like! github.com/CodingTrain/website/wiki/Community-Contributions-Guide

  • @roizdoci
    @roizdoci6 жыл бұрын

    are you using braces? you speech changed.... you sound like you're using braces or have lisp tongue. lol