Placeholder Image

字幕列表 影片播放

  • I don't know where you arrived from,

  • but if you didn't arrive from the previous videos

  • about training your own neural network,

  • you might feel a little bit lost because what I'm going to do

  • here entirely depends on the previous few videos

  • where I trained model to classify musical notes based

  • on mouse clicks in a P5 canvas.

  • So what I'm going to do in this video

  • is change this word from classification--

  • I'm going to do it right now-- to regression,

  • and this is going to drastically change

  • what the neural network outputs and how I use that output.

  • But before I start changing anything else in the code,

  • let me talk about what I mean by regression in the context

  • of machine learning.

  • First, let's recap classification.

  • The idea of classification is that the neural network

  • is going to receive some input and end up

  • with a discrete, categorical output,

  • meaning it's going to assign a label C, D, or E.

  • We can think of that as there's three light bulbs.

  • Are those like little drawings of light bulbs?

  • Each light bulb is associated with one of the possible labels

  • or categories, classifications.

  • And if the neural network decides

  • that this particular input corresponds to the label E,

  • then maybe this light bulb would light up.

  • And you could do this.

  • You could build an Arduino with some LEDs

  • and have the results of the machine learning classification

  • light up a particular LED.

  • A regression, you could think of the output

  • being a slider or maybe a dial.

  • As the data comes into the neural network,

  • the dial ends up at some value between some minimum

  • and maximum.

  • The output is numerical continuous output.

  • So classification is one of a discrete set of possibilities,

  • and regression is any value between a given range.

  • Now you can still have multiple outputs in a regression

  • because you can have multiple continuous values,

  • but in this case, I want to redo this example

  • by having a single output and that output

  • be the frequency value.

  • So if I change the task to regression,

  • now I want my output to be tied to a frequency.

  • No longer do I want during the training process

  • to give target labels.

  • I want to give target numbers so that it

  • will get the number as part of the regression output.

  • So this is my code for data collection.

  • When I click the mouse, the inputs are in x and y,

  • and the target is a target label.

  • But I don't want it to be a target label anymore.

  • I want it to be a frequency.

  • And I can figure out what that target frequency

  • is by looking it up in this particular JavaScript object.

  • So I can say let target frequency equal notes

  • target label, and in fact, that's

  • something that I'm already doing down here.

  • So let's replace this with target frequency.

  • Now, ordinarily, I might remove target label

  • completely as a variable in the system

  • because I don't need the label anymore

  • for the machine learning model, not using CDE and not

  • using the note name for training the model.

  • But while I'm collecting the data,

  • I still would like to see the note C or A or G

  • because I think that's less awkward than drawing

  • the frequency number in the window.

  • So crazily, I think I've actually

  • done with all that I need for the first two steps--

  • the first two steps being collect the data

  • and train the model.

  • I've changed the task to regression,

  • and now I obtained the output to a single output node that

  • just has a frequency value.

  • So I should be able to run all of the bits of code

  • that I did before and have a train the model.

  • Let's see if that works.

  • [MUSIC PLAYING]

  • All right, collected some data-- let's train the model.

  • And it worked.

  • I mean, it worked in the sense that I

  • didn't don't see any errors in the console,

  • and I see a nice graph with the loss going all the way down.

  • Incidentally, the lost went way down very, very quickly.

  • / I probably don't need 200 epochs.

  • Maybe this regression problem is a little bit easier for it

  • to learn more accurately.

  • Who knows?

  • But it optimized very quickly.

  • So now that collecting data and training the model is done,

  • I need to just do that last stage of basically deploying

  • the model and making predictions with new data.

  • And I don't think this is going to work.

  • I haven't adjusted the code at all.

  • It's still looking for a label and all that.

  • So who knows what will happen.

  • First of all, it's writing the word frequency here and just

  • playing one particular note.

  • So let's go look at the prediction code

  • and see what need to adjust for a regression.

  • First thing is because we're no longer doing classification,

  • we shouldn't call model.classify with the given inputs.

  • We want to change this to model.classify.

  • So predict is the function name for a regression.

  • Classify is the function name for a classification.

  • Let's collect it again and retrain the model.

  • It's trained.

  • Let me click into the canvas, and let's look and see

  • what came out here.

  • So this is what I get back.

  • I get back an array.

  • I only have one output in this particular model,

  • and so I look at the 0 element of that array.

  • This is the value it predicted.

  • And I have a label that in case I had multiple things,

  • I could sort of know which goes with which.

  • So based on what's here, I want to change this to results index

  • 0 .value.

  • That's the frequency that I want to play.

  • Now what do I want to draw?

  • Maybe I'll actually look at the number.

  • So let's actually also draw that,

  • but maybe we'll take away the decimal place

  • by using floor just so it's less busy,

  • takes up less room in the canvas itself.

  • OK, all right, let me add that save data feature.

  • Before I collect the data, let me just change

  • that for epochs to 50 because I don't need 200 epochs.

  • Now I'm going to collect a bunch of data,

  • so let me make a bunch of Cs.

  • I clicked around somewhat arbitrarily,

  • but let me now actually save this,

  • so I don't have to always collect the data again

  • every single time.

  • So I'm saying that to a JSON file.

  • I am now going to train the model.

  • What do I expect to happen?

  • Before when I was doing classification,

  • I would expect to hear a C when I click near the Cs and F

  • when I click near the Fs.

  • And I would hopefully get something similar to that.

  • Like does this sound like a C to you?

  • You can hear it's changing, though, f.

  • But look, I'm getting a different note every time

  • because the regression, once again, remember is like a dial.

  • So if I have the notes C over here

  • and I have the note E over here, ideally,

  • when I click here, I'm going to get a note that's

  • in between like D.

  • But the notes are completely irrelevant here.

  • It's really all about the numbers, the idea

  • being that if I have the number 200 here

  • and I have the number 300 here, clicking in between

  • is like having a slider or a dial

  • going between 200 all the way up to 300.

  • And while this works in a somewhat obvious way

  • in this two-dimensional space with me putting

  • a lot of Cs over here and a lot of Gs over there,

  • you could imagine how this could become a much more

  • sophisticated musical instrument,

  • or the output doesn't even have to be a musical frequency,

  • a sound frequency.

  • There's so many possibilities.

  • But now we've seen that in addition

  • to using the ML5 neural network for classification.