Machine Learning Holidays Live Stream

And will log the artifact.


Machine Learning Holidays Live Stream

Fake data artifact.


Machine Learning Holidays Live Stream

Good like that.


Machine Learning Holidays Live Stream

Now if we execute this.


Machine Learning Holidays Live Stream

What do we do wrong.


Machine Learning Holidays Live Stream

You must call one to be in it.


Machine Learning Holidays Live Stream

But we did.


Machine Learning Holidays Live Stream

Oh but we called finish.


Machine Learning Holidays Live Stream

That's we don't want to do that.


Machine Learning Holidays Live Stream

Want to call in it.


Machine Learning Holidays Live Stream

Luckily we said we could read in it.


Machine Learning Holidays Live Stream

So.


Machine Learning Holidays Live Stream

Oh no we created now again.


Machine Learning Holidays Live Stream

Stupid.


Machine Learning Holidays Live Stream

Yep yep.


Machine Learning Holidays Live Stream

We can probably just do this now.


Machine Learning Holidays Live Stream

On MacBook do you get memory leak problems with Nvidia.


Machine Learning Holidays Live Stream

I don't I don't I don't I don't Nvidia on this MacBook.


Machine Learning Holidays Live Stream

So I have no clue.


Machine Learning Holidays Live Stream

Sorry.


Machine Learning Holidays Live Stream

When I use Nvidia I go to server.


Machine Learning Holidays Live Stream

OK.


Machine Learning Holidays Live Stream

So we've logged an artifact.


Machine Learning Holidays Live Stream

Let's check it out.


Machine Learning Holidays Live Stream

Up here.


Machine Learning Holidays Live Stream

Raw data.


Machine Learning Holidays Live Stream

Fake data.


Machine Learning Holidays Live Stream

Nice.


Machine Learning Holidays Live Stream

Look at that look at that.


Machine Learning Holidays Live Stream

Look at that.


Machine Learning Holidays Live Stream

This is this is very pretty.


Machine Learning Holidays Live Stream

Here is our file.


Machine Learning Holidays Live Stream

Right.


Machine Learning Holidays Live Stream

No preview.


Machine Learning Holidays Live Stream

Don't have metadata yet.


Machine Learning Holidays Live Stream

But it's pretty.


Machine Learning Holidays Live Stream

Very cool.


Machine Learning Holidays Live Stream

So we got a data set.


Machine Learning Holidays Live Stream

And now that the final thing to do.


Machine Learning Holidays Live Stream

Hopefully is that we can just take in the sort of off the shelf squad.


Machine Learning Holidays Live Stream

The off the shelf squad notebook that we found previously.


Machine Learning Holidays Live Stream

And plug this in.


Machine Learning Holidays Live Stream

We should just be able to plug in this data set and then train on this.


Machine Learning Holidays Live Stream

No.


Machine Learning Holidays Live Stream

Like let me try to break out the question answering on squad.


Machine Learning Holidays Live Stream

That's here.


Machine Learning Holidays Live Stream

That's a notebook right.


Machine Learning Holidays Live Stream

So let's do it.


Machine Learning Holidays Live Stream

It's probably going to yell at us.


Machine Learning Holidays Live Stream

No change type.


Machine Learning Holidays Live Stream

We don't have an accelerator.


Machine Learning Holidays Live Stream

So change the other one will terminate all other sessions.


Machine Learning Holidays Live Stream

Close it up.


Machine Learning Holidays Live Stream

We can now do this.


Machine Learning Holidays Live Stream

We don't need to log in here.


Machine Learning Holidays Live Stream

We probably don't need that.


Machine Learning Holidays Live Stream

You do need to install git LFS.


Machine Learning Holidays Live Stream

I don't think so.


Machine Learning Holidays Live Stream

So the only point where it matters is here.


Machine Learning Holidays Live Stream

When we load the data set.


Machine Learning Holidays Live Stream

Before that we need to get our 1DB data set in here.


Machine Learning Holidays Live Stream

That's the entire thing.


Machine Learning Holidays Live Stream

So after this cell which we did we installed the stuff.


Machine Learning Holidays Live Stream

Let's also install 1DB.


Machine Learning Holidays Live Stream

Hey hey.


Machine Learning Holidays Live Stream

Yep.


Machine Learning Holidays Live Stream

Transformers.


Machine Learning Holidays Live Stream

That's fine.


Machine Learning Holidays Live Stream

This is fine.


Machine Learning Holidays Live Stream

All of this is fine.


Machine Learning Holidays Live Stream

So import load data set.


Machine Learning Holidays Live Stream

Now we need to just log in and load up load up the data set.


Machine Learning Holidays Live Stream

Log in.


Machine Learning Holidays Live Stream

And use the artifact.


Machine Learning Holidays Live Stream

Use artifact.


Machine Learning Holidays Live Stream

And we don't want the generator we want fake data.


Machine Learning Holidays Live Stream

Model directory.


Machine Learning Holidays Live Stream

Perfect.


Machine Learning Holidays Live Stream

Now can we do like is this really.


Machine Learning Holidays Live Stream

Does this work.


Machine Learning Holidays Live Stream

No.


Machine Learning Holidays Live Stream

OK.


Machine Learning Holidays Live Stream

It would it would be.


Machine Learning Holidays Live Stream

OK why.


Machine Learning Holidays Live Stream

Why does it name it like this.


Machine Learning Holidays Live Stream

Can we.


Machine Learning Holidays Live Stream

To the specified root directory.


Machine Learning Holidays Live Stream

OK let's just call it fake data.


Machine Learning Holidays Live Stream

How about that.


Machine Learning Holidays Live Stream

And now it's fake data.


Machine Learning Holidays Live Stream

Really.


Machine Learning Holidays Live Stream

Insane.


Machine Learning Holidays Live Stream

Look at that.


Machine Learning Holidays Live Stream

It just works.


Machine Learning Holidays Live Stream

Look there is our data point.


Machine Learning Holidays Live Stream

Crazy.


Machine Learning Holidays Live Stream

I mean we don't have a validation data set yet right.


Machine Learning Holidays Live Stream

But.


Machine Learning Holidays Live Stream

I mean here's some.


Machine Learning Holidays Live Stream

Can't pick more elements in the data set.


Machine Learning Holidays Live Stream

I mean that's that's OK.


Machine Learning Holidays Live Stream

It's going to it's going to show.


Machine Learning Holidays Live Stream

Let's say three.


Machine Learning Holidays Live Stream

Yep.


Machine Learning Holidays Live Stream

Nice.


Machine Learning Holidays Live Stream

Excellent.


Machine Learning Holidays Live Stream

So we don't have a we don't have a we don't have a.


Machine Learning Holidays Live Stream

Validation data set.


Machine Learning Holidays Live Stream

Data sets.


Machine Learning Holidays Live Stream

Let's call it.


Machine Learning Holidays Live Stream

What was it before validation or valid or something like this.


Machine Learning Holidays Live Stream

Train.


Machine Learning Holidays Live Stream

Where do we have validation.


Machine Learning Holidays Live Stream

No.


Machine Learning Holidays Live Stream

We don't have it anywhere.


Machine Learning Holidays Live Stream

All right maybe down here.


Machine Learning Holidays Live Stream

Validation it's called validation.


Machine Learning Holidays Live Stream

OK.


Machine Learning Holidays Live Stream

So what we do is we simply replace the we copy over.


Machine Learning Holidays Live Stream

This is a bit immoral but.


Machine Learning Holidays Live Stream

It's fine.


Machine Learning Holidays Live Stream

And then we just run after.


Machine Learning Holidays Live Stream

That's it.


Machine Learning Holidays Live Stream

That's all we do.


Machine Learning Holidays Live Stream

Yep.


Machine Learning Holidays Live Stream

All right.


Machine Learning Holidays Live Stream

So we were able today.


Machine Learning Holidays Live Stream

We were able to.


Machine Learning Holidays Live Stream

Take the model that we've trained to generate questions new questions.


Machine Learning Holidays Live Stream

We were able to train it.


Machine Learning Holidays Live Stream

We were able to load the data set put it through this model to generate a new fake data set.


Machine Learning Holidays Live Stream

We have logged this model and the data set to our weights and biases account.


Machine Learning Holidays Live Stream

So now what we'll be able to do is.


Machine Learning Holidays Live Stream

What we're doing right here.


Machine Learning Holidays Live Stream

And let me copy that to drive just because.


Machine Learning Holidays Live Stream

Just because.


Machine Learning Holidays Live Stream

I don't want to redo it.


Machine Learning Holidays Live Stream

What we're able to do is we simply plug in the fake data set into the training pipeline for squad.


Machine Learning Holidays Live Stream

And.


Machine Learning Holidays Live Stream

Now we train on the fake data.


Machine Learning Holidays Live Stream

And here.


Machine Learning Holidays Live Stream

OK.


Machine Learning Holidays Live Stream

So.


Machine Learning Holidays Live Stream

We didn't do a hugging face login.


Machine Learning Holidays Live Stream

But.


Machine Learning Holidays Live Stream

You need to pass in a valid token to log in tokenizer.


Machine Learning Holidays Live Stream

Yeah at some point push to hub.


Machine Learning Holidays Live Stream

We don't want to push to hub.


Machine Learning Holidays Live Stream

Right.


Machine Learning Holidays Live Stream

That's just fine.


Machine Learning Holidays Live Stream

We don't need to.


Machine Learning Holidays Live Stream

Yep.


Machine Learning Holidays Live Stream

Got the trainer train it.


Machine Learning Holidays Live Stream

Train train train.


Machine Learning Holidays Live Stream

Save.


Machine Learning Holidays Live Stream

That's it.


Machine Learning Holidays Live Stream

That is.


Machine Learning Holidays Live Stream

And now what we can do is we can just improve the pipeline over and over until we get to something satisfactory like a good data set that will actually improve our squad performance.


Machine Learning Holidays Live Stream

Yeah that's that.


Machine Learning Holidays Live Stream

All in in collapse so far I might do like a hyper parameter sweep or something like this and then I'll make an entire video out of out of it.


Machine Learning Holidays Live Stream

But for now I think this is just fine.


Machine Learning Holidays Live Stream

You see just how much plumbing this is.


Machine Learning Holidays Live Stream

Oh no not push to hub.


Machine Learning Holidays Live Stream

I don't want to push to hub.


Machine Learning Holidays Live Stream

No push to hub.


Machine Learning Holidays Live Stream

Thank you.


Machine Learning Holidays Live Stream

We have an F1 of 9.


Machine Learning Holidays Live Stream

That seems kind of overdone.


Machine Learning Holidays Live Stream

That should not be the case.


Machine Learning Holidays Live Stream

OK.


Machine Learning Holidays Live Stream

So that was it for today.


Machine Learning Holidays Live Stream

As I said it was mostly plumbing.


Machine Learning Holidays Live Stream

Anyway thanks for being here so much.


Machine Learning Holidays Live Stream

And I don't know.


Machine Learning Holidays Live Stream

Tell me how you like streaming.


Machine Learning Holidays Live Stream

It's kind of a bit more boring than regular videos but who knows.


Machine Learning Holidays Live Stream

I have to get back and actually do something productive.


Machine Learning Holidays Live Stream


Longformer: The Long-Document Transformer

Hi there, today we're looking at long former the long document transformer by is Beltagy,


Matthew Peters and Armin Cohen of Alan AI. So the long former is a variant of the transformer,


as you might have guessed, the long former is a transformer that can deal with long documents,


it's aptly named. So I am going to discuss what differentiates the long former from the transformer.


If you don't know what a transformer is, watch the video on attention is all you need. I have


a video on that. And I would also suggest you watch the video on BERT because a lot of the


architecture and training here is based on the on the BERT or variants of BERT. So I'll basically


explain what makes the long former different such that it gets long documents, right, so she can be


applied to long documents. Right. So what is the problem with the original transformer? If you have


a transformer model, and let's say you're doing an NLP tasks, which usually is where transformers are


used, and you want have a paragraph like this one right here, the abstract of the paper,


and maybe you want to predict whether the paper gets accepted at a conference or not.


Now, the classic transformers, they do they have a limit, a very harsh limit on the amount of tokens


that they can look at at the same time. So what you would do in a classic transformer is you couldn't


process this entire thing, let's say, you would divide it in chunks, you'd say, okay, here's my


first chunk from here to here, my second chunk from here to here, and then here to here, and so


on. So you go through the documents, split it up in chunks, process each of the chunks individually,


and then maybe aggregate the predictions. But of course, the the drawback is that the model cannot


make specific connections between let's say, some word here like operation, and somewhere down here


like language, it cannot connect the two on a neural level, at least not in the classic transformer


architectures. Now there are ways to try to alleviate this. But classically, if you split up


your documents into individual samples, they become independent, you cannot do attention, this


attention mechanism cannot operate over across the boundaries of these chunks. So the long former,


the goal is to actually just in just be able to put this entire document here into the model at


the same time. So let's look a bit closer into this in a classic transformer model. What you'll


have is you'll have layers of what are what's called attention mechanism. I'm going to draw


six units here. And the units are actually the input sequence. So in a transformer, other than


like a classic neural network, you don't actually have numbers of units in the in the in the layers.


But you can input as many as long sequences as you want until your memory limit is reached,


basically. So these units, they expose something called keys on the lower layer. And these are


vectors that, you know, point somewhere, and the upper layer will produce what are called queries.


And again, I invite you to look at the attention is all you need video if you want more explanation.


And basically, the keys and queries, they decide where information gets routed to. Right? So the


routing of information is what makes the transformer the transformer. So for example,


this here is probably going to be routed to this here. So the information is routed like this. And


then this here is going to be routed like this, you see the routing is according to the dot product


of the of the keys and queries. Right? So in essence, if you have an input sequence tokens,


and you usually transform in a transformer, you transform the things into same length sequences.


That has to do a lot also with how you want to pre train things and so on. So we're not really


going to change that part. If you have n input sequence and n tokens on the next layer, and


everything can attend to everything, so all the inner products are computed, right, everything is


connected to everything. That means that you're going to end up with an O of n squared memory


requirement, because you have n squared connections. The way to alleviate this is much,


much like you would alleviate this in a classic neural network. So in a classic neural network,


imagine you have this MLP, a multi layer perceptron, or what usually known as a fully


connected layer, right? So here, I have the same thing, but it's not a transformer, it's a classic


neural network fully connected. So I have D units right here, and D units in this first hidden layer,


and I'll have a weight matrix in here, right? And the weight matrix means everything is connected


to everything, right? Everything connects to everything else. Again, my memory requirement


here is D squared. Now, how do we deal with this in a classic neural network, we go to what is


called a convolutional neural network, at least that's one of the one of the methods. So let's


make this again. But let's now make this a convolutional neural network. What we'll have is


we'll have a convolutional kernel. In this case, it's just of length three, right? So we just have


three units here, and they will do the same fully connected pattern, but only over these three units


over these three units right here. And then we slide the kernel over right now it's in this


position, it's still the same three units. But now these three things are connected to these


three things that they're now over right. And so you keep sliding this over across the lower layer


until you're finally at the end here. And now you've reduced the memory consumption from D


squared to just D times. And if this is usually the kernel size is called k to D times k, and k,


you can keep pretty much constant. So that's all of D. Right? And the same goes for the long former.


So in the long former, the idea is that you have a so called sliding window attention,


it's exactly the same as it is in the convolution, except that you don't have these hidden units


here. But these are actually seek the parts of the input sequence. And instead of the weight


matrix here, you have the attention mechanism over the keys, queries and values. But the idea


is similar. So you can basically say this is a sort of a convolution. And we've already had this


in the video about axial attention a bit. Now, of course, this is is your trade off memory for


performance. Because before, right, before, I'm going to draw, let's draw it on top of this fully


connected layer before all the units could attend to all the units, right. And now the unit can only


attend to its immediate neighborhood, right, this green unit here can only attend to itself in the


lower layer, and its immediate neighbors if the kernel size is three. But consider what happens


in the next layer. So in the next layer, I have, for example, this unit right here, this is same


unit right on the next layer, it can tend to these two and itself in the lower layer. But these two


themselves can attend to all of these, right, so that the one on the right can attend to one more.


So in the first layer, this particular unit had information from these three units. But in the


second layer, the same unit has now information across these five, right. And this is kind of this


cone of attention. It gets bigger and bigger as you go through the layers. So you lose the information


to incorporate wide ranges of information in a single layer, but you regain it through depth,


right, the deeper you go, the more a single unit gets information, right, this unit gets information


from this unit over here, through the layers, through the layers, it can't, it can't watch the


unit right here in this layer, that's not possible, but it gets the information through the layers.


Of course, there's still a trade off, like a fully connected layer could just do this in one step.


And then in the next layer, it could do it again, right, it can do much more complex computation.


But if you believe that the most important information is actually in the neighborhoods


of the individual tokens, which is conceivable, in something like the convolutional neural network,


you know that, you know, in an image, usually, you have localized information, right, if there's a cat


here, then the nose and the eyes of the cat, they're pretty close together. So in order to


recognize it's a cat, you will mostly want local information, more and more local information. So


in an image, that makes sense. And in a text, it also makes sense to a degree in that usually


words close together in a sentence, they they are important to for each other. Right, but the power


of the transformer was initially that it could attend to everything in a sentence, right. So,


for example, if you have again, the paragraph here, the power of the transformer, at least that


was said is the fact that this, this piece of text here could make a connection to this piece of text


here. And therefore, the understanding of the entire paragraph could be reliant on this connection


being made, which a local model can't do. But if you go through depth that you might be able to


recover that. So the long former is basically what the convolutional neural network does


for MLPs. It does it for transformers, right. So instead of n by n, giving you n squared, now


you go into into this way where you have. So if you do the same for the transformer, you go to o


n times, let's call it w and w being your window size. In this case, they have a illustration of


this right here. So in a original transformer, this is an attention matrix. So here you have your n


units in sequence and drawn in is which unit can attend to which other unit in a given layer. So


you'll see this particular unit, I here can attend, of course, to itself, right, can attend to


can attend to unit i, but it can also attend to this unit, or to this unit, or to this unit to any


unit, right. And that's what gives you this n squared attention, because any unit can attend


to any unit. Now in this sliding window attention pattern, and this is one of the core components of


the long former, you see that the i ith unit here right here can attend to itself, right, but also


to this, and to this, this, but no more, it can only attend to the ith unit, or to i minus w to


i plus w, right. And this here is a window of size w, this is a list sliding window. So given unit


can only attend to itself or its neighbors in one layer, right. And this is exactly what a


convolution is like, if you see, if you see this pattern, this is a, this is a convolutional pattern.


Now, the second core component is they expand on this idea in that they make, they create these


dilated sliding windows. Now, you see, you already know what a sliding window is. Now they're saying,


well, if you if you have this sliding window, it might take quite a number of layers in order to,


you know, get your attention of the entire sequence incorporated, we saw before it took like


three layers to get halfway through this sequence of, what was it, like six tokens, and it took us


like three layers. And with, so basically, if you go, if you go one layer up, right, one layer up,


you gain one more context window in each direction, right. So it's not, you'd have to go,


very, very deep in order to incorporate the information from these very long sequences. And


the sliding, the dilated sliding window helps this where they say, well, technically,


now, any, any, any sequence here. So again, if we have this sequence, and this is the next


layer, let's just draw. So this unit right here, it will be able to attend this and this, but not


this and not this, but it will also be able to attend this and this, but not this and not this,


sorry, not this. So it'll skip one. So right, these, these attention patterns, they will always


kind of skip, skip one. And the idea is that now you have a vastly greater window of attention,


right, your window size is now way bigger. That means you can incorporate information way faster


across the layers, like global information. But of course, now they're kind of arguing against


each other. In when they do this sliding window, they say, well, we're going to have to do this


well, we pose that mostly local information is important for NLP, right, the words right around


the word are important. And now if they say this here, they basically say, oh, well, it's not so


important that we miss this word right here, which is right next to the word that they are attending


from, which is counter counter to what they just said that probably the most important information


is around the word. They, they do get around this by saying, well, if we have different layers in a


transformer, and in the lower layers, we'll use this sliding window, fully local, and in the higher


layers, we'll use this dilated window. And therefore, in the in the lower layers, we postulate


that local information is actually what's needed to understand local features. And then in the


higher layers, we want more global information, because it will incorporate features from the


from the local informations of the lower layers. All right, I can, I can get the argumentation,


but I feel that's just something they've thrown in there to make it work better after they tried it


out. And the the last idea here in the long former is what they call global attention. And these


global attention is sparse. What it means is that there are some special units here. So in this,


this, this and this unit, and these special units, as you can see from the attention pattern,


these are these can actually attend to everything. So this unit can attend, for example, to this one,


or to this one or to anything. These can attend to anything and any unit can attend to those,


right, any unit can attend to the first unit right here. Right, so these are your special


tokens, your special units, and they have global attention. And the reason for this,


particularly, is that sometimes this is needed. And this is an engineering choice. Right?


The example I can give is let's say you have a question answering task in a question answering


task, what you usually have is a question and a paragraph. And let's say the task here is to


answer yes or no. Is the is the question to the question might be a statement, right? I don't know,


King James was king of England from 1120 to 1140. And then the paragraph will be the Wikipedia


entry for King James. And the the question is yes or no, is the is the question true or not?


Is the statement made true or not? How you would feed this to a BERT model to a transformer


is you concatenate these two things quest query or statement and paragraph. Right, these are the


tokens right here. And then you would separate them using a special token called the separator


token. This is just to inform the model that here is where the first thing stops and the next thing


starts. And then at the beginning, you would put a special token called the CLS token. Now, usually,


what you do is you send these things through your transformer. And now in the last layer, right,


you end up, as we've seen before, because you always transform a sequence into a sequence,


you end up with a sequence again, but you just want a single thing, you just want yes or no.


So you designate, you say this particular unit here that corresponds to the CLS token. That's


what I'm going to throw into a logistic regression. And that's what will give me my yes or no answer.


And that's how you train it, right? So you, you don't want to single out any of these, any of these


as like special. So you simply already include a special token at the beginning that then you take


the classification from, right? It's pretty smart. But also, you say, oh, this is such a special token,


I want that to be able to attend to anything, right? Even though, for example, this unit right


here, it can only attend to its neighbors, right? It has this cone thing. And this unit right here


has this cone thing. This unit right here can always attend to anything at each of the layers,


right? It can attend to anything, and anything can attend to it. So it can get information from


anywhere routed to it in each of the layers. And it can send information to any of the other units.


And this is an engineering choice. So at the beginning, you as an engineer have to say which


one of these tokens are special tokens. And for these tokens, you'll actually then do full


attention, right? It can attend to and from anything. So what are our new memory requirements?


What this will give us is, first of all, we have n tokens, right? And here, w is our window size.


So we have n times w memory. But then we also add the global attention. So plus


the number of special tokens times, right? So if there's a special token, it will have n


times two memory requirement because it can attend from and to in each layer. So, and this entire


thing, sorry, with the plus, this entire thing times the number of layers. So this is your new


attention memory requirements. And as you can see here, n plus n, so this is going to be order of n


instead of much smaller than order of n squared, as we had for the original transformer. Right?


So this, this is what the long former basically does. Now they have written custom CUDA kernels


for doing this, for doing this, this dilated attention and so on, which is pretty cool. And


they have code available for the model. They test this on a number of language tasks. And


they, what I find interesting is it actually they start from the Roberta checkpoint, which Roberta,


where is it said somewhere? Oh, yeah, this this Roberta model right here is a variant of BERT,


right? You can see the name in here is a variant of BERT. And that's their their baseline. And they


start from these checkpoints as far as I understand, and they kind of copy over the position embeddings


and so on. And therefore, they only need to train not very much past Roberta. Now, the reason why


they can copy it over actually, is and this I find very interesting is they use a window size


of 512. So until I read this, I got away from reading the paper, thinking that this this window


size here might be fairly small, right? So this window size, it might be, you know, maybe 10, 20,


30 tokens or something, right? But actually, this window size is 512 in their in their


formulation, which basically means that this is this is as much as one of the classic models could


take as a document, right? So sorry, let's go. So this this here is 512. So this is what a classic


model could take as as a as an entire document. And in the classic model, you simply split up the


document, feed chunks, right and then aggregate over them. Now the long former basically has this.


So right now. For now, I said it has less memory requirements actually has the same memory


requirements as a classic model, but it is also able because of these global attention to kind of


incorporate information from the surrounding things. So that's the the new part. Because if you


think about it, if this w here is 512 512 was the original n. So 512 was the nth part of the


512 was the n zero, whatever the old models had as an end. So right now, if I replace this, and


let's let's, you know, not take care of this, if I replace this, it's actually n times n zero. And


that regresses to the classic model if you plug in n zero here, right? So the new part really is


the fact that you have this, this sliding window, and the global attention is is able to incorporate


information from the special tokens as well, because sliding window that was done before.


So I just don't want to get to you the wrong impression that now we can run transformers on


like very small memory machines, we can't. But we can run them on the same memory machines,


because this is the same length, right? But also feed in longer documents and have some information


of the entire document be propagated to these to these blocks, which before we couldn't before,


we could just simply feed these blocks as one and not have global information. So that's, that's the


new thing, at least they haven't tested it on the smaller things, which is cool from an engineering


point, right? You would want to, because if you want to show that you're better, you would want to


basically be able to be as powerful as the old model, but then be more powerful. And that's what


they do. Alright, so if you want to check out the experiments and the ablations is very interesting,


because they turn on and off a lot of things in their model, and kind of check out where things


come from, what helps, what doesn't. And I'll leave this to you and I'll link it. And with that,


Detecting collisions - Intermediate Python Programming p.20

What's up everybody welcome to part 20 of our intermediate Python series in this video


We're going to be building on the last video which we talked about how we could do operator overloading


Create our own dunder add method which would allow us to let's just use these two


You know self is the object itself and then other blob would be the second parameter


It allows to basically do this self plus other blob and it'll just work


You don't there's no parentheses here. There's no real typical


You know passing of parameters. It just works. It's amazing. It's incredible and


Now that we have that we need to have some sort of handling to know when to use it


So we need to know when two blobs are touching each other


So to do that


Not the easiest operation ever, but that's why this has its own tutorial


So from blob.py we know that every blob object


has its own X and Y which is the center of that blob and


Then we know how big the blob is with size and that size is actually the radius. I'm pretty sure of that blob


so


What we could do is we can calculate the distance between two blobs. What's that distance?


And then if that distance is less than the radiuses of those two blobs added together


Those two blobs must be touching


So that's how we're gonna know if they're touching


So to do that we're gonna use


We're gonna calculate the norm the norm is a way of calculating distance between two points on a plane


Also, you might have heard of you like Euclidean distance or something like that


And if you want to know more about this, I do actually have a tutorial specifically on Euclidean distance and using norm


It's for the machine learning series. You don't actually have to follow along that series


You can just straight into Euclidean distance slash norm if you'd like, but it's not necessary


we're gonna import numpy as NP and then we're gonna come on down and


and above draw environment, we're gonna add a new function. We're gonna say define is


touching and


It's gonna take two parameters b1 b2 for blob 1 and blob 2 and then we're just gonna ask if NP


lin-alg for lin-algebra norm for the norm if that of


basically, and then we're gonna say NP dot array of


Of b1 X b1


Y so these are the locations of that blob and again this this is this could be on an infinite dimensional plane


So you have three dimensions 15 dimensions or in our case just simple two dimensions. So it's Euclidean distance, but


You could go further if you wanted if you had 3d blobs, for example, this would still work


Let's see before I get myself lost you want to take that it's the norm and when you calculate the norm, it'll be that minus


Basically the same thing only b2


Okay, if that is less than


B1 dot size plus b2 dot size


And then we'll put spaces around our plus because we're good little pep-aders if that's the case


You know, we want to return true


otherwise we would


Return false, but it's actually what we can do is simplify this even further


and just do


this


Right, this will return true or false make sure you get rid of the colon at the end though


But this will just return true or false


Now most the time I'm kind of against one line functions


But this function will allow us to just have this much text rather than this much text on


Every time I want to compare two blobs


Now in our case, we're probably gonna call this once anyway


But it makes sense to me so that's how we know if they're touching now


We still have to actually iterate through all of our blobs though and find out if they're touching all right ask this question


So now we're gonna do that. So now we're gonna have a new function and it's gonna be defined handle


collisions and it's going to take whatever the list of blobs is


And


This is this pains me, but we're gonna do it this way reds or blues reds greens equals


Blobless so we're gonna unpack them. This is very much a local function that you would not take to anyone else


Because we're basically hard coding the order of this list, but we're gonna do that so we don't have to know or pass the values


of what color blue never mind anyway


We're just gonna do it this way. I'm trying to think if there there's probably a better way to do this


I'm gonna carry on here, but


There's got to be a third way anyway


new long dicts


Is gonna be an empty list because blob lists is basically we're gonna write a new blob list


Now and in fact


Mmm, actually, I think we're gonna do this. We're gonna use that now we're gonna do is for blue ID


I should have prepared much better for this. I'm sorry blue blob in blues copy


dot items


So why am I we doing this as soon as you see this? We know that?


Blues for example blob list is a list of


These like this is the blob list blues reds and greens. It's a list of dictionaries


So we know blues is a dictionary reds is a dictionary greens is a dictionary


So when you see this for something something in something copy right we know what blues is is a dictionary. This is key value


So as soon as you see someone say copy dot item


There's really only one reason you would ever copy the dictionary as you iterate through it or


Make a copy before you iterate through it


And that is you're gonna modify it because you never want to modify the thing that you're iterating through


Especially because there are gonna be times where you'll get away with it most of the time


So it's dangerous so in this case. We're actually gonna make a copy


rather than going through the real one and then what we're gonna say is


for other blobs in


blues reds greens


What are we gonna do so what we're gonna say is for the other blob ID other


blob in


other blobs dot copy dot items


What are we gonna do well?


if blue blob equals other blob we're gonna pass what's this


Basically we're asking is


This blob touching itself


right


something guys so


Basically we're just saying are we evaluating the exact blob that we're evaluating


Right because we know because this will ring true for every blob every blue blob as we iterate through a copy of the blue blobs


We're gonna have one blob that it's gonna


It's gonna check against itself and if you check against itself


And you passed that it's that blue blob as blob number one and blob number two this would come back as a true


So we just want to make sure that we don't do that so we're passing here because otherwise it would always come back true for


that one blob


else


if is


touching


blue


Blob if they're touching we're just gonna say blue blob plus other


Other well now we could be done, and if we were done at this point. We could say


Now I keep thinking of way better ways to do this, but I'm gonna keep going on


If we were done at this point we actually wouldn't have needed to copy both of these dictionaries


but we


