Back to Index

fast.ai APL study session 6


Chapters

0:0
13:33 Gradients
24:8 Point Free Programming
27:55 Function Composition
33:17 Calculating the Golden Ratio
34:47 Meta Mathematics
36:59 Power Function
38:13 Multiplication
44:28 The Golden Ratio
44:37 Golden Ratio
47:11 Fixed Point

Transcript

>> Hi, Taneesh, how are you? >> Hello. Oh, I'm behind, but, yeah, hello. >> You're a silhouette today, huh? >> Yeah. Hello. >> Hello, Taneesh, how are you? >> Good to see you, me, how are you? >> Good. Thank you. Where are you joining us from? >> I'm from Austin, Texas.

>> Okay. >> So, our company, when we initially started doing image recognition based deep learning, we followed your course, and that was the starting point for us on our deep learning journey. >> Oh, excellent. >> Thanks for the course in Austria. >> I hope it's going okay. >> Yeah, it's going okay.

I'll learn hardworking on deep learning. It's the go-to resource for new hires. >> Hello, hello. >> Hi, how are you? >> Oh, I can hear you. You can hear me. That's wonderful. Good, good. >> Sounds personalized. >> Yes, I thought I had some audio issues there, but it seems all is good.

How are you doing, Jeremy? >> I'm okay. I'm making some good progress on getting the course ready. It's mainly been about getting the process working well. >> Wonderful. >> And I'm illustrating it with Dali two pictures. >> Wow. Wow. That's going to be awesome. >> Do you have access?

>> I just got it. So, good timing. >> Sweet. >> I still haven't got an access. I think I signed up like a week later, so that might have been why. I'm still waiting. >> I can try and do a demo if you like. >> That would be awesome.

This is so wonderful with abstract concepts. I mean, for illustrating articles, you know, I think this is going to have an amazing -- well, a group of people, a very wide group of people who might benefit from it. If I were a journalist and were working on an article, I mean, wow, this is so much better than going to stock photo or whatever it's called.

>> I mean, they've got a non-commercial restriction at the moment, but presumably at some point they'll have some kind of paid service. And -- >> Oh, look, Molly's here. First time I think Molly's joined us. Hi, Molly. Have you joined us before? Possibly not able to chat. That's all right.

Hello, anyway. >> No, I haven't had a chance to join before. >> Oh, well, welcome. Have you watched any of the videos, done any APLing as yet? >> I have started the first video I just saw that the study group had started yesterday. So yeah. >> No worries. I'm going to be caught up in no time.

This is what it looks like. So who wants me to -- yeah, who wants to suggest a prompt? You can always put it in the chat if you like or just tell me. What does the fox say? Wait, you're meant to be giving something to draw a picture of.

Is that so you can draw a picture of? >> Maybe a student working on an assignment using array programming language. >> A student working on an assignment using an array programming language. Do you want like a photo realistic photo? Do you want a 3D render? Do you want a pencil drawing?

What kind? >> Maybe something with colors. So pencil drawing, not so much, but -- >> You can do a color pencil drawing or oil pastels. >> Oh, yeah. Yeah. That sounds great. >> A color pencil drawing. I don't think Darlene is interested to know what a thing about -- oopsie dozy -- about array programming from what I've been able to see.

>> Yeah, I don't imagine it should, but, you know, maybe it will surprise us in some way. >> Digital art for striking high quality images, oh, that's good to know. It's taking a bit longer than usual for some reason. >> I wonder if people are going to make movies with Gally and so on, like explain every scene to it, and it makes -- >> Something's happening.

>> Okay, so this is a problem. I often find when you kind of put these extra details, it tries to write things about it, but it doesn't know how to write. So it doesn't really know what array programming is, so it's written some words that it thinks look a bit like that.

>> Right. >> So if we did, like, a programming assignment -- >> Yeah, the first and sixth ones, they look somewhat accurate, I would say. >> Yeah, this person's coding by drawing on a screen. >> Do you have to pay for this, to get access to Gally? >> This is free.

>> Anyone can just go in here, is it? >> You have to apply, and then you just wait for, well, months, I think it was. >> I may try. >> All right, well, while we're waiting -- >> That's been happening. Not too much. >> I added a blog post on my favorite advent of code problem from last year, and -- >> This one here?

>> Yeah, that one was, I don't know, just really, really cool. It kind of worked for APL very, very well. >> Smoke basin. So this is a map representing the height of the ground at that grid point. This is the high bits, okay, that's cool. >> Part one, you have to find -- what do you do?

You have to find the load points, I think. >> Yep, find the load points. Add one to the value of each load point, and add them up. Cool. >> That's a good one from Tanishk, much more creative, a professor who is a cat teaching deep learning in a classroom, photorealistic.

>> Let's see how this went. Okay. >> It is a pencil drawing. >> Yeah. >> I wonder if it would do better if you told it was programming in hieroglyphics, but hieroglyphics are more than array programming. >> Interestingly, when I started watching something about APL on YouTube, it started recommending new videos on hieroglyphs and I guess Egypt and stuff, so it can draw the connection.

All right, let's open up our one. I don't think there's anything to pull, but just in case -- there is, oh, because of the GitHub pages. I thought we could make a custom operator today. >> A cat is not a professor. >> A professor who is a cat, hmm.

>> I guess at least it's got glasses on the top left. >> Yes, that could well, but clearly somebody else in the background is doing the teaching. I did an interview this morning with A16 sets, a newish online magazine kind of thing. We talked about this idea that it's kind of prompt engineering as a skill now.

It's a skill I definitely don't have yet. >> I feel like it doesn't generalize from one model to another. >> Exactly. All right. Has anybody got a favorite, like, drawing program for Mac? Have you tried Procreate? >> Not something that fancy, like something just for doing, you know, these kind of things.

And I also need to find out why my right mouse button doesn't work, because since it doesn't work, I'm not sure how to insert page. File, no, home, oh, yeah, a page. I'm guessing everybody is probably familiar with the idea of gradients, but it's fine if you're not, because I thought we could just briefly mention it.

So if we've got something like a quadratic, then the gradient at some point is the slope at that point. So this would be the gradient at this point, is that slope. And you may or may not remember that the slope is equal to the rise over the run, which is the change in y over the change in x.

So if this is x2 comma y2, and this is x1 comma y1, then the slope is y, is rise over the run, I said, so it's from around, is y2 minus y1 over x2 minus x1. OK, and so what you could do is you could pick some point a bit, you know, like, so this is the point we're going to pick, and then you could just add a little bit to x, like 0.01x, and this is the point here.

So in this case, you could actually write y2 in a different way. So if you've got, so this is like, this is, this is some function of x, like x squared, this is x, right? So you could change y2 to instead be function of x plus a little bit, like, say, 0.01, and y1 would be function of, oops, would be the function of x at the starting point, and then you divide by the amount that you moved x by, which in this case would be 0.01.

That would be another way of doing the change in y over change in x. Does that make sense so far? And so this is like an approximation of the derivative, it's the slope at a point. And I thought we could try and do this in APL. So if we picked a function, I'll show you something interesting.

You can do a function a couple of ways. I think we've already learnt that you can do a function like this. If you want to do y squared, you can do omega squared, that's one way to do it. But you could also just do this, right? That just means power of, so you could do something like this, if that makes sense.

So then actually what we might do first is a nice operator. So it just returns the power operator. Yeah, so this is saying that f is the power function. And that might not sound very interesting yet, but you could make it a bit more interesting by saying f is a combination of a function and an operator, and we could give that a more sensible name, for example.

And that's exactly the same as doing, oops, it's exactly the same as doing that. So here's an example of something we could do. Let's learn a new operator, which is called bind, which is J. So we can get the help, oh, that didn't work. So we can get the help for it by typing help.

Okay, so this thing, this symbol, you'll hear it a lot, it's called Jot. And it said, oh, this is finished. There we go. Professor, there was a cat, it's too hard, but a cat professor is fine. Not sure what happened with four, but clearly professors point at things, this cat professor's just faking it because there's a real, in fact, most of the cat professors, not most, some of them are faking it, but yeah, professor's point at whiteboards with chalk or sticks, apparently.

It's not obvious, it's deep learning, but it certainly looks very messy. Yeah, it's not good. Well, yeah, that one has like some sort of network or something. It looks like, yeah, interesting. The first one is maybe looking at some activation function. Yeah. Could well be, okay. Now Jot is a dyadic operator.

So that means that there won't be a monadic. And it can be a couple of things, it can be beside or bind depending on whether you pass it functions or arrays. Now, for Python programmers, let's call this Python equivalence partial. It's the same as using partial in Python. So that's a kind of a functional programming idea, and most of the functional programming ideas in the standard library are in the func tools module.

That's where you can import partial from, and here's what partial does. We define something, and so let's say we could call this power, which does that, three squared. Okay, so what if we wanted to now define squared using power? We could say square equals partial l comma y equals two.

And what that says is, so partial is a higher order function or an APL operator, something that returns a function, and the function it returns is something that's going to call this function passing in this parameter that's going to always set y in the function to two. So I could then say squared three.

Does that make sense? So bind does the same thing. So we could say squared equals, and we can create a function. So we're doing a power of function, we're doing to the power of two. So this is a function, we don't say equals, obviously. And so you can see here, this is this idea that we don't need to use curly brackets and omega and stuff, we can just define a function directly.

This is called point free programming in other languages, and it's basically where you write functions without specifically referring to the parameters they're going to take. And in this case, because this is an operator that therefore returns a function, there's no need for round brackets and whatnot. Does that make sense?

An operator that returns a function. All operators return a function, that's the definition of an operator in APL. Do you remember last time we did operators, we learned about backslash and slash? Yes. And those are? So it's the B site or bind that's an operator, right? So it's return, it returns a function.

Okay. So it takes the argument on the left and on the right. That's right. And returns a function. And returns a function. So this is the function to apply, and then this is the right hand side to bind. And it returns a function. And it's an operator, therefore it returns a function.

Got it. And you can do it the other way as well, which is you could say a power of 2, and then you could just do the opposite, 2 to the power of, so this means 2 to the power of. Rather than to the power of 2, so 2 to the power of 3 is 8.

So that's the equivalent of, this is the equivalent of partial where I bind the right hand side. This is the equivalent of partial where I bind the left hand side. Wow. That makes sense? It makes sense, it's just surprising how succinct it all is, and it's like Lego bricks.

It is, yeah. So let's move some of this stuff into the right spot. Okay, so then, yeah, let's just go like this. So this one's called bind. And bind's a very common computer science term for this, partial function application or bind, in C++ it's called bind. Now the other thing we could do is we could do beside, and beside is what happens if you put, if you just pass two functions to it.

So for example, we can create a function that first does reciprocal, remember monadic divide is reciprocal, and then it does e to the power of, remember star is e to the power of. So this, so if we go, for example, reciprocal of three, and then we do e to the power of that, it's that, and we should find f of three is the same thing.

Does that make sense? So this is called function composition, it's at least as one form of it. So first it does this function, and then takes the result and passes to that function. You can also use the function that is returned diatically, like so, and that is going to be the same as, I think, oh it's not, is it, yeah, two, yeah it is, it's going to be two to the power of a third.

So this will be the cube root of two, yeah, okay, so it first, so in the case if you do it diatically, then it first applies this to the right hand side, and then it applies this to the left hand side and the result of that. And they've got some really nice pictures of this somewhere, I wonder if they're here.

Oh yeah, bind is also called carrying. So there's lots of ways of doing function composition, and so beside, as you can see, it takes y on the right, it passes it through g, which is this function, which was reciprocal, and then f, which was power of, gets the left hand side and the result of that, which interestingly enough is exactly the same thing as if you just put them next to each other, I think, oh no that's different, that's interesting, okay, I'll take that back, there's something in the, it's a, API wiki is good for this stuff, I suppose.

Wait, it says it's the same, that's very confusing, oh it's without the parentheses, okay, so that does the same thing. So there's a few ways of doing the same thing here. So it's a bit confusing that, I mean it doesn't need to be, but it can be if you're not careful that this is a dyadic operator because it takes the left hand side and the right hand side, it returns a function, the function it returns can either be used monadically or diadically, that makes sense?

And so the way it behaves is based on, if it's used monadically it's reasonably straightforward, we just first apply this and then we apply that. If it's diadically then it behaves like this. It makes sense, it's just interesting that it exists like one questions, you know, what it could be used for.

So the purpose of it is to create, the way I think of it is to expand mathematics, right? So in mathematics we stick symbols next to each other and they have meanings, right? But the rules for what symbols you can stick next to each other and when vary a lot depending on the symbol and stuff like that, you know.

So APL tries to just lay out the ground rules and says this is how you can do it and so let me show you my favourite so far example which is calculating the golden ratio. We're going to need some more operators for this. Okay, so we're going to use, the next operator we're going to learn is star diuresis, yeah, which is shift P.

And this is also called power but it's actually the power operator rather than the power function which is confusing so you do actually have to be careful. The power function is just a star. Okay, it is a operator therefore it returns a function, it's dyadic therefore it needs a left-hand side and a right-hand side.

So let's define, I don't know if any of you have done metamathematics but metamathematics is the philosophical foundations of mathematics and there's a small set of theorems that you can use which I think are called the piano theorem, piano axioms, which people used to think you could then derive all of math from although it turns out it's not necessarily true.

And so basically the idea is you'd say okay we're going to create something called zero and the way to create something called equals and equals is defined by saying for every number x equals x and that if x equals y then y equals x and if x equals y and y equals z then x equals z, I mean these are all obviously true things which are kind of defining some basic theorems.

And then it defines this function called capital S, we just basically say it exists and it returns another number and basically it defines it in such a way that it's it's the successor of a number so the successor of zero is one, the successor of one is two, the successor of two is three and so forth.

And we actually can easily define S now because it's plus one, right? And so if we assume that zero exists and we assume the successor exists then we can create the number one and we can create the number two and we create the number three so forth. And so at this point we want to invent addition because in metamathematics you basically are not allowed to assume anything exists except what you put in your premises.

So addition is what happens when we apply S a bunch of times. If we want to add three to zero we would write three S's followed by zero. Does that make sense? So the power function, the power operator simply says how many times to repeat a function. So I want to repeat the function S three times.

So that's the same as writing S space S space S. Okay? So if we want to create a function called add then we basically are going to apply the S function alpha times to omega. So that's going to start with, so for example, two added to three, we'll start with three and then it will apply S twice.

That would be the same as writing S space S space three. Does that make sense? So we just invented addition. Yes, absolutely it does. So we can do the same thing for modification. We can apply, multiply, we need to start at one and then we can multiply. So we're going to multiply by, I'm not sure if it matters which way around this goes, we're going to multiply omega, we're going to multiply by alpha omega times.

Oops. Oh, I wrote volt, which obviously is going to break everything. I need to add. Okay. I'm adding, oh, I'm adding to zero. There we go. So I'm adding to zero. This is what I add each time. This is how many times I add. So we just invent a model play.

So we can of course now invent to the power off and that would be where we'd start with one and we multiply. The number of times we multiply will be, yeah, the thing on the right. So two to the power of three. Okay. Yeah, it is quite mind bending to look at the syntax though, because you know, with the star thingy, the star diuresis, star diuresis, it look already quite interesting when it was, was there a monadic S function, you know, and now it can also, so yeah, this, this S thing you have in the cell 26, it's just, you know, on the right hand side of it and it does what it does, but then you can also use it with dyadic.

Yeah. And it's, you know, another example of just composing this very small piece of functionality that, yeah. So it's applying its left upper end function cumulatively G times. And if it's a dyadic function, it's applied repeated. Oh no, that's not what we want. It's bound. So if there's a left argument, it's bound as the left argument.

