Announcing Livebook - José Valim

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

In the future make sure to link to the repo in a comment. Here’s a link to the code:

https://github.com/elixir-nx/livebook

👍︎︎ 2 👤︎︎ u/dojoteef 📅︎︎ Apr 18 2021 🗫︎ replies
Captions
hi everyone i'm jose valim and today i want to share with you a new tool that we have been working on at dashbit called livebook so livebook is an interactive and collaborative code notebook application it's implemented in elixir with phoenix live view so you can run it in your machine it's open source and you'll be able to after you run it you'll be able to access live book directly in your browser so that's exactly what i have done i'm running autobook on my machine a live book on my machine and um yeah and it's a web app this is the the home page and let's jump straight to it let's create a new notebook and see how it works so as a code notebook we can do you know very basic things such as adding uh a title to it and then we can start creating our section so let's start introduction section and we can start adding some markdown cells right this is uh markdown and we can also add some elixir cells right so i can say hey i want to print hello road and then i want to execute it and you can see that it prints uh hello world and return as a result of this function right so far so good but uh in order to show how live book really really works let's build something concrete okay and what i want to build is that i want to redo the talk that i did two months ago so two months ago i gave a talk at lam the days where i built a neural network from scratch using the nx library so nx is a new library for elixir that stands for numerical elixir and it's meant to be a set of building blocks for us to build um applications for elixir that rely on numerical computing so uh you know for example machine learning related applications that's the example that i picked so in my talk what i did is that i built a new new neural network from scratch to work against the eminence data set okay so that's what i want to do today as well but instead of building the neural network from scratch i want to use the axon library that was released by show moriarty a week ago last week and the axon library it's a library that is high-level building blocks for building neural networks in elixir okay so that's what we're going to do so let's get started since um so i'm going to create a new section and since we are going to use the axon library we are going to use nx uh the first thing that we are going to do is that we are going to list our dependencies okay so i'm going to copy and paste it so you don't have to see me typing so i'm going to copy some elixir code and i'm going to install those dependencies and we can see here that this declaration is a little bit verbose right but that's because those packages they have not been released yet if they were released we'll be able to do just this just list the dependency and um and its requirement the version that we want okay and here you can already see the first difference with livebook is that dependency management it is explicit okay so every time you want to install the dependencies you need to call mix install and mix install is a new function coming in elixir 1.12 we have the release candidate out right now and and this is very much intentional so in elixir we have a package manager since version 1.0 came out but the only way you could use those packages the only way you could use those dependencies would be if you started the project and listed that in a project right because we never really enjoyed global dependencies the issue with global dependencies and having things that depend on your system is that you know maybe you got a new machine maybe you want to share a script with somebody and a notebook with somebody and if that script that notebook is rely on global dependencies it's going to be hard to reproduce the results okay so a mix install is meant to solve this problem it's meant to make sure that we can easily add dependencies in our scripts in our notebooks and uh still have that listed there very explicitly so we can still share them and reproduce the results elsewhere so i have listed mix install i have executed the code it installed the dependencies and now we can move forward to use those dependencies okay so let's get started so in order for us to build and actually to train a neural network what we need to do is that we need to download two data sets so one are the training images so in our case the minus data set is a data set with images representing digits right like the digits zero one two three four five representing numbers okay and um and we so we need to download this data set with the images and we also need to download the label data set that's going to tell us hey this is a five this is a zero this is a one this is a four and so on so let's do that okay so i'm going again to copy the url here so you don't have to see me typing all this so i'm going to download this data set so i'm going to download the tray images and let's execute this to see how that request is going to look like so you can see that it returns a tuple for us with you know the the the the response header and then you know all all the response headers in the list the actual http headers and then the body that we want to download at the very end okay so let's better match on that so we know it's the top with okay these status and the headers that we don't care about and we have the train body here okay so let's assign that to a variable let's execute this again and now we hopefully have the the body of the request in the train bar body variable so let's continue working on that i know that this is uh gzipped so we need to instruct to extract it i'm going to use the module z leave that comes with verlang so let's call gunzip on the train body and that's going to unpack it and you can see that is returning now some binary blob for us and i know from experience that this binary blob the first four bytes okay is an identifier that we don't care about the next four bytes is going to tell how many images we have in this data set and then it's the next four bytes are going to tell us how many rows each of those images have and then how many columns those images have and finally i want to get the rest of the binary is going to be just to be this blob of images which i'm going to reassign to the train train by variable here so let's execute this let's print those values just so we can take a look at them okay so we can see that we have a data set with 60 000 images and they are 28 per 28. okay so far so good so let's continue iterating on this so now we have the train body and this is a blob right this is just all the images one right after the other so we want to give us a little bit more structure to this so the first thing and the way we're going to give more stretch to this is to convert it to a multi-dimensional uh array right we're going to convert it to a tensor what's the where one dimension is going to keep it each image and then the other dimension is going to keep each row it's going to keep the rows entries and then the columns okay so let's convert it to a tensor and to do that we can use the we're going to use an x and we're going to call from binary and we know that each element in in this blob is a byte okay so it it's a byte representing um those images as you're going to see they are monochromatic so it's a byte representing like hey if it's zero it's black and if it's uh 255 that's going to be white or the opposite okay so it's just a scale so let's do that let's create a tensor and you can see that it really created a really large tense right because it's just a single dimension for now with you know 47 million entries or something like that so um okay now let's uh now that we have created this this binary blob what we are going to do is that let's give some shape to it so i'm going to call reshaping here and uh the first dimension is going to be the number of images then we're going to give the number of rows and they're going to give the number of columns so you can see that now it starts reassembling something multi-dimensional right where we have a dimension for the images and so on and in order for us to actually see the images let's get a heat map out of this right and you can actually see here that we got like hey this is the number five this is the number four this is the number sorry this is number zero this is the number four this is the number one so we got the heat map of those images which is really really nice so um okay so we're starting to work for the data set and it's time to to bring some shape to things let's structure this a little bit more now before we move ahead so i want to locate everything before the heat map um should be to be a tensor and that's where we're going to get the heat map for and also what we're going to do is that when we are working with neural networks it's common for us to normalize the data to be between zero and one right we don't want to send like high values like 255 or even bigger than that because that may make our network goes towards infinity or something like that so it's common for us to normalize it so let's divide everything by 255 so everything's between zero and one and i'm going to run this again and you should see now we once again we get the heat map everything's still the same but we have we can see that the type changed to a float 32 okay fantastic we are done with the training image and now we have to do the same thing uh but we are going to do it for uh the labels okay so first thing i need to do is that i need to download the labels i'm going to once again uh copy and paste the url okay so if i copy and paste it here let's execute it we got a response and again we're going to better match on it so we're going to get the status we're getting the headers and we're going to get the label body this time so let's make this a little bit prettier let's run this again so we put the label body into the proper variable and now we can work on it and again it's going to be very similar the first thing that we want to do is that we want to just zip it we know it's uh it's zipped and again the first four bytes is an identifier that we don't care about and then the next four bytes is the number of labels and we know how many labels you expect right we expect to have exactly 60 000 labels then the same number of images and then i'm going to assign the rest to label body again let's run this and let's see how many labels we got we got exactly sixty thousand beautiful okay so let's continue moving forward so um so what we're going to do is the same thing we are going to create a tensor okay so let's start with the label body oops let's start with the label body and let's create a tensor and again each entry in this blob that we have of the labels it's going to be a byte right it's a byte so it's the same type it's ooh eight and if i run this you can see that now we have a tensor and you can see exactly that we got a five a a zero a four a one exactly the images that we saw so this is good and but there is one thing um our new network it's not going to say hey this is a five it's not going to to say this is a zero right this is not the output that we are going to get from our new network instead what our new network network's going to return that it's going to return a list of probabilities going to say hey i am like um 60 sure that this is a 5 right and you know maybe uh 20 and i think like 20 i think that this maybe is a tree or something like that okay so um so this is not the output that we're going to get so we need to change this slightly we want this to be a list of probabilities and in this case because it's you know we we have looked at those images we are like 100 sure that this is a five right so let's so let's convert this format so let's reshape it okay and when we reshape it the first thing that we're going to do is that we are going to wrap each of those values in a list okay so now you can see that i have a list with five so this is a good first step after reshaping i have a list with five a list with zero and now i want to convert this list with five to be a list with zero zero zero zero zero one right where the position part the number five is going to be one and the way to do that is that we just need to compare each of those rows right that we have here um to to a tensor that we are going to build um from getting from zero to nine right so if we compare those we can it's going to be one it's going they're going to be equal exactly when we have the number five so we can see now that this is at least saying hey i am a hundred percent sure right i 100 the probability is one here 100 percent that this is the number five 100 this is the number zero 100 is the number four and so on okay so all right that's our label tensor let's assign that into a variable and um this is it we have our train tensor we have our label tensor so now all we need to do is that we need to implement our neural network and then we're going to be able to train it so let's keep going ahead so in order to uh build our new network we're going to use axel and as i said exxon is a high level library for building neural networks in elixir and um and they are and it has both the we can either build a model or use it it's building blocks to build the network from scratch we're going to go the high level approach we're going to do the model okay so the first thing that we need to declare here is our inputs and our inputs we know that it's something like this right we have 60 000 images per 28 per 28 but here's the thing we never give all the 60 000 images at once to the neural network we we break them into a small batches which we are going to decide later on so i'm going to say look the batch dimension is going to be new and then because our new network is a very simple neural network it doesn't actually understand like rows and columns in an image so i'm going to flatten those we don't care about those and then i'm going to say we're going to have a hidden layer okay with 128 units and the activation activation function here is going to be a sigmoid and we are going to have another this layer this is the output layer so therefore needs to have the size of 10 right one for each image okay which digit that we want to recognize and the activation function is going to be softmax okay so let's execute this and we can see that it's printing the model that we just built really nicely with all the parameters the shape and so on and um and i know that i am sailing through this right i'm not explaining the details so you can go watch my talk as i said in the days where i build it from scratch i go more into details you know what are those activation functions how they look like and so on but this is good enough for now we have already built our new network model and with our new network model in hand we can actually go ahead and train it okay and the first thing that we need to do to train is as i said okay we don't we we're not going to give all 60 000 images at once right so the first thing that we're going to do is that we we are going to convert our train tensor into a list of smaller tensors right so so it's going to be a list uh of instead of 60 000 images going to be a list of tensors with 32 images list each okay so you can see here instead of being 60 000 is 32 that's our first uh batch this is our second batch and so on and we are going to do the same thing for the labels okay so we are going to create another uh batch at least with our label tensor and again of 32 and now we can train it okay so to train it we're going to start with the model and the first thing that we do is that we define uh the training step and we're going to define a train step with um our optimization function and because this is a neural network that is returning us we say it's multiple categories right it's either a zero one two or three it's not binary so we are going to use the categorical cross entropy um function here to that it's going to build on top of the laws it's going to help us train this neural network and in order to optimize the neural network itself because when we have a neural network we want to minimize the loss we want to it to be as precise as possible i'm going to use a very simple algorithm here uh and axon has things that are more efficient than this like adam for example but i'm going to use the same that is as i used in the talk two months ago which is stochastic gradient descent with 0.1 okay with the step of 0.0.1 so that's our training step and now that we define the strap we can the step we can effectively train it so i'm going to give it the train batch i'm going to give it the label batch i'm going to make it go through uh 10 epochs okay 10 rounds and what i'm going to use here is that i'm going to use the exla compiler right so at the beginning i listed three dependencies right i listed um i listed nx that we use we listed axon and i listed excela that's what we are using here and i'm going to explain what it does soon let's just make this run because it takes like uh 10 seconds or so and so the result of training is going to be the trend parameters and the training state that we don't care about so let's run this and you can see that the box they are going very fast because this is a quite simple neural network and um and the reason why it's going the other is it's going very fast is that we are using xla the xla its bindings for the google xla google xla stands for accelerated linear algebra it's a tensor compiler that can get all the code that we've written in the neural network and compile it just in time to run on the cpu or on the gpu okay so this makes it quite fast and as i was talking we can see we are done training the new network okay and you can see that it went for all ten epochs and it returned the train parameters so we can actually see if it works okay so uh let me start another elixir section here and i want to now that we have the trend parameters let's try to predict okay what i'm going to do is that let's predict the first batch right because we know how it looks like right we know that it is a five zero four and so on so let's give the train parameters when we are predicting and let's give the first batch in our list of batches so now that i execute this okay uh you can see that it's returning a list of probabilities for the first one and you can see that in this list of probabilities it's saying hey i am like 57 sure that this is the number five which is good because it is a number five right and i'm and i'm like 99.7 sure that this is the number zero right and then um and then you know eighty-eight percent sure this is the number four we can actually use arg max here on the the second axis to tell us the results and you can see it's a five a zero a four or one exactly what we expected so you know we're able to to train a new network and i know that i'm giving the training data to predict that's not ideal but it's good enough for now okay so that's it we've you know we we've trained the neural network directly from live book right and everything was very interactive okay and um and i hope you have enjoyed it but i'm not done yet not quite yet there are a couple of other features that i want to show to you of things that we can do with live book so the first one is the next one or rather that i want to show is that you can actually run tests inside live book so let's show an example so first i'm going to define a module here called uh hello i'm going to define a function called road and then it's going to return this string hello okay i'm going to execute this code and now you're going to write tests for this code so the first thing that we need to do is that we need to start the x unit test framework that come as part of elixir and we need to pass a flag in here say auto run false because the way it works in in elixir is that x unit the defaults are tailored for the command line so when you load your tests before the command line exits what it does that automatically runs a test so the tests for you so we want to disable this behavior and now we can define our test cases as usual right so let's say hello test and we can say use x unit case we can make it async if you want and we can define our test let's have a very simple task that the check that it works and we want to assert that um we want to assert that hello road is going to return the string hello road and i'm going to add a exclamation mark here okay just to make this test fail and now after you you could define multiple tasks across multiple snippets and when we are done we can call x unit run and that's going to run all the test cases that we have defined so let's run this and you can see that you know it has just executed the task for us and it failed right and it's showing it's comparing the left side with the right side the left side with the right side and it's showing look it's missing this character right we have this additional character so let's fix it let's fix the code okay let's add this here and let's let's reevaluate the cell and what you can see here that now that we have evaluated the cell again the cell below it all the cells below it they are marked as a stale and this is the way that live book is showing like hey you know you changed some code before and that code before is going it is going to change the results of the other cells most likely if you run them again so i'm marking this as a stale so there is a very visual clear indication that this cell is you know the results of the cells depending on the state of code so if i run this cell again you can see now that the stale code has been removed and that our tasks have passed okay which is a great news um what else there are other cool things other cool things that we can do with live books so we have seen so far how it is interactive but we haven't seen how it is collaborative so let's take a look at that so let me take this from furry screen and what i'm going to do is that i'm going to open up another tab in here [Music] and let's have those things side by side let's open up the same notebook in here and it is collaborative because as you can see here you know as i have this notebook on this side here if i come here and i start doing some changes like i add a new markdown section we can see that the other side is automatically updated as we are changing things and this is a good opportunity to show another feature we also have support for map formulas uh via ktex so if i add a more a math formula it appears on both sides you know if i add some elixir code it's going to continue appearing uh on both sides and i can actually work together on the same code at the same time right you can see that each place is in a different cursor and then we can collaborate while uh writing some code so really collaborative features right here from the beginning um and yeah so this is it oh i have one last thing i actually want to share with you so let me go back to full screen which is okay you you know you started this this notebook you started this live book you collaborated together but what do you do now right you have to save it right you have to purchase this live book somewhere so we can do that i can come here right now you can see that the notebook is on memory only but i can say hey i want to save to file and i'm going to save it to a file called example and you can see that it created a example.live md file and live md stands for live markdown it is effectively a subset of markdown so just for fun what we are going to do is like let's use elixir to to read the let's let's use live book itself to read the file that it saved okay so uh we are going to read example.livemd and we are going to print it um and we can see here that the contents of this file is just marked known so you know we have the title we have the sections we have all the code that we wrote right everything is here in this large live markdown file and um and the only reason why it's a subset is because it has some restrictions like you can't have at the moment called between the title and the first section right like if you have any html in it we are going to discard it when we load it for security reasons so there are some restrictions but it's mostly marked now and that's the important part right because it means that after you're done with your work you can save to a file that you can ship to your colleagues and your colleagues they can if they don't have live book enabled if they don't have live book running they can just look at the file and see what you wrote and what you are trying to explain right um or for example if you want to commit this to a github repository it's going to be readable right you can check it on github if somebody sends a request you can review the the notebook directly in the request without having to bring it to your machine you can make suggestions you can make change so yeah so this is the live book format um [Music] and um and you can just persist it anywhere you want so yeah i believe that now it's uh it's for real this is it uh so uh some final uh words um first of all a huge thank you to iotom kosko he has joined dashby to work on live books so this is the result of his work uh amazing work thank you jonathan and it's worth saying that this is just the initial vision right of what we have uh in mind uh for live book right we say that it's interactive and it's collaborative and we're able to get some ideas about how it is interactive and collaborative during this presentation right but it's uh it's going to be much more than that so for example um we saw some collaboration but there was like no visual indicator of how many people are working on the notebook so that's something that we want to work on next right and we really want to make it interactive so you can actually and not interactive only for like a numerical computing or machine learning but really interactive because the software that we write in elixir it's a live system right we have processes we have supervision trees and we want to make it easy for you to interact with those entities and see how those entities they are behaving so we have a bunch of ideas in this area right and i hope you even though it's still our first step on this journey i hope you have enjoyed it and we not only show live book but we talked about live book we talked about exxon which is this high level library for building neural networks and elixir and we even showcase some features that are coming in the next elixir version alex 31.12 and i hope you could see how all those features they are starting to um you know to work together to bring this really compelling development experience thanks for your time and i hope you have enjoyed it bye
Info
Channel: ElixirConf
Views: 13,101
Rating: 4.9946809 out of 5
Keywords: elixir, liveview
Id: RKvqc-UEe34
Channel Id: undefined
Length: 28min 24sec (1704 seconds)
Published: Tue Apr 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.