首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Tensorflowjs - optimize.minimize找不到任何变量和损失函数的结果之间的联系

Tensorflowjs - optimize.minimize找不到任何变量和损失函数的结果之间的联系
EN

Stack Overflow用户
提问于 2018-05-28 18:03:28
回答 1查看 421关注 0票数 0

我正在尝试将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)的结果之间的联系。请确保使用变量的操作位于传递给minimize()的函数f中。

但是如果我像这样改变我的预测函数,它会起作用:

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错误?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 20:07:04

我设法让我的代码在没有内存泄漏的情况下工作,方法是在将ys变量赋值给tf.add函数的结果之前手动处理它。

这是我的工作解决方案

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));
}
<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>

我不确定这些是否是bug:

  • 使用ys = tf.add()创建一个新的张量,该张量不是由

()处理的

  • 使用ys.assign(tf.add())阻止optimizer.minimize()函数查找与变量

的关系

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50563814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档