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

三世轮回 提交于 2019-12-10 10:23:58

问题


I used tf.expandDims() to add dimensions. Since I'm able to go into model.fit(), but got stuck due to this error Cannot start training because another fit() call is ongoing. and Cannot read property 'length' of undefined. You can find my code here

// 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);
            })
            }

回答1:


When multiple fit are called on the same model, they have to be done sequentially. It means that the second call has to start only when the first one has completed. Using async and await will prevent your second call to happen unless the first has completed.

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



回答2:


you may use this code,

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);
        })
        }


来源:https://stackoverflow.com/questions/54899687/tensorflowjs-exception-cannot-start-training-because-another-fit-call-is-ong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!