在tensorflow.js中,如何计算模型输入的梯度?

时间:2020-10-23 08:55:17

标签: tensorflow tensorflow.js

我想计算TensorFlow.js中相对于输入向量的损耗梯度。

这是我尝试过的:

function f(img) {
  return tf.metrics.categoricalCrossentropy(model.predict(img), lbl);
  // (Typo: the order of arguments should be flipped, but it does not affect the question here)
}

var g = tf.grad(f);

g(img).print();

img是形状为[1,784]的张量。 lbl是形状为[1,10]的张量。 model是经过tf.Sequential训练的香草MNIST DNN。

g(img)的调用因堆栈跟踪而失败:

Uncaught TypeError: Cannot read property 'shape' of undefined
    at gradFunc (Concat_grad.js:29)
    at Object.s.gradient (engine.js:931)
    at a (tape.js:158)
    at tape.js:136
    at engine.js:1038
    at engine.js:433
    at e.t.scopedRun (engine.js:444)
    at e.t.tidy (engine.js:431)
    at e.t.gradients (engine.js:1033)
    at gradients.js:69

我想念什么?

2 个答案:

答案 0 :(得分:1)

我的原始代码段正确; TensorFlow.js的2.6.0和2.5.0版本中有一个tf.grad bug导致了此错误。

该代码按2.4.0或新版本2.7.0中的预期工作。

答案 1 :(得分:0)

通过将model.predict排除在f范围之外,tf.grad将起作用。

function f(img) {
  return tf.metrics.categoricalCrossentropy(img, lbl);
}


var g = tf.grad(f);
const output = model.predict(img);

g(output).print();

版本2.7 +中已修复的顺序模型似乎有错误

相关问题