Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. Layer (type) Output shape Param #
  2. =================================================================
  3. dense_Dense1 (Dense) [null,1] 2
  4. =================================================================
  5. Total params: 2
  6. Trainable params: 2
  7. Non-trainable params: 0
  8.  
  9. const tf = require("@tensorflow/tfjs-node")
  10.  
  11. function convert(c){
  12. return (c*1.8)+32 // Convert celsius to fahrenheit
  13. }
  14.  
  15. var celsius = []
  16. var fahrenheit = []
  17.  
  18. for (let i = 0; i < 20; i++) {
  19. var r = 100; // Keeping this only value to ensure that Tf knows the answer I also have tried with 20 different values but doesn't work
  20. celsius.push([r]) // Shape [20,1]
  21. fahrenheit.push([convert(r)]) // Push the answer (212) to the fahrenheit array
  22. }
  23.  
  24. var model = tf.sequential();
  25. model.add(tf.layers.dense({inputShape:[1], units: 1}))
  26.  
  27. async function trainModel(model, inputs, labels) {
  28. // Prepare the model for training.
  29. model.compile({
  30. optimizer: tf.train.adam(),
  31. loss: tf.losses.meanSquaredError,
  32. metrics: ['accuracy'], // Accuracy = 0
  33. });
  34.  
  35. model.summary();
  36.  
  37. const epochs = 500;
  38.  
  39. return await model.fit(inputs, labels, {
  40. epochs,
  41. batchSize: 20,
  42. verbose: false // Nothing interesting with verbose
  43. });
  44. }
  45.  
  46. c = tf.tensor(celsius)
  47. f = tf.tensor(fahrenheit)
  48.  
  49. var training = trainModel(model, c, f)
  50.  
  51. training.then(function(args){
  52. var prediction = model.predict(tf.tensor([[100]]));
  53. prediction.print(); // Prints a random number
  54. console.log("Real answer = "+convert(100))
  55. })
  56.  
  57. Tensor
  58. [[65.9411697],]
  59. Real answer = 212
  60.  
  61. const tf = require("@tensorflow/tfjs-node")
  62. const nr_epochs=500;
  63.  
  64. function convert(c){
  65. return (c*1.8)+32 // Convert celsius to fahrenheit
  66. }
  67.  
  68.  
  69. var celsius = []
  70. var fahrenheit = []
  71.  
  72. for (let i = 0; i < 100; i++) {
  73. var r = 100; // Keeping this only value to ensure that Tf knows the answer
  74. celsius.push(i) // Shape [20,1]
  75. fahrenheit.push(convert(i)) // Push the answer (212) to the fahrenheit array
  76. }
  77.  
  78. let train = async (xy, ys) => {
  79. const model = tf.sequential();
  80.  
  81. model.add(tf.layers.dense({units: 1, inputShape: [1]}));
  82.  
  83. model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
  84. await model.fit(xs,ys,{epochs: nr_epochs})
  85. return model;
  86. }
  87.  
  88. let predict = (model, n) => {
  89. const predicted = model.predict(tf.tensor2d([n],[1,1]));
  90. return predicted;
  91. }
  92.  
  93. const xs = tf.tensor2d(celsius.slice (0,15), [15,1]);
  94. const ys = tf.tensor2d(fahrenheit.slice (0,15), [15,1]);
  95. (async () => {
  96. let trained = await train (xs,ys);
  97. for (let n of [4,6,12]) {
  98. let predicted = predict (trained, n).dataSync ();
  99. console.log (`Value: ${n} Predicted: ${predicted [0]}`)
  100. }
  101. })()
  102.  
  103. Value: 4 Predicted: 38.01055908203125
  104. Value: 6 Predicted: 42.033267974853516
  105. Value: 12 Predicted: 54.101402282714844
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement