Blockchain Consensus Algorithms and Artificial Intelligence

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello world it's Siraj and welcome to the blockchain I know I've been talking about blockchain a lot these past few weeks And there's a reason for that because this space is heating up really fast and as people who are interested in AI The blockchain is going to provide a lot of opportunities for AI that's what this video is about what I'm gonna Do is I'm going to first talk about the motivations behind combining these two different technologies? blockchain and AI then I'm going to talk about some example apps that use the blockchain and AI Currently so you get a you know some some ideas on on how you could make your own Startup, or your own open source project then I'm going to programmatically write out the proof-of-work Algorithm, this is the this is the key programming bit for this video and after we talk about blockchain and AI we're gonna program this Thing because it is the workhorse of Bitcoin without the proof of work algorithm the blockchain is nothing Okay, so that's what we're gonna code out and the proof of work algorithm is a distributed Consensus algorithm and that's really really important for AI and by the end of this video You will understand why at the at the end of the video I'm gonna talk about a few other distributed consensus algorithms like proof of stake proof of burn and proof of activity But the main algorithm is going to be proof of work. That's that's really what I want to get through, okay, so Let's talk about blockchain and AI so a blockchain a blockchain is oh my god It is such a buzzword But it should be but a blockchain is just a data structure that distributes trust across many nodes Instead of one it's like a glorified singly linked list remember you remember a linked list from data structures and algorithms, right? It's a glorified singly linked list that lives on Everybody's computers all the miners computers all the nodes and all the nodes in the network on their computers So we can think about the blockchain as a huge chain of transactions Let's say we're all part of the Bitcoin network right Bitcoin is a set of rules for how a network should run Let's say you and I are a part of the Bitcoin network I've got some Bitcoin and I want to send you some Bitcoin when I send you some Bitcoin Let's say to Bitcoin hold Hottel to the moon right once I send you some Bitcoin you're gonna hold that Bitcoin and the network is going to represent that Transaction as a 25 character. Hash, okay? This is a hash a string of letters and numbers And then what's going to happen is all the miners in the network are gonna have to validate that Transaction the majority of the miners have to validate that transaction for it to be added to the chain of blocks this immutable this unchangeable list of all the transactions that have ever occurred in the network that all the nodes have to store a copy of and we Can look at this chain of blocks, and it looks kind of like this This is a very simplified view of what the blockchain looks like each block contains a list of Transactions and these transactions are stored in a Merkle tree We'll talk about that in a second, and then it also has a pointer to the next block in the chain That's what I meant by a singly linked list. It's all pointing in one direction so it's a chain of blocks a blockchain right so in a Murkily by the way is a way to organize these transactions multiple transactions Right we can hash a set of hashes, so it creates another hash and then hash those abstractions of hashes again and it makes a tree like structure, and this is just a way of organizing all of these different hashes and Each miner is going to before they can vote right all the miners have to vote on the validity of any transaction Before they can vote they have to provide this proof that they that they've computed this random mathematical problem this proof of their work this proof of their computational work Since every single miner has to provide this proof of work when they give out their vote yes or no this transaction is valid you Would have to have more computing power than the 500 fastest supercomputers in the world combined to create a fake transaction and that's why Bitcoin has an 80 billion dollar market cap has been running for seven or eight years now and No one has broken it so that's blockchain and now we'll talk a little bit about deep learning right so machine learning in the In the sense of we're just gonna classify something to keep it simple instead of generating Right we have some input data some images of cars. We want to select the feature that we think are going to be most relevant To identify those pictures as car or not car now remember This is in the labeled data scenario the supervised scenario We have some cars then we have a set of features for those cars like the color blue red green And we have the the size that make the model and we want to choose what those ideal features are for our classifier to learn from and Once we do that then we can classify it That's that's the that's the original way that machine learning worked But with the but with deep learning instead of having to choose those features by hand The deep learning algorithm will learn what the best features are and this is Extremely useful because hand picking features is a pain And we don't want to have to do that deep learning is a smarter way of learning from our data to classify it So one question is wait So how is this all related well deep learning is a set of algorithms in it's a subset of machine learning so machine learning Includes a set of algorithms that learn from data that means random forests that means support vector machines that means Neural networks and when we take that specific algorithm the neural network and apply it to lots of data Give it lots of computing power and so it has lots of layers We call that deep learning And it outperforms almost every other machine learning model almost every time on almost every task and that's why it's the hottest space in machine learning right now an AI is a is a Superset of all of that so AI doesn't just have to include traditional Algorithms that learn from data statistically we can all we can also use heuristics kind of like these estimate these these guesstimates right or We could use genetic algorithms or evolutionary algorithms There are all sorts of algorithms that encompass AI but machine learning and deep learning are the most popular and offer the most promise Right so it's kind of like this whole yin And yang thing if we think about the eight if we think about AI and blockchain. There's a lot of synergy I'm gonna use the word synergy here Synergistic between these two technologies, and they are very different technologies, but they're very complementary to each other So AI is probabilistic we don't know what the outcome is going to be it's changing right these AI? Algorithms are constantly updating their weights their learnings based on the training data that they're fed and their Algorithms that guess what reality is going to be Blockchains on the other hand are deterministic that means we know what the outcome is going to be they are permanent they are immutable they do not change at all and these are algorithms and cryptography to Record reality so while AI is about machines making educating educated guesses for us Block chains are more about validation and permanence and greater degrees of certainty and and control, right deterministic versus stochastic aka unpredictable right so AI can be our intelligence right it can it can learn from data It can it can find these insights that we could not and the blockchain will play the rule will play the role of the truth Teller the mediator that's going to bring two parties together and to two complete strangers And let them trust each other without needing a third party in the mix It distributes trust across many nodes So let's talk about some use cases right so I made a video on this on this decentralized marketplace for data called open mind But let me go over it really quickly again so in in in terms of AI and blockchain This is one of the most perfect examples, right? Because we have we have a model right I found my data scientist. I have a model that I want to Train Let's say it's a deep neural network I want to train it on your data right so if I want to train it on your data If there was a third party involved like some central app right some central server, then three different people could steal from each other Right if I'm an AI scientist, and I'm trying to trick train on your data I could steal your data, or if I give you my model you could steal my model Or if we use the third party they could steal your data And my model right so what is a way that we can have this? Transaction right your data my model I pay you for your data You train my model on your data return it back to me without anyone having to trust each other And that's the problem that open mind solves by using the by using AI and blockchain Specifically when I say blockchain I'm talking about a smart contract so the TLDR of a smart contract is some code that lives on a blockchain Specifically if theorem is probably the most popular of those block chains where you can you can push code onto the blockchain itself right so Normally, you would push code onto Heroku and then Heroku would say hey, I've got your code I've computed it the answer is five trust me but with a blockchain you don't have to trust Heroku to say to have computed your Algorithm you instead because of this distributed consensus algorithm. That's at work proof of work. You don't have to trust anybody You know that this code is gonna be valid even though It's living on all of these different nodes right so you would push some code a smart contract to this etherium blockchain all the nodes would have to verify that that or the majority of the nodes would have to verify that this code is indeed valid and then they Would also use this consensus algorithm like proof of work or proof of stake to maintain validity So in in the case of open mind a startup sends a model spec to a smart contract Right and because the model will initialize inside of the smart contract itself No one can we know that I'm not trying to steal anybody's data I haven't hidden anything in the model because the model is initialized on a smart contract so no human is in charge of that and Then the smart contract is gonna send that model SPECT That is some money and some instructions on how to train that model to an Oracle and the Oracle is going to Which is a third party is gonna generate a public/private key pair? It's gonna encrypt the model and then send it back To the smart contract and then after training on all the users the model is sent back to the Oracle its decrypted Encrypted with the Oracles keys then sent back and since the Oracle acts as a trusted third party No one can decrypt the model or gradients. We'll have a second smart contract that as an insurance policy Against the Oracle sharing secrets, so it's a two-step process the Oracle will pay a bounty let's say the Oracle is AWS the Oracle will pay a bounty and if anyone discovers the keys the public/private keys for the model then they get that bounty so the Oracle is incentivized to not Give out those public keys right so it's a it's a trust less system because it uses AI and blockchain. Let's keep going Another example is called, New Marais. I new Mirai numeral. Which is a decentralized AI hedge fund Okay, so it also uses the blockchain AI together so what this is is It's a hedge fund that lets data scientists make predictions based on their models so New Marais I offers data for free okay, so it's offering all of this stock market data And I or you as a data scientist can say I'm gonna take this data I'm gonna create some model algorithm, and I'm going to submit my I'm gonna training on this data And I'm going to submit my predictions back to New Marais I with some tokens to support that prediction now this token is it's it's it's internal currency numerous numerous internal corns currency called NMR okay, and it's built on top of the etherium blockchain and so, New Marais I will take all the models that all the data scientists give it and it will combine it into a meta model this this Meta model so use all the learnings all the gradients from all the models And it will combine it to produce this meta model and then it's going to make and then time will show whose predictions were the most accurate and Those data scientists who had the most accurate predictions will not only get their money back But they will get a reward on top of that so everybody is incentivized to make good predictions but if you don't make a good prediction, then you lose your money and The fact that they are using an internal token is a great use case for the blockchain because it acts as a hybrid Asset right you can use this internal token as both a currency but you could also save a part of it and use it as a Security or a stock a share in the network that will grow in value over time as a network becomes more profitable right so That's a way to kind of share the profits with your users, and if I had a version of New Marais I that did not have an internal token that the data scientists did not get to share in the profit from of course New Marais is gonna win and the way to do that is to use a blockchain Where the users can share in the profit from the from the network? Another example is a self-driving car Network pretty soon We're gonna see a lot of these self-driving cars tens of thousands of these giant machines that are autonomous they're on the road And they're moving at you know very fast right so we have to be very careful about how we architect this self-driving car Network now one way to do it is using a centralized node, right we have one server And it's controlling every other car just think about the security implications of that right if someone hacks this server Then every single car is now vulnerable to attack we need a better system, right Cars are on the road. They might be communicating with each other. Oh, you're on this lane. You're on that lane Let me share some resources with you. Let me park myself. Let me pay for my own gas Let me maintain myself through some kind of automated car maintenance shop you can see how their Cryptocurrency works well here, and you can also see how smart contracts work. Well here. Let the machines Talk to each other without the need for a trusted third party, and there's a third point as well Ideally we would want an audit trail right so if in case some kind of car crash happens or some kind of accident Anyone can can verify what the AI model was thinking what its? List of decisions were immediately because it's publicly verifiable and it's immutable and aka a blockchain right so Self-driving car networks that are a fully autonomous Involved no trusted third party. They are networks that run and maintain themselves so after a self-driving car network it's another great example of how to use AI and blockchain together and Right so like I said, it's it Encourages data sharing whenever we use a blockchain it encourages data sharing which leads to more data Which leads to more which leads to better models? It's immutable and it provides an audit trail right so any Kind of decision that an AI makes as long as it's as long as says it's on a blockchain we can all verify all the decisions that it's been making to to kind of come to a conclusion about its result and Native assets are just a great example of giving more power to our Applications right that have these internal tokens where the users share in the profits from the app And the AI as well which leads me to the wildest, but also most awesome slash also most scary idea which is Artificially intelligent distributed autonomous organizations Or a Dao you could we call it a doubt a doubt and in a way the self-driving car network is a Dao But a Dao is a computational process that runs autonomously on Decentralized infrastructure that means there is no central point of control with resource manipulation that means it has some kind of Value some crypto token that it can use to spend on things which means it's code that can own stuff So what do I mean by that well? I think it's best served by looking at this example so check out this trippy image This was generated by an AI ok, but that used genetic programming now imagine the art Dow okay? This isn't this is a this is a set of instructions that are all smart contracts that live on the blockchain This is an unstoppable AI So let's look at this process so what what the arc doubt does is it runs an AI R engine to generate a new image like this using some kind of a I machine learning deep deep learning technology, and then it claimed claims attribution over that generated image on the blockchain Okay, so so we all know that it was the AI that that created that it creates multiple editions of that Art it submits it to some kind of marketplace And then it's it sells it right so then it earns money for its its art and then it uses that wealth to Then pay for its own resources like computing power and generate more art and it just keeps getting better and better Right so you have this AI that is doing this task that the Creator that The Creator asked it to do and it's using money to maintain itself and grow itself, so it will get more and more wealthy right so This could do a lot of great things for us, right? But you could also see how this could be a bad thing right it depends on the person who is creating this technology this is So powerful is this insanely powerful technology, and it's up to us You watching this video to use this technology for good for the good of humanity. It's seriously It's up to us very few people understand any of this like forget blockchain or a I Even Python or code right the fact that you're here at this level of the video is really impressive So I am very proud of you, okay, so anyway. Let's get to the good stuff the code, so there are two Consensus distributed consensus algorithms that are the most popular proof of work and proof of stake let's talk about proof of work So the proof of work algorithm is the reason that Bitcoin works, right? We have this Distributed ledger of all the transactions on the network that every miner stores a copy of on their Computer and when they vote on the validity of a transaction they have to offer this proof of work right this proof of work that they've computed this random math problem and Only if they have done that can they vote on a transaction so it's a four step algorithm It's a four step algorithm so a miner will retrieve a group of transactions from pending transactions to be valid to be validated a block and It's gonna process some work to generate a proof. It's gonna It's gonna broadcast that proof of work to every other node in the network And then it's going to and if the majority of the network agrees on the validity of the transaction That transaction is then added to the blockchain Right, so let's let's let's code this out. All right So I'm gonna code in Python what the proof of work looks like Let's start off by importing our dependencies right so sis is gonna. Help us print out some statements time is gonna Help us clock. How fast this is going hash live is gonna Help us. Hash a transaction and then lastly we're going to import unpack and pact which is going to let us compress and decompress Some data, okay, so those are our dependencies And now let's let's write out our Variables so the first variable we're gonna write out is the time stamp right so a time stamp And this is where we're using our time library. We're gonna say okay time dot time. That's our time stamp We're gonna convert it into a string and that's our time stamp the next Variable so this the next variable is going to be the message so the message it just is just gonna be Is this is just an example, but but hashing a string is suitable work for this project so this is a random message? right we can think of this as a part of a transaction and then lastly we have some payload so the payload is a Combination of the times of the things that we're going to encrypt which is gonna be the time stamp And then the message right, so these are the things that we want to send to the network it could be blocked data as it is with Bitcoin mining in this case it is a message with a payload a message with the timestamp and that is the payload okay, so those are the first three variables and Now let me make this readable so now we're going to Write out a nonce So a nonce is gonna increment from 0 to n until the target is met by the way a nonce is a number only Used once in cryptography anons is highly popular whenever you're creating any kind of encryption scheme You'll use a nonce as a way to I mean, there's so many different use cases but in this case is gonna verify that a miner has indeed tried out many different solutions before offering the proof of computational work So we have a nonce and then we have a guess and the guess is this is gonna store our guests Which effectively it begins at if infinity? M throttle is gonna be equivalent to the Bitcoin difficulty. We'll just have some measure of how difficult we want our proof of work algorithm to be let's just say you know a Million and the target is going to be the maximum value That of eight bytes divided by the difficulty right so it's gonna be the maximum value By the difficulty So the nonce is going to increment by one each time and it's gonna count How many guesses the computer makes? so whenever the system does work it doesn't have to trace back over each guess it only checks the non submitted with the data, so Now what we can do is we can create this payload hash alright by using hash live so we're gonna hash the payload right so that is the message and that is the timestamp payload hash by using hash live sha-512 Take the payload and then digest it Right and now we're gonna start clocking this proof-of-work algorithm by saying start time. We've got our payload We've hashed it now. Let's run the proof of work algorithm So this is a simple loop that I'm gonna write, and this is the proof of work algorithm So I'm gonna say while the guess is greater than the target Let's increment the nonce by one every time and then We're gonna hash our data with two rounds of sha-256, okay, so we're gonna say guess unpack let me just write this out because it's quite a Quite a lot to write out, so we've got hash like Sha-512 and now we're gonna. Hash it again Oh man, right? Hash libed sha-512 and then Again we're gonna hash a hash leidos sha-512, and then we're gonna pack this Greater than Q. Which is a nonce? Plus the payload hash dot digest Dot digest because it was two hashes and then between zero and eight Right so we want the first bytes and the first eight bytes So that's why we said zero to eight and they're gonna be turned into a number effectively This is how a computer makes a guess? So the nonce is going to prove that it that work was done and let me at the end of this let me write out and we could end because we've we've ended the the Timer right so the nonce is going prove that work was done and in this value in this example its value represents the number of attempts our CPU made before it founded valid gas below the target and Because each guess has the same probability of being under the target method of generating our nons It doesn't matter however simply incrementing the nonce is cheaper than generating a random number So that's why we're using a nots right so The nonce is going to count how many times We've we've we've made this guess right so we have some Red so remember our guess is this number right here, and as long as that number is greater than our target This is just going to keep going right and the nonce is our way of of saying that yes, this this miner has indeed Computed all of these different solutions before arriving at this at this at this end solution and so at the end we can print out All of all of our variables to see what has happened here, so That's it for the proof of work, and so yeah, so that's proof of work now Let's talk about proof of stake a little bit, so In proof of work the probability of mining a block is dependent on how much am I how much work is done by the mine So payouts become smaller and smaller for Bitcoin miners so the idea is that there will be less of an incentive to avoid a 51% attack because miners are gonna get paid less overtime because there is a limited supply of bitcoins out there And so and the other argument behind not having proof of work systems is that? Mining communities have become centralized right people have bought these giant computer farms Just to mine and it's getting centralized so one potential solution to proof of work is to use proof of stake so in proof of stake a person can mine depending on how many coins they already hold and the proof of state system makes any 51% attack more expensive so but when you when when a miner is going to vote on the validity of a transaction It's not about how much computing power. It's they have it's about how much stake they have in the network, which means literally how much money do they have And it's a more decentralized system so In general the proof of steak algorithm looks as follows and it's actually it's actually really hard to find code for the proof of steak algorithm or Really any kind of diagram, or I mean you can find code, but it's gonna be it's gonna be huge I mean like c++ just like 300 lines of code, so I'm just gonna. I just code the I just coded out the proof of work this time, but next time I'll write out the proof of steak and the others, but it's it's a very simple algorithm It's a very simple algorithm the blockchain keeps a keeps track of a set of validators also called miners in the proof of steak sense and Anyone can become a validator by sending a special type of transaction that locks up their coin into a deposit and the process of Creating and agreeing on new blocks is then done through a consensus algorithm that all the current validators can participate in So there are a lot of different ways to assign rewards to these validators who participate in the consensus algorithm right there's chain based proof of stake and there's Byzantine fault tolerant proof of stake which is a little too much to go into for one video But that is kind of the next up incoming mainstream Distributed consensus algorithm after proof of work because the argument is that proof of work is way, too computationally expensive it wastes way too much electricity and proof of state can be made better aetherium for example the second highest market cap Cryptocurrency plans on moving from proof of work to proof of stake next year But we'll see how that works because proof of work as expensive as it is as Unnecessarily wasteful as it is it is still the only known Distributed consensus algorithm that can work at the scale that Bitcoin has become at so we'll see how it goes There's also a proof of Activity which is kind of a hybrid of proof of work and proof of stake the idea is that it starts off as a proof of work algorithm with all the miners trying to solve this mathematical problem, and then Once there is a winning Transaction it then switches to proof of stake so then that set up validators are going to be chosen to sign this new block There's also proof of burn which is all about you burning coins by sending them to an address where they are Irretrievable right so once you commit your coins to never-never-land Right this place where you can never get this get them back You earn a lifetime privilege of being able to mine on a system based on a random selection? process and over time your stake in the system decays so you'll want to Continually burn more coins to increase your odds of being selected in this lottery And they're there even more this proof of replication, which is what foul coin uses there's proof of there's proof of Storage there's proof of Intelligence which is way out there, which I'll get to at some point There's a lot of different proofs and these proofs can be distributed, but it's also useful to think about proofs just in a general sense right so on a distributed consensus proof of algorithm that I would most want to see It's called proof of unique identity what that means is can you prove that you are a unique person? In a decentralized way we already know how to do this in a centralized way right like a government or any kind of central entity can store your ID right like a government ID a social security number or whatever passport and they can say well mmm I've got this unique number for you. If you say you're him nope He's the only one right we know how to do that in a central centralized way But how do we do it in a decentralized way, and that hasn't been solved if we are able to solve proof of unique identity it removes the need to have a proof-of-work algorithm right because Miners are already proven to be unique so a civil attack is impossible gaining 51 percent of the network We can just say here are all the nodes that are unique 51 percent of them have to agree right so it can also help us create a basic income right a basic income coin Where you could just give out money to a set of unique nodes in a network There's so much. We could create online governments We could create so many different things, but we would just have to solve this proof of unique identity problem and but anyway Blockchains in a I go blockchain and AI go really well together, and if you were to ask me Suraj What what would you do as a startup or what would you do? If you were you know trying to build something really cool outside of making videos I would say I would combine Blockchain and AI to make an app that no one has ever made before and that's what it lets you do it lets you create Software that has never before been possible So if you're studying AI if you're studying machine learning if you're studying deep learning Know at least a little bit about the blockchain because I promise you it's going to come into your periphery It's gonna come into your line of sight very soon. If not now, okay, so I hope this video was helpful I hope it gave you some ideas and yeah
Info
Channel: Siraj Raval
Views: 49,553
Rating: undefined out of 5
Keywords: blockchain consensus algorithms and artificial intelligence, ai algorithms, consensus algorithms, blockchain artificial intelligence, consensus algorithm blockchain, consensus algorithm, blockchain consensus, blockchain consensus algorithm, ai and blockchain, artificial intelligence blockchain, blockchain and ai, blockchain ai, ai blockchain, ethereum, programming, coding, cryptocurrency, litecoin, consensus, distributed, computing, software, code, blockchain, algorithm, bitcoin, python
Id: 5Tr13l0O1Ws
Channel Id: undefined
Length: 31min 16sec (1876 seconds)
Published: Tue Oct 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.