我想计算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
是接受训练的香草MNIST DNN tf.Sequential
。
调用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
我想念什么?
我的原始代码段是正确的;有一个tf.grad
错误在2.6.0和2.5.0版本导致此错误TensorFlow.js的。
该代码按2.4.0或新版本2.7.0中的预期工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句