Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- // Notice there is no 'import' statement. 'tf' is available on the index-page
- // because of the script tag above.
- var noise=[];
- for (var i = 0; i < images.length; i++) {
- noise.push(Math.random());
- }
- var predictionInput=[];
- for (var i = 0; i < (870/6)*(1236/6); i++) {
- predictionInput.push(Math.random());
- }
- var c;
- var ctx;
- const xs = tf.tensor2d(noise,[images.length/((870/6)*(1236/6)), (870/6)*(1236/6)]);
- const ys = tf.tensor2d(images, [images.length/((870/6)*(1236/6)), (870/6)*(1236/6)]);
- var time;
- var epochs;
- var epochPos=0;
- var model;
- var m = model;
- var save
- var stepSize = 0.1;
- window.onload=()=>{
- var c = document.querySelector("#canvas")
- var ctx = c.getContext("2d");
- // Define a model for linear regression.
- model = tf.sequential();
- model.add(tf.layers.dense({units: 256, inputDim:(870/6)*(1236/6), activation:"relu"}));
- model.add(tf.layers.dense({units: 512, activation:"relu"}));
- model.add(tf.layers.dense({units: 512, activation:"relu"}));
- model.add(tf.layers.dense({units: 256, activation:"relu"}));
- model.add(tf.layers.dense({units: (870/6)*(1236/6), activation:"elu"}));
- // Prepare the model for training: Specify the loss and the optimizer.
- model.compile({loss: 'meanSquaredError', optimizer: tf.train.adam(stepSize)});
- // Generate some synthetic data for training.
- //const xs = tf.tensor2d(images.slice(0,images.length-((870/6)/6*(1236/6)/6)), [68, (870/6)/6*(1236/6)/6]);
- //console.log(xs);
- //xs.print()
- //const ys = tf.tensor2d(images.slice((870/6)/6*(1236/6)/6,images.length), [68, (870/6)/6*(1236/6)/6]);
- //ys.print()
- document.querySelector("#steps").onchange=(e) => {
- stepSize = e.target.value;
- model.compile({loss: 'meanSquaredError', optimizer: tf.train.adam(stepSize)});
- };
- document.querySelector("#save").onclick=async() => {
- save = await model.save('downloads://my-model-1');
- };
- document.querySelector("#begin").onclick=()=>{
- console.log(tf.getBackend());
- epochs = document.querySelector("#epochs").value
- // Train the model using the data.
- train(model).then((res) => {
- //m = res.model;
- epochPos+=epochs;
- console.log(res.history.loss);
- // Use the model to do inference on a data point the model hasn't seen before:
- // Open the browser devtools to see the output
- var prediction = model.predict(tf.tensor2d(predictionInput, [1,(870/6)*(1236/6)]));
- //console.log(prediction);
- for(i=0;i<prediction.size;i++)
- {
- ctx.beginPath();
- 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))+")"
- //console.log(color);
- ctx.strokeStyle=color;
- //console.log(i%((870/6))+","+((i-(i%((870/6))))/((870/6))))
- ctx.moveTo(i%((870/6)), (i-(i%((870/6))))/((870/6)));
- ctx.lineTo(i%((870/6))+1, (i-(i%((870/6))))/((870/6)));
- ctx.stroke();
- }
- });
- }
- }
- async function train(mod){
- //console.log(epochs)
- return await mod.fit(xs, ys, {epochs: epochs,shuffle:true,initialEpoch:epochPos,callbacks:{
- onEpochBegin:()=>{time = new Date();},
- onEpochEnd:(num,logs)=>{
- console.log("Time: "+Math.abs(new Date() - time))
- console.log("Epochs: "+num+" of "+epochs+" ("+num/epochs*100+"%)")
- console.log("Loss: "+ logs.loss)
- console.log("Tensors: "+tf.memory().numTensors)
- }
- }});
- //await model.save('downloads://my-model-1');
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement