tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

onBatchEnd callback batch count

Open StarryJapanNight opened this issue 5 years ago • 5 comments

When calling onBatchEnd with a batchSize = 1, and outputting the batch number, it counts to a number ~5x less than the number of input samples each epoch. I have confirmed the input tensor shape is correct.


const model = tf.sequential();

model.add(tf.layers.lstm({ units: numLSTMCells, batchInputShape: [batchSize, windowSize, numInputs], returnSequences: true, dropout: 0.2, recurrentDropout: 0.2 }));
model.add(tf.layers.lstm({ units: numLSTMCells, dropout: 0.2, recurrentDropout: 0.2 }));
model.add(tf.layers.dense({ units: numOutputs, activation: 'linear' }));

const loss = tf.losses.meanSquaredError;
const optimizer = tf.train.sgd(learningRate);
const metrics = 'mse';
model.compile({
    loss: loss,
    optimizer: optimizer,
    metrics: [metrics]
});

// Train model
const hist = await model.fit(x, y,
    {
        batchSize: batchSize,
        epochs: numEpochs,
        validationSplit: 0.8,
        callbacks: [tf.node.tensorBoard('/tmp/fit_logs' + logDir), new tf.CustomCallback({

            onEpochEnd: async (epoch, logs) => {

                // Save model weights after 100 epochs
                if (epoch % 100 == 0) {

                    // Save model
                    await model.save('file://C://tmp/lstm');


                    // Validate on unseen data
                    let outputValidPred = await makePredictions(model, inputValidTensor);
                    let expectedOuput = await outputValidTensor.array()

                    for (i = 0; i < outputValidPred.length; i++) {
                        console.log(outputValidPred[i], expectedOuput[i])
                    }

                }
            },

            onBatchEnd: async (batch, logs) => {
                console.log(batch)
            }

        })

        ]

    });

StarryJapanNight avatar Aug 20 '21 05:08 StarryJapanNight

Hi, @StarryJapanNight

Apologize for the delayed response and we're re-visiting our older issues and checking whether those issues got resolved or not as of now so May I know are you still looking for the solution or your issue got resolved ?

If issue still persists after trying with latest version of TFJs please let us know with error log and code snippet with defined variables values in the code snippet to replicate the same issue from our end ?

Could you please confirm if this issue is resolved for you ? Please feel free to close the issue if it is resolved ? Thank you!

gaikwadrahul8 avatar May 22 '23 20:05 gaikwadrahul8

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.

google-ml-butler[bot] avatar May 29 '23 21:05 google-ml-butler[bot]

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Jun 06 '23 02:06 github-actions[bot]

This issue was closed due to lack of activity after being marked stale for past 7 days.

github-actions[bot] avatar Jun 14 '23 02:06 github-actions[bot]

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Jun 14 '23 02:06 google-ml-butler[bot]