Getting loss from each sample in CNN (python)
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[&quot;loss&quot;])

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