What's gonna happen is as was seen in the last video?


Sometimes when these two blobs come together


One blob gets to be worth zero or less in size


And when that happens that blob should be dead we need to just remove it from the scenario otherwise


We're gonna be iterating through it every time it might actually touch another blob somehow and all kinds of bugs could arise so


What we need to do is we're gonna say if


other


Blob that size is less than or equal to zero at this point after we've done that operation. We're gonna Dell other


blobs


other blob ID and


Then we're gonna say if blue


Blob dot size is less than or equal to zero Dell blues


Blue ID


Perfect so now


At the end of all this we return blues reds greens


Beautiful


Now we'll say that and


Basically all we need to do is


Come down to draw environment


We're gonna still fill that


Green but we're gonna say now we're gonna take this copy


That blue reds and greens paste that blue reds and greens we're gonna say that's equal now to handle


Collisions


we passed that


Blob list that was passed into draw environment and now draw environment is going to have to output this so we're just gonna do


Actually, we'll just copy this one after the update return blues reds and greens


So now you might must be guessing that we have one more thing. We're gonna have to do coming down here. You've got


this


Right blue blobs red blobs green blobs those are coming from up here these original ones


So actually what we need to say now is just this list


Paste


equals that


I'm gonna run it and then we'll run through one more time


What's happening I just wanna make sure I didn't have an error that was what the goal was


less than or equal to


Zero, let's try again


Handle collisions is not defined handle collision collisions


Let's try again


Items


Really where did I where's the other copy there it is items?


There's a lot of errors my my friends all right, let's hopefully we can see one if we don't


Darn this random. Let's add


There we go okay, so that blue blob just consumed actually that's off the screen darn it. Okay. That's okay


It happened for me you guys allowed to take my word for it actually not really let's just


Rerun this one more time and put these blobs up here. Let's see if we can spot. I'm just looking right now


To see if anybody's close nobody is close to doing anything


Maybe I'll just have to restart it. Oh there. We go this blue blob


Just consume that green one you can scroll back if you want to see it, but anyways that happened


Hoping to see maybe these two combined or these two maybe can you see me yes you can


There we go. Oh man everyone just ate that blue blob all right, so


We think everything is working as intended at least visually it appears that things are working come on guys come into contact


Oh this one just ate something


So anyway as you can kind of see things are working it'd be interesting to see what would happen after


You know a few hours or something how these blobs would be handling each other


Probably you would just have a couple like really really big blobs after a while


anyway


That's all for now, so


I did say I would run through one more time, so let's go ahead and do that just in case something was a little confusing


So basically we start with the blobs that we have that were just basically just generated on random


Then we get into this wild true loop


We're passing those blob dictionaries, and they're just being passed to this draw environment


So we pop on up to draw environment which takes blob list which it took


here and we know it's going to return something back to these values


And then this loop just keeps going on so we're let's go see what whatever terms


So we've passed that blob list great. We take that blob list


We do the same things that we've always been doing and then now we are newly returning the new values because we are going to be


It's going to be a changing state from this point because before it this never changed


It was always these blobs that were just going to be going back and returning to these values


So we take them and the first thing that we do is we fill white


That's bothering me a lot of times you're going to actually probably separate


I mean this is not a game a pygame tutorial


I need to redo my pygame tutorial, but a lot of times you're actually going to separate the drawing of the pygame stuff


From the processing stuff to make pygame run as fast as possible


So actually I'm going to do a little bit of a


So we'll pull that apart and eventually you would probably run this in a separate process or something but


That's for another time so


Anyway, we pass this we handle collisions all the handle collisions does basically runs through iterates through all the bobs are they touching if they are


We use our lovely Dunder add method to change what needs to be changed return the new dictionaries


And life is good. So that's what's happening here


So that's all for now


That's our operator overloading and actually applying that operator overloading to our blob world


If you have questions comments concerns leave them below if you have a suggestion


Especially for handle collisions. I have a couple ideas that I I almost applied live on video


And I just thought better of it


But this is almost certain to be improvable


So if someone comes up with a really great way to improve it awesome if you come up with a simpler way to


Calculate the is touching let me know too. That'll be interesting


Coding Adventure with Kaggle and Lux AI

Welcome, everybody, to a Kaggle coding adventure with Lux AI.


Coding Adventure with Kaggle and Lux AI

Lux AI is a currently running Kaggle competition where players face off in a 1v1 challenge


Coding Adventure with Kaggle and Lux AI

to collect resources and end the game with the largest cities.


Coding Adventure with Kaggle and Lux AI

There are lots of details to the game and knowing them will certainly help you to win


Coding Adventure with Kaggle and Lux AI

the competition, but I do find with competitions like this that I've done in the past, it's


Coding Adventure with Kaggle and Lux AI

usually best to just try to make something basic and then build from there, rather than


Coding Adventure with Kaggle and Lux AI

attempting to be the best right out of the gate.


Coding Adventure with Kaggle and Lux AI

There are tiers of workers, tiers of resources, those resources have varying requirements


Coding Adventure with Kaggle and Lux AI

to obtain, there's a day-night cycle where in the night you need enough resources to


Coding Adventure with Kaggle and Lux AI

produce light or those city tiles die, and so on.


Coding Adventure with Kaggle and Lux AI

Coding all of this can represent a decent challenge alone, much less competing against


Coding Adventure with Kaggle and Lux AI

other people and strategies.


Coding Adventure with Kaggle and Lux AI

So I'll be starting off simple, then slowly building out from there.


Coding Adventure with Kaggle and Lux AI

I don't expect anything that I come up with, at least initially, or even within a few hours,


Coding Adventure with Kaggle and Lux AI

to be competitive in any way, but I do enjoy this style of competition.


Coding Adventure with Kaggle and Lux AI

To begin we need to acquire Lux locally.


Coding Adventure with Kaggle and Lux AI

This is going to vary based on your operating system, so check the GitHub page and follow


Coding Adventure with Kaggle and Lux AI

the instructions if you want to follow along.


Coding Adventure with Kaggle and Lux AI

Essentially it is just merely installing Node.js, then installing the challenge.


Coding Adventure with Kaggle and Lux AI

From there we can check out the kits directory, and then in here we can see there's some examples


Coding Adventure with Kaggle and Lux AI

in various programming languages for coding your AI.


Coding Adventure with Kaggle and Lux AI

I'm interested in Python, so I'll click that, and then in here we have this simple directory.


Coding Adventure with Kaggle and Lux AI

If we check out the simple directory, this is a complete submission-ready example that


Coding Adventure with Kaggle and Lux AI

you could zip up and upload to Kaggle for competition.


Coding Adventure with Kaggle and Lux AI

That said, the bot here is way too primitive to actually compete with, you'd just embarrass


Coding Adventure with Kaggle and Lux AI

yourself, so we'll start by running things locally and then tinkering from there.


Coding Adventure with Kaggle and Lux AI

So for me, I've copied the simple directory, inside here we have of main interest to us,


Coding Adventure with Kaggle and Lux AI

we've got the Lux package in Python, but then mainly agent.py and main.py.


Coding Adventure with Kaggle and Lux AI

So I'll pull those over here, we have main.py, essentially just runs your agent, you don't


Coding Adventure with Kaggle and Lux AI

really need to mess with this at all, and you really shouldn't, other than maybe if


Coding Adventure with Kaggle and Lux AI

you change agent here, or this py file to something else and so on.


Coding Adventure with Kaggle and Lux AI

So then we check out agent.py, this is what contains your actual logic and strategy and


Coding Adventure with Kaggle and Lux AI

all that, so we'll be digging more into this very soon.


Coding Adventure with Kaggle and Lux AI

But first, let's go ahead and run this and see what things look like.


Coding Adventure with Kaggle and Lux AI

So now to actually run this, we're going to go back one directory, since we're in the


Coding Adventure with Kaggle and Lux AI

simple AI directory, and in theory this is our actual agent, so I'll go back one directory


Coding Adventure with Kaggle and Lux AI

and just run a game of simple versus simple.


Coding Adventure with Kaggle and Lux AI

So I'll open up a terminal here, drag that over, make that slightly bigger, and then


Coding Adventure with Kaggle and Lux AI

run the following command, and in this case it's npx run luxai, and then this is AI number


Coding Adventure with Kaggle and Lux AI

one that's going to compete versus AI number two.


Coding Adventure with Kaggle and Lux AI

In this case it just happens to be the exact same AI.


Coding Adventure with Kaggle and Lux AI

And then this is the game size, so we're going to do a 24 by 24 game map, and we'll go ahead


Coding Adventure with Kaggle and Lux AI

and run that, and we should see that we wind up with a replays directory, so if we double


Coding Adventure with Kaggle and Lux AI

click on that we see there's some sort of JSON file that is the replay, and if we want


Coding Adventure with Kaggle and Lux AI

to look at this, we can either come over to the lux viewer and view this like offline,


Coding Adventure with Kaggle and Lux AI

so if we want to just use it locally we could do that, so we can essentially just host the


Coding Adventure with Kaggle and Lux AI

viewer locally, or we can use the following, it's at 2021 viz.lux-ai.org, and we can upload


Coding Adventure with Kaggle and Lux AI

the replay.


Coding Adventure with Kaggle and Lux AI

So we can run that real quick, and in general, so over here we have the stats and stuff like


Coding Adventure with Kaggle and Lux AI

that, and then down here you can press play, and you can speed it up by pressing play,


Coding Adventure with Kaggle and Lux AI

but it's really annoying to do that every single time, and as time goes on you'll probably


Coding Adventure with Kaggle and Lux AI

run many iterations, and this is super slow, even this is slower than I would want it to


Coding Adventure with Kaggle and Lux AI

be, so I've personally found that when I look at replays I just click this and just kind


Coding Adventure with Kaggle and Lux AI

of drag it around, and if I see anything that looks interesting I'll kind of go back and


Coding Adventure with Kaggle and Lux AI

watch it maybe a little slower, but in general I just kind of drag this around.


Coding Adventure with Kaggle and Lux AI

And then you can look over here, you can see city fuel, you can even click on like specific


Coding Adventure with Kaggle and Lux AI

units, so you can click on like your worker, you can click on your city tiles, you can


Coding Adventure with Kaggle and Lux AI

click on these resource tiles, and so on.


Coding Adventure with Kaggle and Lux AI

The objective of this competition is to simply end the game with more city tiles than the


