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
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
6 жыл бұрын
Ah, shoot, you are absolutely right. Pinning this comment!
@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 :)
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
5 жыл бұрын
these videos are incredible
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.
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!
I love this channel because it has time travels. love it when he does that
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
6 жыл бұрын
Oh, awesome, I did not know this!
I've done many ML courses, but yours way to explain is being the best! Congratiulations!!!
Yayyy! Keep up the good work Daniel
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.
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 :-)
omg didn't notice it lasted 44 minutes! It was a blast, good video!
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.
Thanks for your video tutorial, they are great.
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!
Hooray ! A coding challenge with tensorflow !
This is out of my range!!!
finally get the real example with tensorflow js coool
awesome project :)
Excellent! Now we're coding with Power!
Thanks!!
great tutorial bro
the future thing was awesome :D
I first noticed that a tensor flow was possible with js.It's very very amazing!!
THE FUTURE THING WAS JUST AWESOME!!!!!!!!!!!!!!!!!!!
the future dan was awesome lol. i felt like i was really watching a movie. :P
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?
Do you have a tutorial on how you create your coding environment?
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
at minute 18 the Sub() function, what is the use? and could it be done in other ways for this linear regression?
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
What are the only five Tensors left with tf.memory().numTensors ? (there's m,b and ...)
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?
5:28 when my teacher asks me a question
u should do a tutorial about that how future of you breaking in the middle :) that was some Hollywood material :) like it :))
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?
I'm entertained.
Unable to draw the line..... After writing plotted point function
Why is there 5 tensors instead of just 2 at the end ?
where is the codes he used in this video ? in which github repo?
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
6 жыл бұрын
Great feedback, thank you!
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
5 жыл бұрын
it minimizes the loss function using the optimizer's algorithm
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
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 )
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
6 жыл бұрын
It is not wrong, it is actually a better approximation but the math and coding is just harder.
can anyone send me the complete code
Where is the github page for this? Or am I just blind and it is right there in the description?
@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
6 жыл бұрын
Thanks Daniel!
why do you create m and b with random 1 tensor ?
@scepto43
4 жыл бұрын
like in any project, you need to start somewhere, so he uses some random variable as a starting point
With "rmsprop" optimizer, I got 2 lines at the same time...
Daniel code a time machine...
tf.variable( obj ) also works as obj.variable()
@TheCodingTrain
6 жыл бұрын
Thanks for the tip!
lol do you have a green laptop on your left? =) 12:20
@RingoDemichet
6 жыл бұрын
Yep he does, he uses it to manage his stream, the chat and so on I believe :)
@TheCodingTrain
6 жыл бұрын
yes, this is correct!
i am still confused about why do it need to be squared? can someone explain to me
@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
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
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
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!
0:39 Have you liked your own video ? :D
@TheCodingTrain
6 жыл бұрын
always 😂
@blasttrash
6 жыл бұрын
that reminds me of Snape's always from Harry Potter. lol
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?
im confused af
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
5 жыл бұрын
Nice work! You can submit a link to the coding train website if you like! github.com/CodingTrain/website/wiki/Community-Contributions-Guide
are you using braces? you speech changed.... you sound like you're using braces or have lisp tongue. lol