Skip to content

Fix the bug of non-convergence when use SparseCategoricalCrossentropy #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -4,17 +4,21 @@ namespace Tensorflow.Keras.Losses
{
public class SparseCategoricalCrossentropy : LossFunctionWrapper, ILossFunc
{
private bool _from_logits = false;
public SparseCategoricalCrossentropy(
bool from_logits = false,
string reduction = null,
string name = null) :
base(reduction: reduction, name: name == null ? "sparse_categorical_crossentropy" : name){ }
base(reduction: reduction, name: name == null ? "sparse_categorical_crossentropy" : name)
{
_from_logits = from_logits;
}

public override Tensor Apply(Tensor target, Tensor output, bool from_logits = false, int axis = -1)
{
target = tf.cast(target, dtype: TF_DataType.TF_INT64);

if (!from_logits)
if (!_from_logits)
{
var epsilon = tf.constant(KerasApi.keras.backend.epsilon(), output.dtype);
output = tf.clip_by_value(output, epsilon, 1 - epsilon);