Tensorflowjs - optimize.minimize无法找到任何变量与损失函数的结果之间的连接

时间:2018-05-28 10:03:28

标签: javascript tensorflow tensorflow.js

我试图使用Daniel Shiffman的线性回归例子和tensorflowjs(https://www.youtube.com/watch?v=NZR-N_dhK2M)来使用多项式方程而不是线性方程。但我正在努力预测功能。 在我的第一个版本(见下文)中,optimize.minimze函数找不到我的函数和我的tf.variables之间的链接(存储在我的系数数组中)。 另一方面,我的第二个版本工作,但有一个我无法修复的内存泄漏

这是非工作版本:

const WIDTH = 800, HEIGHT = 400;

const x_vals = [];
const y_vals = [];

let coefficients = [];
let degree = 5;

let lr = 0.2;
let optimizer = tf.train.adamax(lr);

function setup() {
  createCanvas(WIDTH, HEIGHT);
  background(0);
  initCoeffs();

  let up = false;
  for (let i = 0; i < WIDTH; i += WIDTH / 10) {
    x_vals.push(map(i, 0, WIDTH, -1, 1));
    y_vals.push(map((up) ? 0 : HEIGHT, 0, HEIGHT, -1, 1));
    up = !up;
  }
}

function initCoeffs() {
  for (let i = 0; i < degree; i++)
    coefficients.push(tf.variable(tf.scalar(random(1))));
}

function loss(pred, labels) {
  return tf.losses.meanSquaredError(labels, pred);
}

function predict(x) {
  const xs = tf.tensor1d(x);
  const ys = tf.variable(tf.zerosLike(xs));
  for (let i = 0; i < degree; i++) {
    const coef = coefficients[i];
    const pow_ts = tf.fill(xs.shape, degree - i);
    const sum = tf.add(ys, coef.mul(xs.pow(pow_ts)));
    ys.assign(sum);
  }
  ys.print();
  return ys;
}

function draw() {
  noFill();
  background(0);
  stroke(255);
  strokeWeight(8);
  for (let i = 0; i < x_vals.length; i++) {
    point(map(x_vals[i], -1, 1, 0, WIDTH), map(y_vals[i], -1, 1, 0, HEIGHT));
  }
  strokeWeight(4);

  if (x_vals.length > 0) {
    tf.tidy(() => {
      const ys = tf.tensor1d(y_vals);
      optimizer.minimize(() => loss(predict(x_vals), ys));
    });
  }


  let lineX = [];
  for (let x = -1.1; x <= 1.1; x += 0.01)
    lineX.push(x);
  const ys = tf.tidy(() => predict(lineX));
  let lineY = ys.dataSync();
  ys.dispose();

  beginShape();
  for (let i = 0; i < lineY.length; i++)
    curveVertex(map(lineX[i], -1, 1, 0, WIDTH), map(lineY[i], -1, 1, 0, HEIGHT));
  endShape();

  for (let i = 0; i < lineY.length; i++) {
    stroke(200, 100, 100);
    point(map(lineX[i], -1, 1, 0, WIDTH), map(lineY[i], -1, 1, 0, HEIGHT));
  }
}

function mousePressed() {
  x_vals.push(map(mouseX, 0, WIDTH, -1, 1));
  y_vals.push(map(mouseY, 0, HEIGHT, -1, 1));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.11.2/tf.min.js"></script>

如您所见,我在控制台中出现此错误:

  

无法找到任何变量与损失函数y = f(x)的结果之间的连接。请确保使用变量的操作在函数f中传递给minimize()。

但如果我像这样改变我的预测函数,它就可以了:

function predict(x) {
  const xs = tf.tensor1d(x);
  let ys = tf.variable(tf.zerosLike(xs));
  for (let i = 0; i < degree; i++) {
    const coef = coefficients[i];
    const pow_ts = tf.fill(xs.shape, degree - i);
    const sum = tf.add(ys, coef.mul(xs.pow(pow_ts)));
    ys = sum;
  }
  ys.print();
  return ys;
}

问题是第二个版本创建了内存泄漏,因为我使用let来声明我的ys tf.variable。

如何在没有optimize.minimizer错误的情况下修复代码以避免内存泄漏?

由于

1 个答案:

答案 0 :(得分:1)

通过在将ys变量赋值给tf.add函数的结果之前手动处理ys变量,我设法让我的代码无内存泄漏。

这是我的工作解决方案

&#13;
&#13;
const WIDTH = 800, HEIGHT = 400;

const x_vals = [];
const y_vals = [];

let coefficients = [];
let degree = 15;

let lr = 0.2;
let optimizer = tf.train.adamax(lr);

function setup() {
  createCanvas(WIDTH, HEIGHT);
  background(0);
  initCoeffs();

  let up = false;
  for (let i = 0; i < WIDTH; i += WIDTH / 10) {
    x_vals.push(map(i, 0, WIDTH, -1, 1));
    y_vals.push(map((up) ? 0 : HEIGHT, 0, HEIGHT, -1, 1));
    up = !up;
  }
}

function initCoeffs() {
  for (let i = 0; i < degree; i++)
    coefficients.push(tf.variable(tf.scalar(random(1))));
}

function loss(pred, labels) {
  return tf.losses.meanSquaredError(labels, pred);
}

function predict(x) {
  const xs = tf.tensor1d(x);
  let ys = tf.variable(tf.zerosLike(xs));
  for (let i = 0; i < degree; i++) {
    const coef = coefficients[i];
    const pow_ts = tf.fill(xs.shape, degree - i);
    const sum = tf.add(ys, coefficients[i].mul(xs.pow(pow_ts)));
    ys.dispose();
    ys = sum.clone();
  }
  return ys;
}

function draw() {
  noFill();
  background(0);
  stroke(255);
  strokeWeight(8);
  for (let i = 0; i < x_vals.length; i++) {
    point(map(x_vals[i], -1, 1, 0, WIDTH), map(y_vals[i], -1, 1, 0, HEIGHT));
  }
  strokeWeight(4);

  if (x_vals.length > 0) {
    tf.tidy(() => {
      const ys = tf.tensor1d(y_vals);
      optimizer.minimize(() => loss(predict(x_vals), ys), coefficients);
    });
  }


  let lineX = [];
  for (let x = -1.1; x <= 1.1; x += 0.01)
    lineX.push(x);
  const ys = tf.tidy(() => predict(lineX));
  let lineY = ys.dataSync();
  ys.dispose();

  beginShape();
  for (let i = 0; i < lineY.length; i++)
    curveVertex(map(lineX[i], -1, 1, 0, WIDTH), map(lineY[i], -1, 1, 0, HEIGHT));
  endShape();

  for (let i = 0; i < lineY.length; i++) {
    stroke(200, 100, 100);
    point(map(lineX[i], -1, 1, 0, WIDTH), map(lineY[i], -1, 1, 0, HEIGHT));
  }
  //console.log(tf.memory().numTensors);
}

function mousePressed() {
  x_vals.push(map(mouseX, 0, WIDTH, -1, 1));
  y_vals.push(map(mouseY, 0, HEIGHT, -1, 1));
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.11.2/tf.min.js"></script>
&#13;
&#13;
&#13;

我不确定这些是不是错误:

  • 使用ys = tf.add()创建一个未被tidy()处理的新张量
  • 使用ys.assign(tf.add())阻止optimizer.minimize()函数查找与变量的关系