Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BUG] Result of model computing does not align with tf python #991

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

Closed
AsakusaRinne opened this issue Mar 2, 2023 · 0 comments · Fixed by #1001
Closed

[BUG] Result of model computing does not align with tf python #991

AsakusaRinne opened this issue Mar 2, 2023 · 0 comments · Fixed by #1001
Assignees
Labels
bug Something isn't working

Comments

@AsakusaRinne
Copy link
Collaborator

Here is the minimal example to reproduce it (the loaded model is a model used to train mnist):

C# code:

        var model = keras.models.load_model("xxx");

        model.summary();

        model.compile(new Adam(0.0001f), new LossesApi().SparseCategoricalCrossentropy(), new string[] { "accuracy" });

        var x = tf.reshape(tf.range(0, 784, dtype: TF_DataType.TF_FLOAT), (1, 784));
        var result = model.Apply(x);
        var loss = new LossesApi().SparseCategoricalCrossentropy().Call(tf.constant(3), result);
        Console.WriteLine(loss);

Python code:

    model: tf.keras.Model = tf.keras.models.load_model("xxx")
    model.summary()

    model.compile("adam", "sparse_categorical_crossentropy", ["accuracy"])

    x = tf.reshape(tf.range(0, 784), [1, 28, 28])
    result: tf.Tensor = model(x)
    print(result, tf.float64)
    loss = tf.keras.losses.sparse_categorical_crossentropy(tf.constant(3), result)
    print(loss)

Model summary:

_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 28, 28, 1)]       0         
                                                                 
 flatten (Flatten)           (None, 784)               0         
                                                                 
 dense (Dense)               (None, 100)               78500     
                                                                 
 dense_1 (Dense)             (None, 10)                1010      
                                                                 
 softmax (Softmax)           (None, 10)                0         
                                                                 
=================================================================
Total params: 79,510
Trainable params: 79,510
Non-trainable params: 0
_________________________________________________________________

Both the results of forward and backward are different between python and csharp, which has a bad impact on model performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant