# 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



@pytest.fixture
def session():
    return tf.Session()


def test_categorical_crossentropy_from_logits(session):
    batch, timestep, vocab_size = 8, 4, 2
    shape = (batch, timestep, vocab_size)
    # tf.random_normal is not used because every executing Session.run,
    # the value changes.
    y_true = tf.constant(np.random.normal(size=shape))
    y_pred = tf.constant(np.random.normal(size=shape))

    # Confirm the shape

# 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




@pytest.fixture
def session():
    return tf.Session()


def test_categorical_crossentropy_from_logits(session):
    batch, timestep, vocab_size = 8, 4, 2
    shape = (batch, timestep, vocab_size)
    # tf.random_normal is not used because every executing Session.run,
    # the value changes.
    y_true = tf.constant(np.random.normal(size=shape))
    y_pred = tf.constant(np.random.normal(size=shape))

    # Confirm the shape

    result = categorical_crossentropy_from_logits(y_true, y_pred)