Coding Adventure with Kaggle and Lux AI

opponent.


Coding Adventure with Kaggle and Lux AI

That's all there is to it.


Coding Adventure with Kaggle and Lux AI

Of the slightly over 400 teams currently participating in the competition, the bottom 125 or so at


Coding Adventure with Kaggle and Lux AI

the time of my recording are all just keeping a mere single city tile like the simplebot


Coding Adventure with Kaggle and Lux AI

does here.


Coding Adventure with Kaggle and Lux AI

So the tiny change of merely building one more city tile would put us above about 25%


Coding Adventure with Kaggle and Lux AI

of the competition.


Coding Adventure with Kaggle and Lux AI

So let's go ahead and do that.


Coding Adventure with Kaggle and Lux AI

Also while we're doing that, one issue that creeps up a lot, especially with coding a


Coding Adventure with Kaggle and Lux AI

strategy bot like this, is that code can explode and get really messy really fast because you're


Coding Adventure with Kaggle and Lux AI

going to try a strategy, try another strategy, you're going to have a bunch of logic that


Coding Adventure with Kaggle and Lux AI

corresponds maybe just to one strategy, and so on, and things just get really messy.


Coding Adventure with Kaggle and Lux AI

Now I'm no pillar of organization, but I'm going to do some housekeeping with the bot,


Coding Adventure with Kaggle and Lux AI

even just the simplebot right here, as I run through the existing code and we make some


Coding Adventure with Kaggle and Lux AI

changes to build one more city.


Coding Adventure with Kaggle and Lux AI

To start, the first 32 lines are probably good enough to keep as they are.


Coding Adventure with Kaggle and Lux AI

After this though, we come across the first block of code that could easily be a function,


Coding Adventure with Kaggle and Lux AI

which is the creation of this list that contains the resource tile objects.


Coding Adventure with Kaggle and Lux AI

We're not going to save total line count in the file here by turning this into a function,


Coding Adventure with Kaggle and Lux AI

but instead we're looking to abstract away our code logic from the strategy logic as


Coding Adventure with Kaggle and Lux AI

much as possible so as to make the strategy logic as clear as possible.


Coding Adventure with Kaggle and Lux AI

So for this, we'll define resource tiles as getResourceTiles and pass the game state,


Coding Adventure with Kaggle and Lux AI

width, and height.


Coding Adventure with Kaggle and Lux AI

This function doesn't exist yet, so I'll copy the code block that defined these in the first


Coding Adventure with Kaggle and Lux AI

place, go and make a new function called getResourceTiles, and then paste in this code.


Coding Adventure with Kaggle and Lux AI

At the end, I'll return the resource tiles, and there's our first bit of cleaned up logic.


Coding Adventure with Kaggle and Lux AI

The next chunk of code that I'll address here is the code that simply finds a resource.


Coding Adventure with Kaggle and Lux AI

I'll use a similar method here to just say the closest resource tile is equal to the


Coding Adventure with Kaggle and Lux AI

return of a function called getCloseResource and pass the unit resource tiles and player


Coding Adventure with Kaggle and Lux AI

object.


Coding Adventure with Kaggle and Lux AI

Then I cut the code, go to the top, define a new function called getCloseResource, paste


Coding Adventure with Kaggle and Lux AI

in the code, and add the parameters.


Coding Adventure with Kaggle and Lux AI

Continuing along in the code, same thing here where there's a block of code that simply


Coding Adventure with Kaggle and Lux AI

finds the closest city tile to return to with our resources.


Coding Adventure with Kaggle and Lux AI

I'll establish the variable to be equal to the return of a function called getCloseCity,


Coding Adventure with Kaggle and Lux AI

paste in the code, and realize that I never actually returned the closest resource tile


Coding Adventure with Kaggle and Lux AI

in the above and previous function.


Coding Adventure with Kaggle and Lux AI

Add that in, and then of course also a return closest city tile for that getCloseCity function.


Coding Adventure with Kaggle and Lux AI

Now we've considerably simplified our actual agent function.


Coding Adventure with Kaggle and Lux AI

Again, we didn't remove code in the total file, we actually added more lines here, but


Coding Adventure with Kaggle and Lux AI

the idea is to keep the agent function, our logic, our strategy as simple and easy to


Coding Adventure with Kaggle and Lux AI

understand as possible for ourselves, especially as we begin to add much, much more logic.


Coding Adventure with Kaggle and Lux AI

The next thing to do is run the code, make sure there are no errors, and check the replay


Coding Adventure with Kaggle and Lux AI

to make sure everything is acting as expected.


Coding Adventure with Kaggle and Lux AI

Looks good to me.


Coding Adventure with Kaggle and Lux AI

Now to beat those hundred or so competitors, we really just need to build one more city


Coding Adventure with Kaggle and Lux AI

tile.


Coding Adventure with Kaggle and Lux AI

Using the documentation, to do this requires a unit with 100 units of resources, and then


Coding Adventure with Kaggle and Lux AI

we just call unit.buildCity.


Coding Adventure with Kaggle and Lux AI

We really just want to do this once, so just build one more city tile for now, and to do


Coding Adventure with Kaggle and Lux AI

this we need to know how many do we actually have.


Coding Adventure with Kaggle and Lux AI

Scrolling up in the documentation, we can see that we can find this out possibly through


Coding Adventure with Kaggle and Lux AI

player.cities, but it's kind of unclear to me what this is.


Coding Adventure with Kaggle and Lux AI

To help us here, I'll be bringing in logging.


Coding Adventure with Kaggle and Lux AI

Due to how the game itself is using terminal outputs to actually play the game, we can't


Coding Adventure with Kaggle and Lux AI

really use prints here, plus the game moves so fast that an actual log file I think would


Coding Adventure with Kaggle and Lux AI

be more useful anyway.


Coding Adventure with Kaggle and Lux AI

I'll specify my log file and start things off by just simply logging this player.cities


Coding Adventure with Kaggle and Lux AI

value.


Coding Adventure with Kaggle and Lux AI

Then I'll run the code, check the log file, and it appears what we have here is indeed


Coding Adventure with Kaggle and Lux AI

some sort of ID for the key and then the city object as the value.


Coding Adventure with Kaggle and Lux AI

One thing to note immediately here is that we have both cities and city tiles.


Coding Adventure with Kaggle and Lux AI

Adjacent city tiles are part of the same city, so cities are comprised of city tiles.


Coding Adventure with Kaggle and Lux AI

Coming back to my code, I'll specify then that the cities are now player.cities.values


Coding Adventure with Kaggle and Lux AI

and the city tiles, which we want two of for now, will be an empty list to start and then


Coding Adventure with Kaggle and Lux AI

I'll iterate through all cities and then all city tiles in those cities city tiles and


Coding Adventure with Kaggle and Lux AI

add those to my city tiles list.


Coding Adventure with Kaggle and Lux AI

Finally, I'll log the cities and city tiles just to make sure my logic and things are


Coding Adventure with Kaggle and Lux AI

working how I expect.


Coding Adventure with Kaggle and Lux AI

Running this, I'm initially confused because log files are appended and I'm not seeing


Coding Adventure with Kaggle and Lux AI

the new information because I'm scrolled up to the top.


Coding Adventure with Kaggle and Lux AI

Thinking it'll help, I add some text to differentiate these and upon seeing it doesn't help, I realize


Coding Adventure with Kaggle and Lux AI

my mistake and I find that I find what I was expecting by simply scrolling down.


Coding Adventure with Kaggle and Lux AI

Clearing the log file and running again, much better.


Coding Adventure with Kaggle and Lux AI

That's what I wanted and everything is indeed looking as I expected.


Coding Adventure with Kaggle and Lux AI

One city tile and one city.


Coding Adventure with Kaggle and Lux AI

And now it's time to crush my opponents.


Coding Adventure with Kaggle and Lux AI

Before iterating over units, I'll set a new variable called build city to false and then


Coding Adventure with Kaggle and Lux AI

I'll check to see if the list of city tiles is less than two.


Coding Adventure with Kaggle and Lux AI

If it is, then I want to build another city tile.


Coding Adventure with Kaggle and Lux AI

So then we begin iterating through the workers.


Coding Adventure with Kaggle and Lux AI

We know we need 100 units of resources to build a city and each worker unit holds 100


Coding Adventure with Kaggle and Lux AI

resources.


Coding Adventure with Kaggle and Lux AI

So if the unit's cargo space left is more than zero, we need to just keep gathering.


Coding Adventure with Kaggle and Lux AI

Else, if build city is true, then we'll log that we do wish to build a city, but where?


Coding Adventure with Kaggle and Lux AI

Deciding where to build is all by itself another part of the strategy.


Coding Adventure with Kaggle and Lux AI

So for now, let's just try to build adjacent so that it stays a part of one single city.


Coding Adventure with Kaggle and Lux AI

I'll set a global variable called build location at the top of the script to come down to the


Coding Adventure with Kaggle and Lux AI

agent function and add it as a global and then begin to build the logic.


Coding Adventure with Kaggle and Lux AI

What we really want to do here is find an empty tile next to our current city tile.


Coding Adventure with Kaggle and Lux AI

So first I grab that close city tile, then build a list of directional changes.


Coding Adventure with Kaggle and Lux AI

We're working in xy coordinates, so adding one to the x position of the city tile checks


Coding Adventure with Kaggle and Lux AI

the tile to the east of that tile and one to the y would check the tile to the south


Coding Adventure with Kaggle and Lux AI

and so on for the negatives.


Coding Adventure with Kaggle and Lux AI

There may be times where these spots are all filled.


Coding Adventure with Kaggle and Lux AI

This is something we'll handle for another time.


Coding Adventure with Kaggle and Lux AI

So we'll iterate through those directions and check for emptiness, allowing for errors


Coding Adventure with Kaggle and Lux AI

since sometimes your city tile might actually be on the edge of the map, so we'll check


Coding Adventure with Kaggle and Lux AI

for a tile that doesn't even exist, but we'd also like to see any other errors that we


Coding Adventure with Kaggle and Lux AI

didn't even think about too.


Coding Adventure with Kaggle and Lux AI

For each tile, we'll see if it has any resources, any road, or any existing city tile already


Coding Adventure with Kaggle and Lux AI

on it.


Coding Adventure with Kaggle and Lux AI

If it doesn't, great, that's our build location and let's break out of the loop.


Coding Adventure with Kaggle and Lux AI

