Advertisement
Guest User

Filter Generator Tuning Patch

a guest
May 19th, 2017
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.14 KB | None | 0 0
  1. diff --git a/code.js b/code.js
  2. index 346f3af..6868483 100644
  3. --- a/code.js
  4. +++ b/code.js
  5. @@ -1,4 +1,5 @@
  6.  "use strict";
  7. +const LOG_DEBUG = process.argv.includes("--debug");
  8.  
  9.  class Color {
  10.      constructor(r, g, b) { this.set(r, g, b); }
  11. @@ -111,6 +112,7 @@ class Solver {
  12.  
  13.      solve() {
  14.          let result = this.solveNarrow(this.solveWide());
  15. +        if(LOG_DEBUG) { console.log("-----------"); }
  16.          return {
  17.              values: result.values,
  18.              loss: result.loss,
  19. @@ -149,6 +158,8 @@ class Solver {
  20.          let highArgs = new Array(6);
  21.          let lowArgs = new Array(6);
  22.  
  23. +        if(LOG_DEBUG) { console.log(`\nRunning SPSA for ${iters} iterations. α=${alpha} γ=${gamma} A=${A} a=${a} c=${c} θ=${values}`); }
  24. +
  25.          for(let k = 0; k < iters; k++) {
  26.              let ck = c / Math.pow(k + 1, gamma);
  27.              for(let i = 0; i < 6; i++) {
  28. @@ -166,6 +177,8 @@ class Solver {
  29.  
  30.              let loss = this.loss(values);
  31.              if(loss < bestLoss) { best = values.slice(0); bestLoss = loss; }
  32. +
  33. +            if(LOG_DEBUG && k % 10 === 0) { console.log(`Iteration ${k + 1}. Loss: ${loss.toFixed(3)} ${this.css(values)}`); }
  34.          } return { values: best, loss: bestLoss };
  35.  
  36.          function fix(value, idx) {
  37. @@ -206,4 +219,21 @@ class Solver {
  38.          function fmt(idx, multiplier = 1) { return Math.round(filters[idx] * multiplier); }
  39.          return `filter: invert(${fmt(0)}%) sepia(${fmt(1)}%) saturate(${fmt(2)}%) hue-rotate(${fmt(3, 3.6)}deg) brightness(${fmt(4)}%) contrast(${fmt(5)}%);`;
  40.      }
  41.  }
  42. +const TEST_ITERATIONS = 2000;
  43. +let color = new Color(0, 0, 0);
  44. +let sumLoss = 0;
  45. +let start = Date.now();
  46. +function randRGB() { return Math.floor(Math.random() * 256); }
  47. +
  48. +for(let i = 0; i < TEST_ITERATIONS; i++) {
  49. +    color.set(randRGB(), randRGB(), randRGB());
  50. +    if(LOG_DEBUG) { console.log("Color: " + color.toString()); }
  51. +
  52. +    let solver = new Solver(color);
  53. +    let result = solver.solve();
  54. +    sumLoss += result.loss;
  55. +}
  56. +console.log("Average loss: " + sumLoss / TEST_ITERATIONS);
  57. +console.log("Average time: " + (Date.now() - start) / TEST_ITERATIONS + "ms");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement