Getting loss from each sample in CNN (python) [closed]

I have to plot a cumulative density graph for the performance of my CNN model.

But the problem is to do that, I have to get all the losses for each sample in the validation set but it seems like tensorflow only shows the final losses for each epoch.

Is there any way I can get all the losses for all the samples?

You may use Keras model which is provided in TensorFlow. Keras model lets you evaluate a batch of different number of samples compared to when training.

For example, when training:

myModel.fit(x=X, y=Y, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=1)

Evaluate every single sample to get output loss of each sample (https://www.tensorflow.org/api_docs/python/tf/keras/Model#evaluate):

losses = []

for i in range(len(X)):
sampleX = X[i]
sampleY = Y[i]
loss = myModel.evaluate(x=[sampleX], y=[sampleY], batch_size=1, steps=1, verbose=0)
losses.append(loss)

Another way is to use Keras callback during multiple sample evaluation (https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/Callback#on_batch_end):

losses = []

class myCallback(tf.keras.callbacks.Callback):
def on_batch_end(batch,log):
global losses
losses.append(log["loss"])

myModel.evaluate(x=X, y=Y, batch_size=1, steps=NUM_SAMPLES, callbacks=[myCallback()])
$$`$$