Then we'll check if the unit is already at the build location.


Coding Adventure with Kaggle and Lux AI

If it is, build the city.


Coding Adventure with Kaggle and Lux AI

Note that you need to define the action, then append it to the list of actions.


Coding Adventure with Kaggle and Lux AI

This full list of actions is what the game reads and acts on.


Coding Adventure with Kaggle and Lux AI

After building, we'll set build city to false and build location back to none and continue.


Coding Adventure with Kaggle and Lux AI

If we don't need to find the build location and we're not yet at the build location, but


Coding Adventure with Kaggle and Lux AI

we have a build location, then we wish to navigate to that location.


Coding Adventure with Kaggle and Lux AI

We'll append that action with a unit.move to the direction of the build location.


Coding Adventure with Kaggle and Lux AI

Unit moves are actually in north, south, east, west, or center and basically denoted as N,


Coding Adventure with Kaggle and Lux AI

S, E, W, and C, not in coordinates.


Coding Adventure with Kaggle and Lux AI

Keep that in mind.


Coding Adventure with Kaggle and Lux AI

When you do a unit.move, it's actually in N, S, E, W, or C.


Coding Adventure with Kaggle and Lux AI

Eventually, we'll need to probably create our own pathing function rather than using


Coding Adventure with Kaggle and Lux AI

this one since it doesn't handle for things like avoiding unit collisions and other problems.


Coding Adventure with Kaggle and Lux AI

So we'll need to get more into these movements later probably.


Coding Adventure with Kaggle and Lux AI

Now I want to run everything and make sure it works and there's some error with logging.


Coding Adventure with Kaggle and Lux AI

This error makes no sense to me.


Coding Adventure with Kaggle and Lux AI

It still makes no sense to me.


Coding Adventure with Kaggle and Lux AI

It seems to be logging related despite using logging earlier just fine.


Coding Adventure with Kaggle and Lux AI

It appears to be caused somehow by it, but I just I have no idea why.


Coding Adventure with Kaggle and Lux AI

But commenting out the logs and running appears to work as expected.


Coding Adventure with Kaggle and Lux AI

So rather than using logging, I'll just try out a typical file.


Coding Adventure with Kaggle and Lux AI

I'll define its name and then go ahead and open it with a write intention purely to clear


Coding Adventure with Kaggle and Lux AI

the file every time.


Coding Adventure with Kaggle and Lux AI

Next, everywhere I was logging and still want to log some info, I'll instead add an open


Coding Adventure with Kaggle and Lux AI

file to append option and write what I want.


Coding Adventure with Kaggle and Lux AI

I could also encase the whole agent function in this open so I don't need to keep calling


Coding Adventure with Kaggle and Lux AI

the with open.


Coding Adventure with Kaggle and Lux AI

But I don't intend to be doing a ton of logging.


Coding Adventure with Kaggle and Lux AI

I'll probably regret this later.


Coding Adventure with Kaggle and Lux AI

Running things again, I get an error.


Coding Adventure with Kaggle and Lux AI

I forget the quotes in step.


Coding Adventure with Kaggle and Lux AI

I do a find and replace run again.


Coding Adventure with Kaggle and Lux AI

And of course, my replace causes a new error.


Coding Adventure with Kaggle and Lux AI

We're using double quotes inside of double quotes.


Coding Adventure with Kaggle and Lux AI

Run again and I see I'm not getting a second city tile.


Coding Adventure with Kaggle and Lux AI

Checking the logs, I see we're indeed hitting that error exception and logging it.


Coding Adventure with Kaggle and Lux AI

After some poking around, I realized that after all of this in the condition where we


Coding Adventure with Kaggle and Lux AI

have the build location that we wish to navigate to, after we hit that else, we're going to


Coding Adventure with Kaggle and Lux AI

actually continue this block and hit that final if statement, which then instead instructs


Coding Adventure with Kaggle and Lux AI

the exact same unit to go find the closest city tile.


Coding Adventure with Kaggle and Lux AI

To remedy this, I'll add a continue after the units move to the build location.


Coding Adventure with Kaggle and Lux AI

Trying again, things run, no errors, but I don't see a second city tile with either agent


Coding Adventure with Kaggle and Lux AI

in the replay.


Coding Adventure with Kaggle and Lux AI

Looking at the log file, I see we wanted to build a second city.


Coding Adventure with Kaggle and Lux AI

We really did.


Coding Adventure with Kaggle and Lux AI

But then we don't.


Coding Adventure with Kaggle and Lux AI

What's up?


Coding Adventure with Kaggle and Lux AI

Doing it again and checking the replay, we can see things actually worked this time for


Coding Adventure with Kaggle and Lux AI

both agents.


Coding Adventure with Kaggle and Lux AI

So what actually happened here?


Coding Adventure with Kaggle and Lux AI

Well, like I said, the default navigation is kind of silly.


Coding Adventure with Kaggle and Lux AI

Anytime an agent runs over a city tile, all of the resources are deposited there.


Coding Adventure with Kaggle and Lux AI

So what was happening was our agent tried to move to a tile that was across a city tile,


Coding Adventure with Kaggle and Lux AI

deposited everything, then had cargo space.


Coding Adventure with Kaggle and Lux AI

So it went back to collect and repeated this over and over.


Coding Adventure with Kaggle and Lux AI

Now actually, if we want to build adjacent to a city tile, I believe we can do that with


Coding Adventure with Kaggle and Lux AI

zero resources on the worker.


Coding Adventure with Kaggle and Lux AI

I think the worker actually subtracts or the resources get subtracted from the adjacent


Coding Adventure with Kaggle and Lux AI

city tile.


Coding Adventure with Kaggle and Lux AI

But that's not the logic I was intending here.


Coding Adventure with Kaggle and Lux AI

So we really want something to fix this.


Coding Adventure with Kaggle and Lux AI

And at this point, I'm starting to realize I might not be very good at this competition.


Coding Adventure with Kaggle and Lux AI

And how much time will be required to actually make a decent bot?


Coding Adventure with Kaggle and Lux AI

So the first thing I've done is copied the simple directory and called it intermediate.


Coding Adventure with Kaggle and Lux AI

And in here, this is the agent that I'm going to continue working on.


Coding Adventure with Kaggle and Lux AI

To begin expanding the city, we can just build another city tile, but then our upkeep is


Coding Adventure with Kaggle and Lux AI

higher.


Coding Adventure with Kaggle and Lux AI

So we want more workers too.


Coding Adventure with Kaggle and Lux AI

I think I want to have one worker per city tile, but we cannot build another worker first,


Coding Adventure with Kaggle and Lux AI

since in order to build workers, you need to have less workers than city tiles.


Coding Adventure with Kaggle and Lux AI

To start building city tiles, I think we also want to start tracking workers, the resources


Coding Adventure with Kaggle and Lux AI

those workers are using, and the city tiles that those workers deposit to.


Coding Adventure with Kaggle and Lux AI

Initially, if we're just building one big city, we don't need to associate workers to


Coding Adventure with Kaggle and Lux AI

that city tile.


Coding Adventure with Kaggle and Lux AI

But if we want to start expanding, we need to at least probably associate workers to


Coding Adventure with Kaggle and Lux AI

the overall city that they're depositing to.


Coding Adventure with Kaggle and Lux AI

But for now, I'll just map to this specific city tile, along with mapping workers to resources


Coding Adventure with Kaggle and Lux AI

using a couple of global dictionaries.


Coding Adventure with Kaggle and Lux AI

Next, when looking for a resource, we'll skip a resource if it's already a resource for


Coding Adventure with Kaggle and Lux AI

a different worker.


Coding Adventure with Kaggle and Lux AI

Later, we might elect to allow for two or three or any number of workers to share a


Coding Adventure with Kaggle and Lux AI

resource tile, but this is something worthy of trial and error later.


Coding Adventure with Kaggle and Lux AI

For now, I'll just assign one per.


Coding Adventure with Kaggle and Lux AI

In the agent function, I'll add the two global dictionaries.


Coding Adventure with Kaggle and Lux AI

I'll add a list that contains all workers that I can reference and iterate through.


Coding Adventure with Kaggle and Lux AI

And then I am checking to see if those workers exist in the unit to city dictionary.


Coding Adventure with Kaggle and Lux AI

If not, I'll just assign them to a closed city, and then I do the exact same thing for


Coding Adventure with Kaggle and Lux AI

workers and resources.


Coding Adventure with Kaggle and Lux AI

Then I already know that I'm going to want more than two city tiles, so I'm going to


Coding Adventure with Kaggle and Lux AI

change the build city boolean criteria to instead be a measure of the ratio of workers


Coding Adventure with Kaggle and Lux AI

to city tiles.


Coding Adventure with Kaggle and Lux AI

Again, the specific number that I'm going to go with here is something that could easily


Coding Adventure with Kaggle and Lux AI

be tuned.


Coding Adventure with Kaggle and Lux AI

Fuel burn rates per city tile varies based on the city's size.


Coding Adventure with Kaggle and Lux AI

So it's possible that one worker unit could theoretically serve two or more city tiles,


Coding Adventure with Kaggle and Lux AI

or maybe 20 workers could serve 30 city tiles, and so on.


Coding Adventure with Kaggle and Lux AI

Or maybe it's better to have a worker per city tile.


Coding Adventure with Kaggle and Lux AI

Next, when iterating over units and filling cargo, we'll grab the resource tile to head


Coding Adventure with Kaggle and Lux AI

for from the unit resource dictionary now instead.


Coding Adventure with Kaggle and Lux AI

It's probably wise to confirm this cell has a resource still, and if not, get a new one


Coding Adventure with Kaggle and Lux AI

and assign that unit to the new one.


Coding Adventure with Kaggle and Lux AI

If we do want to build, I still am okay with the logic for the build location, so I'm ready


Coding Adventure with Kaggle and Lux AI

to go ahead and extract this out into its own function.


Coding Adventure with Kaggle and Lux AI

So now the build location will be the return of a new function called find empty tile near.


Coding Adventure with Kaggle and Lux AI

I'm also going to go ahead and add in checking for the diagonal tiles for response to.


Coding Adventure with Kaggle and Lux AI

This is mostly a copy and paste and could definitely be made quite a bit better with,


Coding Adventure with Kaggle and Lux AI

I don't know, something like a for loop over the two types of directions or something like


Coding Adventure with Kaggle and Lux AI

that, but this will do for now.


Coding Adventure with Kaggle and Lux AI

I'm also going to change this LIF to an IF because we might as well issue a unit's navigation


Coding Adventure with Kaggle and Lux AI

even if we need to define the build location first, and then I'll change the final IF to


Coding Adventure with Kaggle and Lux AI

an LIF.


Coding Adventure with Kaggle and Lux AI

The continues, I think, should stop us from landing here anyways, but I'm just throwing


Coding Adventure with Kaggle and Lux AI

it in just in case.


Coding Adventure with Kaggle and Lux AI

Then rather than returning to the closest city, we'll return to this unit's specified


Coding Adventure with Kaggle and Lux AI

city tile.


Coding Adventure with Kaggle and Lux AI

If the unit doesn't have one, we'll get a new one.


Coding Adventure with Kaggle and Lux AI

Running things, I see that we just need to move our resource tiles definition a bit up


Coding Adventure with Kaggle and Lux AI

since we're referencing it earlier in some of the new logic.


Coding Adventure with Kaggle and Lux AI

Another run, another issue, a typo, fix that, run again.


Coding Adventure with Kaggle and Lux AI

We do get another city, so that is indeed what we expected.


Coding Adventure with Kaggle and Lux AI

Checking the logs, however, I see that we're constantly finding a worker without a city


Coding Adventure with Kaggle and Lux AI

or a resource, and that's not expected.


Coding Adventure with Kaggle and Lux AI

I also note here that each unit object here is a new or different object in memory, so


Coding Adventure with Kaggle and Lux AI

that's kind of a problem because we're using the object itself as the key.


Coding Adventure with Kaggle and Lux AI

Checking the docs, though, I see that units also have unique IDs.


Coding Adventure with Kaggle and Lux AI

Maybe we can use that value instead.


Coding Adventure with Kaggle and Lux AI

So I'm going to go ahead and change all the references of the worker to instead be worker.id,


Coding Adventure with Kaggle and Lux AI

essentially, and running again, things look much better.


Coding Adventure with Kaggle and Lux AI

The next order of business for me is to address the running over of city tiles and depositing


Coding Adventure with Kaggle and Lux AI

all of our cargo when we do it.


Coding Adventure with Kaggle and Lux AI

If we want to avoid doing this, we need to navigate around the city.


Coding Adventure with Kaggle and Lux AI

There may be times where we also want to navigate around an enemy city or something like this,


Coding Adventure with Kaggle and Lux AI

and this logic could be used for that.


Coding Adventure with Kaggle and Lux AI

Under the build city if statement, we want to address the final else where we actually


Coding Adventure with Kaggle and Lux AI

navigate to that build location.


Coding Adventure with Kaggle and Lux AI

The logic here is first to determine what is the x and y coordinate difference between


Coding Adventure with Kaggle and Lux AI

our current position and the one that we want to be at.


Coding Adventure with Kaggle and Lux AI

Then take note that movement commands for units are not actually coordinates, but instead


Coding Adventure with Kaggle and Lux AI

are n, s, e, w, or c for north, south, east, west, or center.


Coding Adventure with Kaggle and Lux AI

So I'll make note of what coordinates would translate to what directions.


Coding Adventure with Kaggle and Lux AI

Then whichever absolute value is greater in the coordinate differences, we'll just assume


Coding Adventure with Kaggle and Lux AI

that's the direction that we want to start off going.


Coding Adventure with Kaggle and Lux AI

So then I'll check the tile one step in that direction to see if it is empty.


Coding Adventure with Kaggle and Lux AI

If it is, great, we'll go that way.


Coding Adventure with Kaggle and Lux AI

If not, we'll go in the other direction that we need to go instead.


Coding Adventure with Kaggle and Lux AI

This logic could probably be improved pretty significantly, and a small wall of city tiles


Coding Adventure with Kaggle and Lux AI

would be enough, I think, to actually thwart this completely.


Coding Adventure with Kaggle and Lux AI

But running this, I forgot to import numpy, so I'll do that, run again, and I see that


Coding Adventure with Kaggle and Lux AI

we still tied, but that's because both teams built a second city.


Coding Adventure with Kaggle and Lux AI

There wasn't really a city tile in the way.


Coding Adventure with Kaggle and Lux AI

Again, I see an instance where the intermediate bot won, so let's check that replay.


Coding Adventure with Kaggle and Lux AI

I see indeed that my unit went around the other city tile.


Coding Adventure with Kaggle and Lux AI

Finally, just to make things a bit more clear in the logs, I'll add a log when the city


Coding Adventure with Kaggle and Lux AI

is actually built.


Coding Adventure with Kaggle and Lux AI

Running one more time and checking the logs, things are looking pretty good here.


Coding Adventure with Kaggle and Lux AI

Now that I've got the logic for workers to build more city tiles, I need the logic to


Coding Adventure with Kaggle and Lux AI

build more workers.


Coding Adventure with Kaggle and Lux AI

To do this, I'll continue building workers until I have as many workers as city tiles.


Coding Adventure with Kaggle and Lux AI

I'll iterate over all city tiles, and if that city tile can build a worker, I'll go ahead


Coding Adventure with Kaggle and Lux AI

and do that.


Coding Adventure with Kaggle and Lux AI

While I'm here, since we need to do research to eventually get coal and uranium, I'll also


Coding Adventure with Kaggle and Lux AI

do research if I cannot build another worker.


Coding Adventure with Kaggle and Lux AI

Running this, I can see that I've hit a div by zero error, which can really only mean


Coding Adventure with Kaggle and Lux AI

that I had zero city tiles.


Coding Adventure with Kaggle and Lux AI

Checking the replay, I can see that I indeed expanded too quickly and the city did not


Coding Adventure with Kaggle and Lux AI

survive the night.


Coding Adventure with Kaggle and Lux AI

There are kind of two issues here.


Coding Adventure with Kaggle and Lux AI

One, obviously, ideally, this just wouldn't happen, and two, we could have easily just


Coding Adventure with Kaggle and Lux AI

rebuilt new city tiles during this game if we wanted to and we wouldn't have necessarily


Coding Adventure with Kaggle and Lux AI

ended with zero city tiles, and we would have beat the other bot.


Coding Adventure with Kaggle and Lux AI

So first we'll determine if we really should build more city tiles.


Coding Adventure with Kaggle and Lux AI

To do this, I'll figure out which city we're going to build onto and then get the fuel


Coding Adventure with Kaggle and Lux AI

for that city.


Coding Adventure with Kaggle and Lux AI

Then we can divide the total city fuel by the size of that city, giving us an about


Coding Adventure with Kaggle and Lux AI

average number of fuel per city tile.


Coding Adventure with Kaggle and Lux AI

And really, we're just looking for a way to have enough fuel per city tile to make it


Coding Adventure with Kaggle and Lux AI

through the night.


Coding Adventure with Kaggle and Lux AI

So now I can pick some number, like 300 units of fuel per city, and if we have more than


Coding Adventure with Kaggle and Lux AI

that, we can allow ourselves to build another city tile.


Coding Adventure with Kaggle and Lux AI

I'll log some of the information and then add an if statement to handle for if we have


Coding Adventure with Kaggle and Lux AI

the fuel to build or not.


Coding Adventure with Kaggle and Lux AI

If we don't have the fuel, I'll just sloppily copy and paste the code to go find a city


Coding Adventure with Kaggle and Lux AI

to dump the resources into.


Coding Adventure with Kaggle and Lux AI

Running things, I see I've got an error about the resource assignment variable.


Coding Adventure with Kaggle and Lux AI

It looks like somehow in a copy and paste frenzy, I left in old names or something.


Coding Adventure with Kaggle and Lux AI

Fixing that and things are getting closer to what I want, though now units are running


Coding Adventure with Kaggle and Lux AI

into each other a lot and getting stuck.


Coding Adventure with Kaggle and Lux AI

Running again, I get another div by zero issue when all of my city tiles have died.


Coding Adventure with Kaggle and Lux AI

So what I'll do here is encase the build city boolean and then try and accept and set it


Coding Adventure with Kaggle and Lux AI

to true in the exception case.


Coding Adventure with Kaggle and Lux AI

Truly expert coding and running this, things are looking better, but still having problems


Coding Adventure with Kaggle and Lux AI

with collisions.


Coding Adventure with Kaggle and Lux AI

To handle for these collisions, my first idea is to create another global dictionary that


Coding Adventure with Kaggle and Lux AI

tracks unit positions over steps.


Coding Adventure with Kaggle and Lux AI

If that unit has stayed at the same coordinates for some number of steps, it's stuck and we


Coding Adventure with Kaggle and Lux AI

want to move it.


Coding Adventure with Kaggle and Lux AI

For now, I propose that movement is random.


Coding Adventure with Kaggle and Lux AI

For each step, I'll iterate through the workers and if they're already in this worker positions


Coding Adventure with Kaggle and Lux AI

dictionary, I'll append the new position.


Coding Adventure with Kaggle and Lux AI

If not, then I'll create a new key where the value is a deck with a maximum length of three.


Coding Adventure with Kaggle and Lux AI

Essentially we'll just store the last three positions of each worker.


Coding Adventure with Kaggle and Lux AI

I'm going to go ahead and log this dictionary and run things to see if things are looking


Coding Adventure with Kaggle and Lux AI

as I expect.


Coding Adventure with Kaggle and Lux AI

They sure do and I can already see workers that are definitely stuck.


Coding Adventure with Kaggle and Lux AI

To hopefully remedy this, we'll check the last positions so long as we have two or more


Coding Adventure with Kaggle and Lux AI

in this deck.


Coding Adventure with Kaggle and Lux AI

Then we're going to check how many unique values using a set of the list of the deck.


Coding Adventure with Kaggle and Lux AI

If there's only one unique coordinate pair, this unit must be stuck.


Coding Adventure with Kaggle and Lux AI

So I'll log that and then move a random choice between north, south, east, or west.


Coding Adventure with Kaggle and Lux AI

Running this, I get a list index out of range error.


Coding Adventure with Kaggle and Lux AI

Checking the replay, I see this makes sense since the entire city died.


Coding Adventure with Kaggle and Lux AI

To solve for this, some more expert coding with a try and accept.


Coding Adventure with Kaggle and Lux AI

If we hit an exception, I'll just continue.


Coding Adventure with Kaggle and Lux AI

Next I get a build location that is none.


Coding Adventure with Kaggle and Lux AI

As the city grows, there will be many times where there's no directly surrounding tiles


Coding Adventure with Kaggle and Lux AI

that we could build on.


Coding Adventure with Kaggle and Lux AI

I think here we could actually loop and use some sort of range to keep checking more and


Coding Adventure with Kaggle and Lux AI

more distant tiles in a further away radius instead of just one out.


Coding Adventure with Kaggle and Lux AI

But for now, how about another try and accept?


Coding Adventure with Kaggle and Lux AI

I'll sort the exceptions to the log so we can handle those later.


Coding Adventure with Kaggle and Lux AI

Running this a few times, I can see that things are looking up for me and my empire is forming.


Coding Adventure with Kaggle and Lux AI

At this point, I'd say we're pretty close to something good enough as a first submission


Coding Adventure with Kaggle and Lux AI

and a decent starting point to leave everyone on without raising the bar probably at all.


Coding Adventure with Kaggle and Lux AI

This will probably be a pretty average bot.


Coding Adventure with Kaggle and Lux AI

The last thing I'll share is a way to test hyperparameter changes to your agent.


Coding Adventure with Kaggle and Lux AI

We have lots of values like city fuel, how many units could share a resource, and even


Coding Adventure with Kaggle and Lux AI

more nebulous things like maybe a new navigation or avoidance logic.


Coding Adventure with Kaggle and Lux AI

You probably have many ideas of how to improve things already from what you've seen here


Coding Adventure with Kaggle and Lux AI

or maybe a curiosity about what might change if you tweak certain things.


Coding Adventure with Kaggle and Lux AI

So one option is to just simply log how many city tiles you have ended the game with.


Coding Adventure with Kaggle and Lux AI

So I'll just create a name and then at the very end, if the observation step is 359,


Coding Adventure with Kaggle and Lux AI

the very last step of the game, write out how many city tiles we have.


Coding Adventure with Kaggle and Lux AI

We can use this to compare two agents and count wins in games or even just the average


Coding Adventure with Kaggle and Lux AI

number of city tiles in general that this agent is capable of creating.


Coding Adventure with Kaggle and Lux AI

So here for example, I'm comparing the agent code that we've built up to this point and


Coding Adventure with Kaggle and Lux AI

then agent R&D is with one change, which is to find an empty tile radius that can be greater


Coding Adventure with Kaggle and Lux AI

than one.


Coding Adventure with Kaggle and Lux AI

That one change made a pretty considerable impact in the average city tiles created.


Coding Adventure with Kaggle and Lux AI

You should also be probably paying attention to any scenarios where you ended with zero


Coding Adventure with Kaggle and Lux AI

city tiles.


Coding Adventure with Kaggle and Lux AI

These are often just throwaway games that you could have made a tiny change to save


Coding Adventure with Kaggle and Lux AI

it.


Coding Adventure with Kaggle and Lux AI

There are many improvements that we could make here.


Coding Adventure with Kaggle and Lux AI

We could definitely clean up this code a ton like we did in the beginning just to make


Coding Adventure with Kaggle and Lux AI

it easier to read and I'm sure there are even bugs in my logic.


Coding Adventure with Kaggle and Lux AI

We could also eke out more performance by doing things like paying attention to how


Coding Adventure with Kaggle and Lux AI

close is the night.


Coding Adventure with Kaggle and Lux AI

So cycles are in 40 steps, 30 of those are days and then 10 are nights.


Coding Adventure with Kaggle and Lux AI

Well we could be far more expansive, maybe in the first 15 days and then settle down


Coding Adventure with Kaggle and Lux AI

and adhere more strictly to fuel requirements as we approach nighttime.


Coding Adventure with Kaggle and Lux AI

Stuff like that.


Coding Adventure with Kaggle and Lux AI

There's so many things that we can do.


Coding Adventure with Kaggle and Lux AI

But I still think this is a pretty good stopping point and a place for people to build on if


Coding Adventure with Kaggle and Lux AI

they want.


Coding Adventure with Kaggle and Lux AI

When you're ready to submit, you just compress to a tar.gz and then you can submit your bot.


Coding Adventure with Kaggle and Lux AI

Within about 5 minutes you should start seeing matches against other people.


Coding Adventure with Kaggle and Lux AI

After about a day my score with this agent was 656 which leaves a lot of room for improvement.


Coding Adventure with Kaggle and Lux AI

I also see there are over 500 teams now so there's even more competition to play with.


Coding Adventure with Kaggle and Lux AI

So definitely check it out if you are interested.


Coding Adventure with Kaggle and Lux AI

I'll also put a link to my ending code up to this point in the description so if you


Coding Adventure with Kaggle and Lux AI

want to take it and start cleaning it up and adding your own logic, go ahead.


Coding Adventure with Kaggle and Lux AI

This video is sponsored by Udacity which is currently offering 75% off of all of their


Coding Adventure with Kaggle and Lux AI

nanodegrees if you use code CENTX21.


Coding Adventure with Kaggle and Lux AI

They've just recently redone the self-driving car nanodegree which I've spent some time


Coding Adventure with Kaggle and Lux AI

checking out and would definitely recommend if you're interested but the discount code


Coding Adventure with Kaggle and Lux AI

works on any of the nanodegrees so feel free to pick something else that piques your interest.


Coding Adventure with Kaggle and Lux AI

I'll put links in the description that you can use for the 75% off and thank you to Udacity


Coding Adventure with Kaggle and Lux AI


Goroutine Synchronization - Go Lang Practical  Programming Tutorial p.19

What is going on everybody welcome to part 19 of our Go language programming tutorial


series in this tutorial what we're going to be talking about is kind of building off the


last tutorial which is concurrency in Go specifically with Go routines.


So in the last tutorial we started to show some examples of Go routines and how easy


they are at least at the basic level to get them to do cool stuff.


We started to talk about some of the issues that we're going to have right out of the


gate when we actually start to try to use these in practice.


The main one and currently being that when we run everything as a Go routine nothing


is blocking therefore we're using like a sleep to make sure the Go routines finish.


Well this is not really ideal.


So instead what we're going to do in this tutorial is work with the sync package so


we're going to go ahead and import sync and then from here we're just going to define


a starting variable so we're going to say var we're going to call this wg which is just


going to be short for weight group and what it's going to do for us is basically this


is going to be sync dot weight group type and what we're going to be able to do is specify


hey we're currently working on a Go routine we want to wait for this Go routine so every


time you have a Go routine you'll just kind of add one to your weight group and then every


time a Go routine is done you'll notify the weight group okay we're done and then that


way you can actually wait because for example it won't necessarily always be the case that


you just want to run some Go routines and then you're done.


There's going to be times where maybe you want to run some Go routines wait for those


Go routines to finish and then run some more Go routines right so it's not just about hey


let's make sure these just execute in our program once or whatever it's there's going


to be lots of ways that you're going to want to synchronize Go routines so this is one


of the ways.


So anyways once you've once you've defined your weight group here what we're going to


do is we're going to come down to the main and basically every time right before you


start a Go routine you just add one to to the weight group so we'll just add one there


we'll add one here and then at the end here we're just going to do weight group dot wait


so what's going to do is it's going to wait until these finish and I'll get rid of the


sleep as well so one thing to consider is well how's it going to know when it's done


well what you're going to need to do is you're going to need to notify the weight group when


things are done so basically when this for loop is done executing it's done so we're


going to do wg done and now we're going to do is we can save this we can come over here


to our program here and we can run this cool and so it's done we didn't waste any time


with sleeps we didn't you know we didn't add too much time to sleep we also didn't miss


out on anything everything ran in the order that we expected and all that and so you know


just to exemplify this hey there we'll just add a high here let me save that let me add


a weight here save that again let's run that so as you can see this is kind of a way that


we've we've synchronized these now because we're bleeding after these two and then we


add another one and so then that one just runs together so what if we instead we could


have removed this way and now they'll they'll run with each other so hi hey there hey hi


there hey hi there anyway cool super basic kind of addition to our go routines but now


we've kind of run into another issue what would happen if somewhere along the lines


we we hit some sort of error and this this code just kind of panics out or whatever and


we never actually get to tell the weight group that we're done well we're going to be waiting


for ever so in the next tutorial and the coming tutorials we're going to be talking about


well first we're going to talk about the defer statement and then we'll be talking about


panic and recover so that's what we're going to be covering in the next tutorials if you


have questions comments concerns whatever up to this point feel free to leave them below


Rethinking Attention with Performers (Paper Explained)

Hi there, today we'll look at rethinking attention with performers by researchers of Google,


the University of Cambridge, DeepMind and the Alan Turing Institute.


This paper is yet another paper in the quest to make transformers more performant and what


better name to give to a technique than the performer.


So the performer performers are a new kind of class of models, they try to approximate


the transformer.


If you don't know what a transformer is, I've done like a ton of videos on transformers


on attention mechanisms.


And you can, there's more than enough material to look that up.


Today we'll talk about performers.


And the performers as I already said, they approximate transformers.


And they do so without running into the classic transformer bottleneck, which is that the


attention matrix in the transformer is has space and compute requirements that are quadratic


in the size of the input.


And that limits how much input you can put into the model.


So it kind of limits how long of text you can input if you work with text or how big


your images are that you can work with.


This is all kind of bad at when you use transformers.


So the performers get around this by this technique, they call fast attention via positive


orthogonal random features, abbreviated favor plus they use this favor plus to get around


it.


And what's interesting is that the favor pluff, I'll just call it favor, this fast attention,


it is potentially useful beyond transformers.


So it's apparently been here developed in the realm of the transformers, but they say,


which may be of independent interest for scalable kernel methods, you'll see what they do is


they approximate the attention matrix by decomposing it, but they do it in a special way.


And they do it in the in the way if you know what random Fourier features are, maybe you


can kind of think, think ahead a little bit, if not, we'll get into it for sure.


I think honestly, this might be one of the enabling one of the next mini breakthroughs


in deep learning, not big breakthrough, but kind of mini breakthrough.


I remember a time when we used sigmoid and tanh nonlinear, believe it or not, you young


kids, at the beginning of deep, not the beginning of deep learning, but before deep learning


really took off.


It was the sensible thing to use softmax and tanh nonlinearities everywhere in your neural


networks, because well, first of all, they were like differentiable.


So that was cool.


And then, you know, it was sort of how nature does it with the step function in like it


was an approximation to the step function in the true neuron and so on.