Sunteți pe pagina 1din 3

25/09/2016

ConvNetJS:DeepLearninginyourbrowser

ConvNetJS
DeepLearninginyourbrowser
Intro

DeepLearning
Resources

GettingStarted

Documentation

Introduction
Therearetwowaystousethelibrary:insidethebrowser,oronaserverusingnode.js.

InbrowserUse:SkeletonCodeandSetup
Youmightfindthissectionusefulifyouarerelativelynewtowebdevelopment.Thefastestwaytoobtainthe
libraryinaplugandplaywayifyoudon'tcareaboutdevelopingisthroughthislinktoconvnetmin.js,which
containstheminifiedlibrary.Alternatively,youcanalsochoosetodownloadthelatestreleaseofthelibraryfrom
Github.Thefileyouareprobablymostinterestedinisbuild/convnetmin.js,whichcontainstheentirelibrary.To
useit,createaabarebonesindex.htmlfileinsomefolderandcopybuild/convnetmin.jstothesamefolder.
Hereissomeexamplecodetogetyoustarted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

<html>
<head>
<title>minimaldemo</title>

<!CSSgoeshere>
<style>
body{
backgroundcolor:#FFF;/*example...*/
}
</style>

<!importconvnetjslibrary>
<scriptsrc="convnetmin.js"></script>

<!javascriptgoeshere>
<scripttype="text/javascript">

functionperiodic(){
vard=document.getElementById('egdiv');
d.innerHTML='Randomnumber:'+Math.random()
}

varnet;//declaredoutside>globalvariableinwindowscope
functionstart(){
//thisgetsexecutedonstartup
//...
net=newconvnetjs.Net();
//...

//exampleofrunningsomethingevery1second
setInterval(periodic,1000);
}

</script>
</head>

<bodyonload="start()">
<divid="egdiv"></div>

39
</body>
http://cs.stanford.edu/people/karpathy/convnetjs/started.html

1/3

25/09/2016

39
40

ConvNetJS:DeepLearninginyourbrowser

</body>
</html>

Here,weareplacingbothCSSandJavascriptintoheadofthehtmlfile,butnormallypeopleliketoseparate
theseoutintodifferentfiles.Notehowthebodytagcontainsanonloadattributewhichexecutes
thestart()methodoncethefileisloaded:Hereyoucaninitializeeverything(includingyournetwork).The
functionperiodic()iswireduptoexecuteevery1000ms.
IrecommendyouuseChromeasyourbrowser.Draganddropyourindex.htmlintotheaddressbartoloadup
yourlocalhtmlfile!Irecommendalwayskeepingyourconsoleopen:rightclick,choose"InspectElement"and
gotothe"Console"tab.Youcanwritetothisusingconsole.log("test").
Insomecases,ifyouaretryingtoloadimagesorotherdatadynamically,youmightrunintoissueswithrunning
localhtmlfilesandcrossoriginpolicies.Forexample,theMNISTorCIFARdemoswillnotworklocallybecause
theyloadimagesdynamically.Asimpleworkaroundistorunadummylocalwebserverinyourfolder.On
Ubuntuforexample,cdintoit,startupone:pythonmSimpleHTTPServer,andthennavigatetothelocal
addressthatpythonprintsforyouinyourbrowsertoseeyourfilesinthatfolder.

Example:NeuralNetClassification
Inaclassificationsettingthenetworkisaskedtoprovideapredictionamongafixedsetofdistinctclasses.Lets
createasimple2layerneuralnetworkbinaryclassifier(i.e.twodistinctclasses)thattakes2dimensionaldata
points.Thefirstlayerofeverynetworkmustbean'input'layerinwhichwedeclarethesizeoftheinputdata.
ConvNetJSlayersarebasedonVolclassthatrepresentsa3dimensionalvolumeofnumbers.The3
dimensionsare(sx,sy,depth),butifyou'renotworkingwithimageswewillalwayskeepsx=1,sy=1,andonly
worryaboutdepth.Therefore,wewilldeclarethesizeofinputvolumetobe1x1x2(out_sx=1,out_sy=1,
out_depth=2).Thenextthreelayerswillbefullyconnectedlayers('fc'forshort)ofneurons,andthelastlayer
willbeaclassiferlayer(called'softmax')whichoutputsprobabilities.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

varlayer_defs=[];
//inputlayerofsize1x1x2(allvolumesare3D)
layer_defs.push({type:'input',out_sx:1,out_sy:1,out_depth:2});
//somefullyconnectedlayers
layer_defs.push({type:'fc',num_neurons:20,activation:'relu'});
layer_defs.push({type:'fc',num_neurons:20,activation:'relu'});
//asoftmaxclassifierpredictingprobabilitiesfortwoclasses:0,1
layer_defs.push({type:'softmax',num_classes:2});