So we've basically seen this idea, right, of binding an argument. So that's basically what it's doing. It's saying that this is multiplied by alpha each time. Okay. Now where it gets really fun is that you don't have to put a, a number on the right. You can put a function on the right.

And so this is going to come some way towards answering your question Radek about what's all this for. So we've now got a sequence of five glyphs in a row. Okay. So what does that mean? Well, this cliff here, we know it means take the reciprocal and then add, and as this is on the left, so it's going to be take the reciprocal and add one.

So let's try it. Let's just grab that copy. We'll call that F. Whoops. And so if we go F of one and that's sorry, one F one, I should say that equals two, right? Because it takes the reciprocal of this and then it applies plus to the result and the thing on the left.

So one plus one is two because that's because of this, this is what we get if we do a beside, we first apply reciprocal to the right and then we apply plus to the left and the result. So the reciprocal of one is one and then the left hand side is one and the result of reciprocal is one.

And so one plus one is two. And we could take the result of that and pass it back into exactly the same function with the same left hand side. And we could do that again, take the result of that and pass it into the right hand side. And if we do this a bunch of times, we actually are doing something quite interesting, which is we're creating something called a continued fraction and the continued fraction that we're creating is this one.

So we started with one plus one over one and then we made it one plus one over, then we made it one plus one over that and then we made it one plus one over that and then we made it one plus one over that. Now, if we keep doing that enough times, eventually we're going to get a number called phi.

And phi is also known as the golden ratio. And the golden ratio by the golden ratio appears in nature and art basically everywhere. So basically, you know, it appears in nature when we look at kind of the proportions of things, it appears as the ratios in famous paintings, it appears on the snails shell, it's this number that appears everywhere.

And why are we talking about it? Well, we can calculate it by typing this again and again and again. But that's going to get pretty boring. We could do this, right? So that's going to do one over, sorry, one plus one over one. And then it's going to do one over that.

And that's going to do one over that. It's going to do one over that. It's going to do one over that. And I think we now know that we could replace that with just do f a bunch of times, I don't know, five times. So that's nice because now we can let go a bit further and get, that's actually a pretty good estimate of the golden ratio.

There you go, yep, about one to 1.618. Does that make sense so far? Yes. All right. But how do we know how far to go? Well, basically, we want to keep on applying f until the next time we apply f, the result doesn't change to within floating point error.

If you replace 15 with equals, then the power operator, if you put this on the right-hand side, repeats this function again and again and again. And each time it passes to this function, the previous result and the current result. And it stops if this function returns one. And in math, we call that the fixed point.

The fixed point of a function is the point at which, or a sequence, I guess, is a point at which it stops changing. So there's exactly the same thing, but iterated exactly around a amount of times. I'm not sure how to change the precision that we print out things here.

But if you printed this out in high precision and then passed it to itself again, it wouldn't change. And so if you replace f with its definition, which is this, then you get that. And so the answer to your question of what's all this for is so that we can write short, concise mathematical expressions for things like here's the fixed point of the continued fraction that calculates five.

Is that kind of mind blowing? It is very much so. But it's amazing. It is amazing. And, you know, yeah, there's something delightful, I think, at least to me about, you know, like, yeah, realizing notation can take you this far. And that, like, you know, I would much rather write this than this, you know.

And this doesn't even -- you can't even put this in a computer, because what the hell does dot, dot, dot mean? That means, like, oh, a human ought to be able to guess what goes here. So, yeah, I think it's beautiful. It's a beautiful notation. It's a powerful notation.

And it lets us express complex things fairly simply, like, once we get the idea. And the nice thing is then, like, because we were able to express this quite simply, then we can use that as a thing that we then create another layer of abstraction on top of that.

We use that as an input to something else. You know? So it's because that, you know, if we didn't have this kind of bind -- sorry, this subside composition idea, then this whole thing wouldn't really have worked, you know. We can use these ideas in Python as well. In Python, you can do function composition.

And I think Vastcore might have something, if I'm going to correctly. I can't quite remember what's where. Yeah, compose. So, this is the same as the side in APL. So you can pass it a list of functions. So, for example, here's one function and here's another function and here we are composing the two together.

It goes in the opposite order, I think. So it applies F1 first and then F2 correctly. Sorry, what were you saying? Oh, no, I was going to put you in the compose function. There's a bunch of things like this in Vastcore. So, for example, I really like partial, so I created a better version of partial which doesn't throw away documentation.

This is basically the same as kind of broadcasting. There we are. I created my own bind class. Now, I created all these before I did much with APL because they used another functional programming languages. Possibly or maybe even probably APL did them first, I'm not sure. There's actually a whole academic world, or combinatorial logic, which is all about, eliminate the need for variables, kind of like point-free programming.

And the ideas that are in APL, I wouldn't say they come from combinatorial logic because nobody knows if Ken Iverson had ever seen this stuff before. It's quite possible he reinvented all this from scratch. But it turns out they're all exactly the same. And something that Connor in the ArrayCast podcast mentioned, which has just arrived, is that there's a book of puzzles which actually cover lots of the combinatorial logic ideas.

So I will let you know once I start going through what I think. So there's a lot of ideas of the onion we can peel off. And it turns out that we kind of find ourselves in all these different areas of math and logic and philosophy and whatever. Jeremy, if you check that book out, you have to let me know how it goes.

I checked it out before maybe a couple of years ago. Steven Wolf from Mathematica was talking about combinators. Maybe it was only a year ago, and I happened to get interested and I checked that book out and I kind of worked through some, but I got lost pretty quickly.

I'm sure that you will probably fare much better than me. I don't know about that. I'm not sure. See how it goes. Well, you know, maybe at some point we can start working through it together if people get interested. Yeah, it's interesting listening to an earlier Raycast episode where kind of talked about how he just started with APL and he went to some APL user group meeting or something and said to people, wait, trains?

Are they combinators? And combinators are trains? And everybody at the APL user group meeting, he said, no, this is a new APL invention. It's not some other thing. Because nobody, you know, intellectual worlds don't mash very much. Nobody realized. Particularly the APL intellectual world. I've noticed it's pretty cloistered, to be honest.

So anyway, yeah, they are the same thing. So I'm trying to remember why the hell we were doing all this. This is definitely our most intense study session, right? And please don't be worried if this feels a bit intimidating because it is. And that's fine. And we're meant to be learning new interesting parts of the world and when we find them, we shouldn't expect them to make sense straight away.

So we were doing JWT. That's probably it. That's a good place to finish, isn't it? Yeah, that's fine. I don't think we need any of this. Oh, that's right. We're going to do custom operators. Okay. I think we've got some good background to tackle that. Oh, yeah. Okay. So we've got to here.

I guess before we go, we should at least write down something so that we've made some start here. Oh, yeah. Okay. I remember now. I was going to do squared. That's right. So I wanted my function to be squared. And so to do that, I had to show you that this is min squared.

Okay. So then to calculate the derivative of this function at some point, we're going to use this formula we wrote down. This one here. So it's going to be f of let's create some number to hold up 0.01. So the difference we're going to work on is going to be 0.01.

So it's going to be the fact f of, and so let's calculate this at some point, say two. Now let's do three. Okay. So we're going to calculate it at X equals three. So we're going to go function of X plus D. Okay. Minus function of X. Okay. And then that's going to have to go in parentheses.

Okay. And then we're going to divide that by 0.01. There are better ways to write this in APL, but I want to make it like somewhat familiar. Okay. And for those of you who remember calculus, the actual derivative of X squared is 2x. So the correct answer would have been six.

So which is, you know, we're on the right track. If we set D to a smaller number, we would get a more precise answer. Yeah. So what we're going to try and head to is that we can actually create our own operator that will calculate the derivative of function at a point.

And so maybe that's what we'll try to do next time. Any comments or questions before we go? That's really cool. Awesome. I'm glad to hear. Yeah, that's great again. Thank you. All right. All right. Bye all. Thank you for showing us this. Bye bye. Bye. Thank you. Thank you.