TensorFlow JS 异常 - 无法开始训练,因为正在进行另一个 ft() 调用

TensorFlowJS Exception - Cannot start training because another fit() call is ongoing

我使用了 tf.expandDims() 添加维度。由于我能够进入 model.fit(),但由于此错误而卡住了 无法开始训练,因为另一个 fit() 调用正在进行中。无法读取未定义的 属性 'length'。 你可以在这里找到我的代码

// Train the model using the data. 
let tesnor_dim =[];
let tensr;for(var j=0; j<2; j++){ 
console.log('resize_image',resize_image);
tensr = tf.expandDims(ysarr[j], 0); 
tesnor_dim.push(tensr);
console.log('tesnor_dim',tesnor_dim);
    model.fit(resize_image[j], tesnor_dim[j], {epochs: 100}).then((loss) => {
         console.log('resize_image[j]',resize_image[j]);
         console.log('tesnor_dim[j]',tesnor_dim[j]);
         console.log('loss',loss);
         const t = model.predict(resize_image[j]);
         console.log('Prediction:::'+t);
         pred = t.argMax(1).dataSync(); // get the class of highest probability
                const labelsPred = Array.from(pred).map(e => setLabel[e])
                console.log('labelsPred:::'+labelsPred);
                //const saveResults = model.save('downloads://my-model-1');
                //console.log(saveResults);
            }).catch((e) => {
                console.log(e.message);
            })
            }

当在同一个模型上调用多个 fit 时,它们必须按顺序完成。这意味着第二个调用必须仅在第一个调用完成后才开始。使用 asyncawait 将阻止您的第二次调用,除非第一个调用已完成。

loss = await model.fit(resize_image[j], tesnor_dim[j], {epochs: 100})
// continue rest of processing

您可以使用此代码,

await model.fit(resize_image[j], tesnor_dim[j], {epochs: 100}).then((loss) => {
     console.log('resize_image[j]',resize_image[j]);
     console.log('tesnor_dim[j]',tesnor_dim[j]);
     console.log('loss',loss);
     const t = model.predict(resize_image[j]);
     console.log('Prediction:::'+t);
     pred = t.argMax(1).dataSync(); // get the class of highest probability
            const labelsPred = Array.from(pred).map(e => setLabel[e])
            console.log('labelsPred:::'+labelsPred);
            //const saveResults = model.save('downloads://my-model-1');
            //console.log(saveResults);
        }).catch((e) => {
            console.log(e.message);
        })
        }