Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var tf = require('@tensorflow/tfjs');
  2.  
  3. const express = require('express');
  4. const bodyParser = require('body-parser');
  5.  
  6. app.use(bodyParser.json());
  7. app.use(bodyParser.urlencoded({ extended: true }));
  8.  
  9. app.listen(3000, () => console.log('WebService run...'));
  10.  
  11. const model = tf.sequential();
  12. model.add(tf.layers.dense({units: 10, activation: 'sigmoid',inputShape: [2]}));
  13. model.add(tf.layers.dense({units: 1, activation: 'sigmoid',inputShape: [10]}));
  14.  
  15. model.compile({loss: 'meanSquaredError', optimizer: 'rmsprop'});
  16.  
  17. const training_data = tf.tensor2d([[0,0],[0,1],[1,0],[1,1]]);
  18. const target_data = tf.tensor2d([[0],[1],[1],[0]]);
  19.  
  20. for (let i = 1; i < 100 ; ++i) {
  21.  var h = await model.fit(training_data, target_data, {epochs: 30});
  22.    console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
  23. }
  24.  
  25. app.post('/test',function(req,res){
  26.   var data_to_test = req.body.data_to_test;
  27.  
  28.   console.log("data_to_test= "+data_to_test+);
  29.  
  30.   var predict = model.predict(data_to_test).print();
  31.  
  32.   res.end(predict);
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement