Advertisement
Guest User

no gpu :-(

a guest
Aug 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.57 KB | None | 0 0
  1. <script>
  2.       // Notice there is no 'import' statement. 'tf' is available on the index-page
  3.       // because of the script tag above.
  4.       var noise=[];
  5.       for (var i = 0; i < images.length; i++) {
  6.         noise.push(Math.random());
  7.       }
  8.       var predictionInput=[];
  9.       for (var i = 0; i < (870/6)*(1236/6); i++) {
  10.         predictionInput.push(Math.random());
  11.       }
  12.  
  13.       var c;
  14.       var ctx;
  15.       const xs = tf.tensor2d(noise,[images.length/((870/6)*(1236/6)), (870/6)*(1236/6)]);
  16.       const ys = tf.tensor2d(images, [images.length/((870/6)*(1236/6)), (870/6)*(1236/6)]);
  17.       var time;
  18.       var epochs;
  19.       var epochPos=0;
  20.       var model;
  21.       var m = model;
  22.       var save
  23.       var stepSize = 0.1;
  24.       window.onload=()=>{
  25.  
  26.  
  27.           var c = document.querySelector("#canvas")
  28.           var ctx = c.getContext("2d");
  29.  
  30.  
  31.  
  32.           // Define a model for linear regression.
  33.           model = tf.sequential();
  34.           model.add(tf.layers.dense({units: 256, inputDim:(870/6)*(1236/6), activation:"relu"}));
  35.           model.add(tf.layers.dense({units: 512, activation:"relu"}));
  36.           model.add(tf.layers.dense({units: 512, activation:"relu"}));
  37.           model.add(tf.layers.dense({units: 256, activation:"relu"}));
  38.           model.add(tf.layers.dense({units: (870/6)*(1236/6), activation:"elu"}));
  39.  
  40.           // Prepare the model for training: Specify the loss and the optimizer.
  41.           model.compile({loss: 'meanSquaredError', optimizer: tf.train.adam(stepSize)});
  42.  
  43.           // Generate some synthetic data for training.
  44.           //const xs = tf.tensor2d(images.slice(0,images.length-((870/6)/6*(1236/6)/6)), [68, (870/6)/6*(1236/6)/6]);
  45.           //console.log(xs);
  46.           //xs.print()
  47.           //const ys = tf.tensor2d(images.slice((870/6)/6*(1236/6)/6,images.length), [68, (870/6)/6*(1236/6)/6]);
  48.           //ys.print()
  49.         document.querySelector("#steps").onchange=(e) => {
  50.             stepSize = e.target.value;
  51.             model.compile({loss: 'meanSquaredError', optimizer: tf.train.adam(stepSize)});
  52.         };
  53.         document.querySelector("#save").onclick=async() => {
  54.             save = await model.save('downloads://my-model-1');
  55.         };
  56.         document.querySelector("#begin").onclick=()=>{
  57.             console.log(tf.getBackend());
  58.             epochs = document.querySelector("#epochs").value
  59.  
  60.             // Train the model using the data.
  61.  
  62.             train(model).then((res) => {
  63.             //m = res.model;
  64.             epochPos+=epochs;
  65.             console.log(res.history.loss);
  66.             // Use the model to do inference on a data point the model hasn't seen before:
  67.             // Open the browser devtools to see the output
  68.             var prediction = model.predict(tf.tensor2d(predictionInput, [1,(870/6)*(1236/6)]));
  69.             //console.log(prediction);
  70.             for(i=0;i<prediction.size;i++)
  71.             {
  72.                 ctx.beginPath();
  73.                 var color = "rgb("+(prediction.get(0,i)*255-(prediction.get(0,i)*255%1))+","+(prediction.get(0,i)*255-(prediction.get(0,i)*255%1))+","+(prediction.get(0,i)*255-(prediction.get(0,i)*255%1))+")"
  74.                 //console.log(color);
  75.                 ctx.strokeStyle=color;
  76.                 //console.log(i%((870/6))+","+((i-(i%((870/6))))/((870/6))))
  77.                 ctx.moveTo(i%((870/6)), (i-(i%((870/6))))/((870/6)));
  78.                 ctx.lineTo(i%((870/6))+1, (i-(i%((870/6))))/((870/6)));
  79.                 ctx.stroke();
  80.             }
  81.  
  82.             });
  83.         }
  84.       }
  85.       async function train(mod){
  86.         //console.log(epochs)
  87.         return await mod.fit(xs, ys, {epochs: epochs,shuffle:true,initialEpoch:epochPos,callbacks:{
  88.             onEpochBegin:()=>{time = new Date();},
  89.             onEpochEnd:(num,logs)=>{
  90.                 console.log("Time: "+Math.abs(new Date() - time))
  91.                 console.log("Epochs: "+num+" of "+epochs+" ("+num/epochs*100+"%)")
  92.                 console.log("Loss: "+ logs.loss)
  93.                 console.log("Tensors: "+tf.memory().numTensors)
  94.             }
  95.         }});
  96.         //await model.save('downloads://my-model-1');
  97.     }
  98.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement