How to build your own predictive market with deep learning on the blockchain #MAXhype
git clone https://github.com/daviddao/prediction-market-tutorial
Install homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install parity
brew tap paritytech/paritytech
brew install parity --stable
Install npm
brew install node
Install webpack
npm i -g webpack
cd path/to/prediction-market-tutorial
./init.sh
webpack --watch # will update everytime you change something
Inside the DApp folder, run:
# For Mac systems
ln -s $PWD/dist $HOME/Library/Application\ Support/io.parity.ethereum/dapps/mydapp
parity --jsonrpc-cors all # removes annoying access control allow origin issues
go to http://127.0.0.1:8180
and switch to your local development chain (otherwise your computer will download ethereum)
pragma solidity^0.4.19;
contract PredictionMarket {
uint chosen;
event Voted(address indexed who, uint indexed option);
function bet(uint _option) payable {
require(!hasVoted[msg.sender]);
bets[_option] += msg.value;
chose[msg.sender] = _option;
hasVoted[msg.sender] = true;
Voted(msg.sender, _option);
}
function release() public {
require(chose[msg.sender] == chosen);
msg.sender.transfer(bets[chosen] * 10);
}
function choose(uint _option) {
chosen = _option;
}
mapping (uint => uint) public bets;
mapping (address => bool) public hasVoted;
mapping (address => uint) public chose;
function die() {
selfdestruct(msg.sender);
}
}
Edit these lines with your uploaded address and abi
const address = '<contract address>';
const ABI = '<abi json>';
DeepLearnJS is a GPU accelerated library for deep learning in the front-end.
Import in React inside src/client/scripts/app.jsx
// Deep Learn utilities
import { Array3D, gpgpu_util, GPGPUContext, NDArrayMathCPU, NDArrayMathGPU } from 'deeplearn';
import { SqueezeNet } from 'deeplearn-squeezenet';
Inside App constructor, add following lines
// derp learning
this.gl = gpgpu_util.createWebGLContext(inferenceCanvas);
this.gpgpu = new GPGPUContext(this.gl);
this.math = new NDArrayMathGPU(this.gpgpu);
//this.math = new NDArrayMathCPU(); //try uncomment to see what happens
this.squeezeNet = new SqueezeNet(this.math);
Load the model inside the derplearn predict attribute
<DerpLearn
predict={
() => {
this.randomGenerator();
this.squeezeNet.load().then(() => {
const y = this.inference()
});
}}
/>
async inference() {
// Preprocessing
randomCatOrDog.width = 227; randomCatOrDog.height = 227;
randomCatOrDog.style.width = '227px'; randomCatOrDog.style.height = '227px';
// Prediction
const logits = this.squeezeNet.predict(Array3D.fromPixels(randomCatOrDog));
const topClassesToProbs = await this.squeezeNet.getTopKClasses(logits, 5);
for (const className in topClassesToProbs) {
console.log(
`${topClassesToProbs[className].toFixed(5)}: ${className}`);
console.log(className);
this.resolve(className);
break;
}
}
resolve(className) {
if (className == "Siberian husky") {
this.predictionMarket.choose(0);
} else {
this.predictionMarket.choose(1);
}
}