# Path: keras_image_captioning/losses.py
# def categorical_crossentropy_from_logits(y_true, y_pred):
#     # Discarding is still needed although CaptionPreprocessor.preprocess_batch
#     # has added dummy words as all-zeros arrays because the sum of losses is
#     # the same but the mean of losses is different.
#     y_true = y_true[:, :-1, :]  # Discard the last timestep/word (dummy)
#     y_pred = y_pred[:, :-1, :]  # Discard the last timestep/word (dummy)
# 
#     loss = tf.nn.softmax_cross_entropy_with_logits(labels=y_true,
#                                                    logits=y_pred)
#     return loss


import numpy as np
import pytest
import tensorflow as tf
from .losses import categorical_crossentropy_from_logits
    loss = session.run(result)
    assert loss.shape == (batch, timestep - 1)

    # Confirm that the last timestep/word (dummy) is discarded

    y_true_np = session.run(y_true)
    y_pred_np = session.run(y_pred)

    y_true_np_changed = y_true_np.copy()
    y_pred_np_changed = y_pred_np.copy()
    y_true_np_changed[:, -1, :] = np.random.normal(size=(batch, vocab_size))
    y_pred_np_changed[:, -1, :] = np.random.normal(size=(batch, vocab_size))

    y_true_changed = tf.constant(y_true_np_changed)
    y_pred_changed = tf.constant(y_pred_np_changed)

    result = categorical_crossentropy_from_logits(y_true_changed,
                                                  y_pred_changed)
    loss_changed = session.run(result)
    np.testing.assert_array_almost_equal(loss, loss_changed)

    # Confirm that padding words don't contribute to the loss

    # Mustn't be -1 (the last) since it is a dummy word that will be discarded
    dummy_index = -2

    y_true_np_dummy = y_true_np.copy()
    y_true_np_dummy[1, dummy_index, :] = np.zeros(shape=(vocab_size,))
    y_true_dummy = tf.constant(y_true_np_dummy)


# Path: keras_image_captioning/losses.py
# def categorical_crossentropy_from_logits(y_true, y_pred):
#     # Discarding is still needed although CaptionPreprocessor.preprocess_batch
#     # has added dummy words as all-zeros arrays because the sum of losses is
#     # the same but the mean of losses is different.
#     y_true = y_true[:, :-1, :]  # Discard the last timestep/word (dummy)
#     y_pred = y_pred[:, :-1, :]  # Discard the last timestep/word (dummy)
# 
#     loss = tf.nn.softmax_cross_entropy_with_logits(labels=y_true,
#                                                    logits=y_pred)
#     return loss


# Path: keras_image_captioning/losses_test.py
import numpy as np
import pytest
import tensorflow as tf
from .losses import categorical_crossentropy_from_logits

    loss = session.run(result)
    assert loss.shape == (batch, timestep - 1)

    # Confirm that the last timestep/word (dummy) is discarded

    y_true_np = session.run(y_true)
    y_pred_np = session.run(y_pred)

    y_true_np_changed = y_true_np.copy()
    y_pred_np_changed = y_pred_np.copy()
    y_true_np_changed[:, -1, :] = np.random.normal(size=(batch, vocab_size))
    y_pred_np_changed[:, -1, :] = np.random.normal(size=(batch, vocab_size))

    y_true_changed = tf.constant(y_true_np_changed)
    y_pred_changed = tf.constant(y_pred_np_changed)

    result = categorical_crossentropy_from_logits(y_true_changed,
                                                  y_pred_changed)
    loss_changed = session.run(result)
    np.testing.assert_array_almost_equal(loss, loss_changed)

    # Confirm that padding words don't contribute to the loss

    # Mustn't be -1 (the last) since it is a dummy word that will be discarded
    dummy_index = -2

    y_true_np_dummy = y_true_np.copy()
    y_true_np_dummy[1, dummy_index, :] = np.zeros(shape=(vocab_size,))
    y_true_dummy = tf.constant(y_true_np_dummy)


    result = categorical_crossentropy_from_logits(y_true_dummy, y_pred)