在 Angular 上使用 tensorflow 时无法编译片段着色器

failed to compile fragment shader when using tensorflow on Angular

我正在尝试在 angular 应用程序上实施迁移学习。对于新数据的收集,我想我将只使用与 tensorflow js 教程中完全相同的代码,即在 pacman 游戏中使用的代码。

这是 tensorflow.js 教程中 controller_dataset.js 的代码。我在 angular.

上的一个组件上导入了它
import * as tf from '@tensorflow/tfjs';


export class ControllerDataset {
    constructor(numClasses) {
      this.numClasses = numClasses;
    }


   addExample(example, label) {

    const y = tf.tidy(() => tf.oneHot(tf.tensor1d([label]).toInt(), this.numClasses));

    if (this.xs == null) {
      this.xs = tf.keep(example);
      this.ys = tf.keep(y);
    } else {
      const oldX = this.xs;
      this.xs = tf.keep(oldX.concat(example, 0));

      const oldY = this.ys;
      this.ys = tf.keep(oldY.concat(y, 0));

      oldX.dispose();
      oldY.dispose();
      y.dispose();
    }
  }
}

问题是当调用 controller.addExample 时会导致以下错误

ERROR Error: Failed to compile fragment shader.
at createFragmentShader (tf-core.esm.js:17)
at e.createProgram (tf-core.esm.js:17)
at compileProgram (tf-core.esm.js:17)
at tf-core.esm.js:17
at e.getAndSaveBinary (tf-core.esm.js:17)
at e.compileAndRun (tf-core.esm.js:17)
at e.oneHot (tf-core.esm.js:17)
at ENV.engine.runKernel.indices (tf-core.esm.js:17)
at e.runKernel (tf-core.esm.js:17)
at e.oneHot (tf-core.esm.js:17)

您的错误来自使用 this.numClasses。在你的 class 的构造函数中,你传入了参数 numClasses。但是当你的组件被调用时,没有定义参数 numClasses 。结果,this.numClasses 将是未定义的。

考虑为 this.numClasses 分配一个值

 addExample(example, label) {

    // set this.numClasses with a value 
    const y = tf.tidy(() => tf.oneHot(tf.tensor1d([label]).toInt(), this.numClasses));

 }