//createanetoutofit
varnet=newconvnetjs.Net();
net.makeLayers(layer_defs);

//thenetworkalwaysworksonVol()elements.Theseareessentially
//simplewrappersaroundlists,butalsocontaingradientsanddimensions
//linebelowwillcreatea1x1x2volumeandfillitwith0.5and1.3
varx=newconvnetjs.Vol([0.5,1.3]);

varprobability_volume=net.forward(x);
console.log('probabilitythatxisclass0:'+probability_volume.w[0]);
//prints0.50101

Soweseethatthenetwork(whichisinitializedrandomly)assignsprobability50.1%tothepoint[0.5,1.3]being
class0.Sincesoftmaxensuresthatprobabilitiesalwayssumtoone,ifyouprintedprobabilityofclass1,you
wouldget0.49899,orroughly49.9%.Notethatconvnetjs.Volisa3Dvolumeclassthattheentirelibraryis
basedon.Itisaverythinwrapperaroundasimplearrayofnumbers(field.w),butinadditionstoresthe
dimensionsofthevolume:sx(width),sy(height),anddepth,andalsothegradients(field.dw).Insimplenon
convolutionalnetworks,thewidthandheightarealwayssimplykeptat1.
Letsnowactuallyprovidethisasdatatothenetwork,sayingxshouldinfactmapto0withahighprobability.
WewilluseabuiltinTrainerclass:
1
2

vartrainer=newconvnetjs.Trainer(net,{learning_rate:0.01,l2_decay:0.001});
trainer.train(x,0);

http://cs.stanford.edu/people/karpathy/convnetjs/started.html

2/3

25/09/2016

3
4
5
6

ConvNetJS:DeepLearninginyourbrowser

varprobability_volume2=net.forward(x);
console.log('probabilitythatxisclass0:'+probability_volume2.w[0]);
//prints0.50374

Thetrainertakesawholebunchof(optional)parameters,butfornowjustnoticethatoncewebackpropagated
theinformationthatxisinfactclass0,thenetworkadjustsitsparameterstomakethatmorelikely(0.50374,up
from0.50101).Totrainonanactualdataset,yousimplyloopthroughallyourpointsatrandomandrepeatedly
backpropagatetheirtrueclassthroughthenetwork,whichgraduallyadjustsitsweightstomakeyour
predictionsmorelikely.

Example:NeuralNetRegression
Regressionisthetaskofpredictingrealvaluedoutputs,notjustprobabilitiesofafixednumberofclasses.For
example,wecouldbepredictingtheheightofaperson.Tosetuparegressionnetwork,weproceedverysimilar
totheexampleabove,butreplacethesoftmax(classifier)witharegressionlosslayer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

varlayer_defs=[];
layer_defs.push({type:'input',out_sx:1,out_sy:1,out_depth:2});
layer_defs.push({type:'fc',num_neurons:5,activation:'sigmoid'});
layer_defs.push({type:'regression',num_neurons:1});
varnet=newconvnetjs.Net();
net.makeLayers(layer_defs);

varx=newconvnetjs.Vol([0.5,1.3]);

//trainonthisdatapoint,saying[0.5,1.3]shouldmaptovalue0.7:
//notethatinthiscasewearepassingitalist,becauseingeneral
//wemaywanttoregressmultipleoutputsandinthisspecialcasewe
//usednum_neurons:1fortheregressiontoonlyregressone.
vartrainer=newconvnetjs.SGDTrainer(net,
{learning_rate:0.01,momentum:0.0,batch_size:1,l2_decay:0.001});
trainer.train(x,[0.7]);

//evaluateonadatapoint.Wewillgeta1x1x1Volback,sowegetthe
//actualoutputbylookingintoits'w'field:
varpredicted_values=net.forward(x);
console.log('predictedvalue:'+predicted_values.w[0]);

Veryimportantly,noticethattheregressionlossalwaysexpectsaLISTwhenyoucallthetrainmethod(e.g.
[0.7])inthiscase.Itisacommonmistaketoinsteadpassinasinglenumber,suchas0.7alone.

Example:TrainingaConvolutionalNetworkforImages
FornowseeeithertheCIFAR10orMNISTdemos.TheyreadimagesandprocessthemwithConvNetJS.
TherearenosimpleplugandplayfunctionsinthelibrarytoplugimagesintoConvNetsJS,socurrentlyitis
requiredtoreadthroughthecodeandunderstandhowthispartworks.

Example:TraininingaReinforcementLearningAgent
FornowseethedocumentationlinkandtheReinforcementLearningDemo

http://cs.stanford.edu/people/karpathy/convnetjs/started.html

3/3

S-ar putea să vă placă și