I TRAINED AN AI TO SOLVE 2+2 (w/ Live Coding)
deep learning machine learning arxiv explained neural networks ai artificial intelligence paper 2+2 twitter woke math algebra james lindsay culture definition gan generative adversarial networks generator discriminator live coding deep learning tutorial
#ai #tech #code

A whole bunch of humans are arguing whether 2+2=4 or 2+2=5. Pointless! Let the machines handle this!

Colab: https://colab.research.google.com/drive/1tDjFW7CFGQG8vHdUAVNpr2EG9z0JZGYC?usp=sharing

Disclaimer: This is a joke.

Links:
YouTube: https://www.youtube.com/c/yannickilcher
Twitter: https://twitter.com/ykilcher
Discord: https://discord.gg/4H8xxDF
BitChute: https://www.bitchute.com/channel/yannic-kilcher
Minds: https://www.minds.com/ykilcher
Parler: https://parler.com/profile/YannicKilcher
LinkedIn: https://www.linkedin.com/in/yannic-kilcher-488534136/

If you want to support me, the best thing to do is to share out the content :)

If you want to support me financially (completely optional and voluntary, but a lot of people have asked for this):
SubscribeStar: https://www.subscribestar.com/yannickilcher
Patreon: https://www.patreon.com/yannickilcher
Bitcoin (BTC): bc1q49lsw3q325tr58ygf8sudx2dqfguclvngvy2cq
Ethereum (ETH): 0x7ad3513E3B8f66799f507Aa7874b1B0eBC7F85e2
Litecoin (LTC): LQW2TRyKYetVC8WjFkhpPhtpbDM4Vw7r9m
Monero (XMR): 4ACL8AGrEo5hAir8A9CeVrW8pEauWvnp1WnSDZxW7tziCDLhZAGsgzhRQABDnFy8yuM9fWJDviJPHKRjV4FWt19CJZN9D4n
 Hi there, you might have seen the recent debate about 2 plus 2, where everyone tries to weigh in, the big question being is 2 plus 2 equal to 4, or is 2 plus 2 equal to 5? And for some reason, the entirety of Western civilization hangs in the balance right here. But everyone's missing the point. Everyone's just kind of arguing about this. But I want to point something out right here. Let's have a look at the accounts arguing right here. You know, James, Eric, you know what all of these have in common? They're humans. Humans arguing about fundamental questions of the universe and culture. What could possibly go wrong? So today, we're going to replace fallible, weak minded humans by AI. We're going to build an AI that's going to answer the question, what is 2 plus 2? Now first thing we're going to do is to import PyTorch. If you're using TensorFlow, what's wrong with you? Come on, just checking whether CUDA is available. CUDA is basically shorthanded AI for magic. So don't worry about that part. Now we're going to borrow quite a bit of code from the PyTorch example, because they've already implemented sort of the same thing. So the model we're going to use right here is going to be a generative adversarial network. Now you might be wondering, hey, is it really smart to build AI on something that's called adversarial? Isn't that a little bit dangerous? To that I say, all right now, so we're going to grab the code from over here. First thing we need is the model itself. Now the model is composed of a generator and a discriminator. The generator is right here. Plink plonk. Let's plop that in here. That looks good. Look at that generator. Those convolutions, batch norms, relu's. This is going to be so artificial and so intelligent, you won't believe it. So the generator is responsible for basically outputting things. In our case, what we're going to input a two and a plus and a two, and then the output should be, you know, whatever the result of that is. Now as a data set, we're going to use the famous MNIST data set. This data set is a very challenging data set. It's very large data set. But I think in order to tackle an important question like this, we need to go for the cram de la cram of data sets. So MNIST is a data set that contains a lot of these handwritten digits. You might think these are just numbers, but these are more than numbers. These numbers have a meaning. So the computer just sees this in numbers. But as a human, you would see this right here. Be the zero. This data set is filled with digits. Four. Wow. That's one of the things we need. Look at that. A nine. Beautiful. Beautiful. So our goal is going to be to try to make the network learn what two plus two is. Now if you know machine learning, you know that you need training data. So we need a labeled data set of two plus two equals and then whatever two plus two equals. So first, we're going to filter out all of the examples where that show a two. So we need to train this network, right? So we need a number of training steps, you know, in AI, we like to train for a lot of steps. Let's just go for 9000. What we'll do is we'll train 9000 times 64 images and the AI is going to learn what two plus two is. Alright, so in each step, we need to create a batch of training samples. What we need is a two plus and two. So for the two is we can just select two of the two's that we had before. Now the plus is a little bit more tricky. So in order to make a plus, there's none in the MNIST data set. You have to understand the MNIST data set is also quite old. I think it was invented before the plus sign was invented. So that's not in the data set. So we have to create a plus by ourselves. It's going to be hard, but we'll give it a try. Now I'm usually way too dumb to use mesh grid, but I'm just gonna try. I mean, you know, what can go wrong? Okay, so as you can see, we're absolutely on the wrong track right here. Ladies and gentlemen, the most beautiful plus in the history of AI. Alright, so we got a plus and we got all of our two's. So now let's put them together. Look at that. Two plus two, next sample, two plus two, next sample, two plus two. So our AI is going to be trained on data samples just like this. Now in order to make the generator accept samples like this, we sort of need to change a little bit because if we try to just put this into the generator, probably it won't work. You see, there's an error. The generator is not artificially intelligent enough yet. So we need to make it take samples. So our samples are of size 28 by 84. And what the generator right now expects is a sample of size 100 by 512 by four by four. So you may notice we have never made use of our batch size. So let's fix that right now. So now we're training in batches of images, but it's still not cool for the generator. So we need to change the generator right here. What's this good for? Nothing, nothing. All right, so it expects the input to be of a certain size. And we are going to change that right here. We also don't want any strides. Strides are for losers. And let's see where that gets us. Okay, so we made our generator accept images that we want and produce images of the size that we want. Now the entire question here is we need labels for our training data set because who's to say what two plus two is. And as I said, usually I would outsource this to grad students, but these are humans as well. So we're kind of in a pinch right here. So what we're going to do is employ a heuristic. We're going to ask our machine right here what two plus two for the training examples is. Okay. So in Python, you can do this by typing two plus two. And you know, in this case, that happens to be four, but who knows? So for each of these training examples, we're going to take the class label, which is provided in the data set, and we're going to take these class labels and add them together. And whatever comes out is going to be the label for this. In this case, it's four, you know, but it could be anything. And we're just going to use these as training data for our model. So for that, we're going to need the label of the first sample and the label of the second sample. And our final label is simply going to be label one plus the label two. As I said, this is a heuristic for training the AI. Now, usually in a generative adversarial network or a GAN for short, you'd have something that's called a generator, which we do. And you'd have something that's called a discriminator. Now, I have my problems with this discrimination. There is no space for discrimination in the AI field. So we're going to leave away the discriminator right here. I'm sorry. I'm sorry. We're going to directly go to the loss from the generator. In order to calculate the loss, we need a reference. And for that, we're simply going to go to our data set with our label and find any of the images that correspond to that label. So if our heuristic, if our oracle says two plus two is equal to nine, we're just going to go to our data set, get a nine and put that as a training output. Okay. Okay. So if we look at one of the labels that just happens to be a four in this case, but we're going to go through the entire number of 9000 steps. And in each steps, we'll train 64 of these different combinations of two plus two. And we'll give one of the labels each time and we'll see what the AI comes up with. For that, we need a loss. Now the loss we're going to use here is going to be the L2 loss. Now there's some controversy, but you know, it is the most powerful loss proven and we have to employ the most powerful tools. So let's do that. So our loss here at the beginning is 509. Now that's a lot of loss. That's a big loss. We need to get that loss down. And to do that, we need one of these optimizers. Now optimizers are kind of the secret workhorses of AI and people don't talk about them enough. I wish there was like a field of research that deals with optimizers, like could be called optimization or something like this. I'm not sure. I just, I just think it would make a lot of sense. So my favorite learning rate is three E minus four just because it contains all of the different things, like a letter and a dash. And that seems like a pretty good thing to do. So we're going to use Adam here as an optimizer. Adam, I know, I don't know Adam personally, but I know a couple of his friends and they tell me he's pretty good. So you know, it's going to go zero grad and I'm dumb. So I need to look up how to use an optimizer and boom. Okay. Okay. So it's again a four. I'm sorry about this. I think this is it. This is it. This is AI history right here, right now for five steps, 10 steps. All right. I have waited and waited and waited and it's finally done. We have now trained the generator to calculate what two plus two equals from the training data set. So now we actually need to ask it what is two plus two. And of course we can't ask it a sample that it has already seen. We need to take a new sample from the test set as is customary in machine learning. So let's get the MNIST test set. Now the test data set consists of images as does the train data set, but the model has never seen the test data set before. This is a property we call generalization. So let's find two nice twos. All right. That's the first one. Okay. These are two nice twos. Let's put them together. Okay. So this is going to be our input to the generator. Okay. So I'm putting the test sample here into the generator that is trained and I've labeled the output in all caps just to tell the model that this is really important computation. I'm just going to run this cell for a couple of times just to make sure that generator is in fact very sure about how important that is. All right. I think that's enough. Let's have a look at that final output. I'm shaking. Are you ready for AI history?
 Hi there, you might have seen the recent debate about 2 plus 2, where everyone tries to weigh
 in, the big question being is 2 plus 2 equal to 4, or is 2 plus 2 equal to 5?
 And for some reason, the entirety of Western civilization hangs in the balance right here.
 But everyone's missing the point.
 Everyone's just kind of arguing about this.
 But I want to point something out right here.
 Let's have a look at the accounts arguing right here.
 You know, James, Eric, you know what all of these have in common?
 They're humans.
 Humans arguing about fundamental questions of the universe and culture.
 What could possibly go wrong?
 So today, we're going to replace fallible, weak minded humans by AI.
 We're going to build an AI that's going to answer the question, what is 2 plus 2?
 Now first thing we're going to do is to import PyTorch.
 If you're using TensorFlow, what's wrong with you?
 Come on, just checking whether CUDA is available.
 CUDA is basically shorthanded AI for magic.
 So don't worry about that part.
 Now we're going to borrow quite a bit of code from the PyTorch example, because they've
 already implemented sort of the same thing.
 So the model we're going to use right here is going to be a generative adversarial network.
 Now you might be wondering, hey, is it really smart to build AI on something that's called
 adversarial?
 Isn't that a little bit dangerous?
 To that I say, all right now, so we're going to grab the code from over here.
 First thing we need is the model itself.
 Now the model is composed of a generator and a discriminator.
 The generator is right here.
 Plink plonk.
 Let's plop that in here.
 That looks good.
 Look at that generator.
 Those convolutions, batch norms, relu's.
 This is going to be so artificial and so intelligent, you won't believe it.
 So the generator is responsible for basically outputting things.
 In our case, what we're going to input a two and a plus and a two, and then the output
 should be, you know, whatever the result of that is.
 Now as a data set, we're going to use the famous MNIST data set.
 This data set is a very challenging data set.
 It's very large data set.
 But I think in order to tackle an important question like this, we need to go for the
 cram de la cram of data sets.
 So MNIST is a data set that contains a lot of these handwritten digits.
 You might think these are just numbers, but these are more than numbers.
 These numbers have a meaning.
 So the computer just sees this in numbers.
 But as a human, you would see this right here.
 Be the zero.
 This data set is filled with digits.
 Four.
 Wow.
 That's one of the things we need.
 Look at that.
 A nine.
 Beautiful.
 Beautiful.
 So our goal is going to be to try to make the network learn what two plus two is.
 Now if you know machine learning, you know that you need training data.
 So we need a labeled data set of two plus two equals and then whatever two plus two
 equals.
 So first, we're going to filter out all of the examples where that show a two.
 So we need to train this network, right?
 So we need a number of training steps, you know, in AI, we like to train for a lot of
 steps.
 Let's just go for 9000.
 What we'll do is we'll train 9000 times 64 images and the AI is going to learn what two
 plus two is.
 Alright, so in each step, we need to create a batch of training samples.
 What we need is a two plus and two.
 So for the two is we can just select two of the two's that we had before.
 Now the plus is a little bit more tricky.
 So in order to make a plus, there's none in the MNIST data set.
 You have to understand the MNIST data set is also quite old.
 I think it was invented before the plus sign was invented.
 So that's not in the data set.
 So we have to create a plus by ourselves.
 It's going to be hard, but we'll give it a try.
 Now I'm usually way too dumb to use mesh grid, but I'm just gonna try.
 I mean, you know, what can go wrong?
 Okay, so as you can see, we're absolutely on the wrong track right here.
 Ladies and gentlemen, the most beautiful plus in the history of AI.
 Alright, so we got a plus and we got all of our two's.
 So now let's put them together.
 Look at that.
 Two plus two, next sample, two plus two, next sample, two plus two.
 So our AI is going to be trained on data samples just like this.
 Now in order to make the generator accept samples like this, we sort of need to change
 a little bit because if we try to just put this into the generator, probably it won't
 work.
 You see, there's an error.
 The generator is not artificially intelligent enough yet.
 So we need to make it take samples.
 So our samples are of size 28 by 84.
 And what the generator right now expects is a sample of size 100 by 512 by four by four.
 So you may notice we have never made use of our batch size.
 So let's fix that right now.
 So now we're training in batches of images, but it's still not cool for the generator.
 So we need to change the generator right here.
 What's this good for?
 Nothing, nothing.
 All right, so it expects the input to be of a certain size.
 And we are going to change that right here.
 We also don't want any strides.
 Strides are for losers.
 And let's see where that gets us.
 Okay, so we made our generator accept images that we want and produce images of the size
 that we want.
 Now the entire question here is we need labels for our training data set because who's to
 say what two plus two is.
 And as I said, usually I would outsource this to grad students, but these are humans as
 well.
 So we're kind of in a pinch right here.
 So what we're going to do is employ a heuristic.
 We're going to ask our machine right here what two plus two for the training examples
 is.
 Okay.
 So in Python, you can do this by typing two plus two.
 And you know, in this case, that happens to be four, but who knows?
 So for each of these training examples, we're going to take the class label, which is provided
 in the data set, and we're going to take these class labels and add them together.
 And whatever comes out is going to be the label for this.
 In this case, it's four, you know, but it could be anything.
 And we're just going to use these as training data for our model.
 So for that, we're going to need the label of the first sample and the label of the second
 sample.
 And our final label is simply going to be label one plus the label two.
 As I said, this is a heuristic for training the AI.
 Now, usually in a generative adversarial network or a GAN for short, you'd have something that's
 called a generator, which we do.
 And you'd have something that's called a discriminator.
 Now, I have my problems with this discrimination.
 There is no space for discrimination in the AI field.
 So we're going to leave away the discriminator right here.
 I'm sorry.
 I'm sorry.
 We're going to directly go to the loss from the generator.
 In order to calculate the loss, we need a reference.
 And for that, we're simply going to go to our data set with our label and find any of
 the images that correspond to that label.
 So if our heuristic, if our oracle says two plus two is equal to nine, we're just going
 to go to our data set, get a nine and put that as a training output.
 Okay.
 Okay.
 So if we look at one of the labels that just happens to be a four in this case, but we're
 going to go through the entire number of 9000 steps.
 And in each steps, we'll train 64 of these different combinations of two plus two.
 And we'll give one of the labels each time and we'll see what the AI comes up with.
 For that, we need a loss.
 Now the loss we're going to use here is going to be the L2 loss.
 Now there's some controversy, but you know, it is the most powerful loss proven and we
 have to employ the most powerful tools.
 So let's do that.
 So our loss here at the beginning is 509.
 Now that's a lot of loss.
 That's a big loss.
 We need to get that loss down.
 And to do that, we need one of these optimizers.
 Now optimizers are kind of the secret workhorses of AI and people don't talk about them enough.
 I wish there was like a field of research that deals with optimizers, like could be
 called optimization or something like this.
 I'm not sure.
 I just, I just think it would make a lot of sense.
 So my favorite learning rate is three E minus four just because it contains all of the different
 things, like a letter and a dash.
 And that seems like a pretty good thing to do.
 So we're going to use Adam here as an optimizer.
 Adam, I know, I don't know Adam personally, but I know a couple of his friends and they
 tell me he's pretty good.
 So you know, it's going to go zero grad and I'm dumb.
 So I need to look up how to use an optimizer and boom.
 Okay.
 Okay.
 So it's again a four.
 I'm sorry about this.
 I think this is it.
 This is it.
 This is AI history right here, right now for five steps, 10 steps.
 All right.
 I have waited and waited and waited and it's finally done.
 We have now trained the generator to calculate what two plus two equals from the training
 data set.
 So now we actually need to ask it what is two plus two.
 And of course we can't ask it a sample that it has already seen.
 We need to take a new sample from the test set as is customary in machine learning.
 So let's get the MNIST test set.
 Now the test data set consists of images as does the train data set, but the model has
 never seen the test data set before.
 This is a property we call generalization.
 So let's find two nice twos.
 All right.
 That's the first one.
 Okay.
 These are two nice twos.
 Let's put them together.
 Okay.
 So this is going to be our input to the generator.
 Okay.
 So I'm putting the test sample here into the generator that is trained and I've labeled
 the output in all caps just to tell the model that this is really important computation.
 I'm just going to run this cell for a couple of times just to make sure that generator
 is in fact very sure about how important that is.
 All right.
 I think that's enough.
 Let's have a look at that final output.
 I'm shaking.
 Are you ready for AI history?