Tensorflow.js 使用打字稿的示例中的等待错误

Error with await in Tensorflow.js examples using typescript

从这个官方 tensorflow.js 文档中,我用 typescript 尝试了这个例子 tensorflow.js

我尝试 运行 来自 tensorflow.js 文档的这个例子,但是 await 给我这个错误。

我该怎么办?

如果我删除 await 损失属性未定义

如果我尝试将 async 添加到模型变量中,我会收到更多错误。 那么我该如何处理这种错误呢?

错误信息。

> ERROR in ./src/app/classes/ExperimentModels.ts
Module parse failed: The keyword 'yield' is reserved (158:22)
You may need an appropriate loader to handle this file type.
|         model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });
|         for (let i = 0; i < 5; i++) {
|             const h = yield model.fit(tf.ones([8, 10]), tf.ones([8, 1]), {
|                 batchSize: 4,
|                 epochs: 3
webpack: Failed to compile.

ERROR in src/app/classes/ExperimentModels.ts(229,22): error TS1308: 'await' 
expression is only allowed within an async function.

代码

    const model = tf.sequential({
      layers: [ tf.layers.dense({ units: 1, inputShape: [10] })]
    });

    model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });

    for (let i = 0; i < 5; i++) {
      const h: any = await model.fit(tf.ones([8, 10]), tf.ones([8, 1]), {
        batchSize: 4,
        epochs: 3
      });
      console.log(`Loss after Epoch ${i} : ${h.history.loss[0]}`);
    }

async 函数定义 的修饰符。您必须将 ex_fit 定义为异步,而不是对 tf.sequential:

的调用
async ex_fit() { ... }

另外,显然你在第 158 行中有另一个错误 yield。你也可以 post 吗?