Skip to content
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

[BUG Report]: System.NullReferenceException during fit() #1206

Open
avipreshel opened this issue Nov 4, 2023 · 3 comments
Open

[BUG Report]: System.NullReferenceException during fit() #1206

avipreshel opened this issue Nov 4, 2023 · 3 comments
Assignees

Comments

@avipreshel
Copy link

avipreshel commented Nov 4, 2023

Description

I have a pretty plain code with a custom loss function. The code throws a null exception right at the beginning

`using Tensorflow;
using Tensorflow.Keras.Losses;
using Tensorflow.Keras.Metrics;
using Tensorflow.Keras.Optimizers;
using Tensorflow.NumPy;
using Tensorflow.Operations.Initializers;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;

namespace KerasDotNet
{
internal class WeightedF1Loss : ILossFunc
{
public string Reduction => throw new NotImplementedException();

    public string Name => nameof(WeightedF1Loss);

    Tensor _beta;
    Tensor _epsilon;
    public WeightedF1Loss(float beta)
    {
        _beta = tf.constant(beta, dtype: TF_DataType.TF_FLOAT);
        _epsilon = tf.constant(float.Epsilon, dtype: TF_DataType.TF_FLOAT);
    }

    public Tensor Call(Tensor y_true, Tensor y_pred, Tensor sample_weight)
    {
        y_pred = tf.cast(y_pred >= 0.7f, TF_DataType.TF_FLOAT);

        var tp = tf.reduce_sum(y_true * y_pred);
        var fp = tf.reduce_sum((1 - y_true) * y_pred);
        var fn = tf.reduce_sum(y_true * (1 - y_pred));
        var precision = tp / (tp + fp + _epsilon);
        var recall = tp / (tp + fn + _epsilon);
        var f1_score = (tf.square(_beta) + 1) * (precision * recall) / (tf.square(_beta) * precision + recall + _epsilon);
        var res = 1 - f1_score;
        return res;
    }
}

internal class Program
{
    static void Main(string[] args)
    {
        //tf.enable_eager_execution(); ==> Runs the same with or without this line
        var x = np.array(new float[,] { { 1, 1, 1 }, { 2, 2, 2 }, { 1, 0, 3 }, { 1, 1, 3 } });
        var y = np.array(new float[,] { { 0 }, { 1 }, { 1 }, { 0 } });

        var loss = new WeightedF1Loss(0.2f);
        var inputs = keras.Input(shape: 3);
        var l1 = keras.layers.Dense(2, activation: "relu").Apply(inputs);
        var outputs = keras.layers.Dense(1, activation: "sigmoid").Apply(l1);

        var model = keras.Model(inputs, outputs);
        model.compile(optimizer: new Adam(), loss: loss, new[] { "precision", "recall" });

        model.summary();

        model.fit(x, y, batch_size: 1, epochs: 10);


    }
}

}`

The exception is thrown from GetDataType() since "data" is null.

Stack trace dump:

Tensorflow.Binding.dll!Tensorflow.Binding.GetDataType(object data) Line 615 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.ops.convert_to_tensor(object value, Tensorflow.TF_DataType dtype, string name, bool as_ref, Tensorflow.TF_DataType preferred_dtype, Tensorflow.Contexts.Context ctx) Line 353 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.tensorflow.convert_to_tensor(object value, Tensorflow.TF_DataType dtype, string name, Tensorflow.TF_DataType preferred_dtype) Line 2038 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Eager.EagerRunner.AddInputToOp(object inputs, bool add_type_attr, Tensorflow.OpDef.Types.ArgDef input_arg, System.Collections.Generic.List flattened_attrs, System.Collections.Generic.List<Tensorflow.Tensor> flattened_inputs, Tensorflow.Eager.SafeEagerOpHandle op, Tensorflow.Status status) Line 450 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Eager.EagerRunner.TFE_FastPathExecute(Tensorflow.FastPathOpExecInfo op_exec_info) Line 319 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Contexts.Context.ExecEagerAction(string OpType, string Name, Tensorflow.ExecuteOpArgs args) Line 494 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Contexts.Context.ExecuteOp(string opType, string name, Tensorflow.ExecuteOpArgs args) Line 534 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.gen_training_ops.resource_apply_adam(Tensorflow.Tensor var, Tensorflow.Tensor m, Tensorflow.Tensor v, Tensorflow.Tensor beta1_power, Tensorflow.Tensor beta2_power, Tensorflow.Tensor lr, Tensorflow.Tensor beta1, Tensorflow.Tensor beta2, Tensorflow.Tensor epsilon, Tensorflow.Tensor grad, bool use_locking, bool use_nesterov, string name) Line 7 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.Adam._resource_apply_dense(Tensorflow.IVariableV1 var, Tensorflow.Tensor grad, System.Collections.Generic.Dictionary<Tensorflow.Keras.Optimizers.DeviceDType, System.Collections.Generic.Dictionary<string, Tensorflow.Tensor>> apply_state) Line 79 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.OptimizerV2.apply_grad_to_update_var(Tensorflow.IVariableV1 var, Tensorflow.Tensor grad, System.Collections.Generic.Dictionary<Tensorflow.Keras.Optimizers.DeviceDType, System.Collections.Generic.Dictionary<string, Tensorflow.Tensor>> apply_state) Line 108 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.OptimizerV2._distributed_apply.AnonymousMethod__1(Tensorflow.ops.NameScope ) Line 130 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Binding.tf_with<Tensorflow.ops.NameScope>(Tensorflow.ops.NameScope py, System.Action<Tensorflow.ops.NameScope> action) Line 256 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.OptimizerV2._distributed_apply.AnonymousMethod__0(Tensorflow.ops.NameScope ) Line 133 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Binding.tf_with<Tensorflow.ops.NameScope>(Tensorflow.ops.NameScope py, System.Action<Tensorflow.ops.NameScope> action) Line 256 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.OptimizerV2._distributed_apply(System.Collections.Generic.IEnumerable<(Tensorflow.Tensor, Tensorflow.IVariableV1)> grads_and_vars, string name, System.Collections.Generic.Dictionary<Tensorflow.Keras.Optimizers.DeviceDType, System.Collections.Generic.Dictionary<string, Tensorflow.Tensor>> _apply_state) Line 134 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.OptimizerV2.apply_gradients.AnonymousMethod__1(Tensorflow.ops.NameScope ) Line 73 C# Symbols loaded.
Tensorflow.Binding.dll!Tensorflow.Binding.tf_with<Tensorflow.ops.NameScope, Tensorflow.Operation>(Tensorflow.ops.NameScope py, System.Func<Tensorflow.ops.NameScope, Tensorflow.Operation> action) Line 263 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Optimizers.OptimizerV2.apply_gradients(System.Collections.Generic.IEnumerable<(Tensorflow.Tensor, Tensorflow.IVariableV1)> grads_and_vars, string name, bool experimental_aggregate_gradients) Line 75 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Engine.Model._minimize(Tensorflow.Gradients.GradientTape tape, Tensorflow.Keras.Engine.IOptimizer optimizer, Tensorflow.Tensor loss, System.Collections.Generic.List<Tensorflow.IVariableV1> trainable_variables) Line 897 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Engine.Model.train_step(Tensorflow.Keras.Engine.DataAdapters.DataHandler data_handler, Tensorflow.Tensors x, Tensorflow.Tensors y) Line 877 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Engine.Model.train_step_function(Tensorflow.Keras.Engine.DataAdapters.DataHandler data_handler, Tensorflow.OwnedIterator iterator) Line 856 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Engine.Model.FitInternal(Tensorflow.Keras.Engine.DataAdapters.DataHandler data_handler, int epochs, int verbose, System.Collections.Generic.List<Tensorflow.Keras.Engine.ICallback> callbackList, (Tensorflow.NumPy.NDArray, Tensorflow.NumPy.NDArray)? validation_data, System.Func<Tensorflow.Keras.Engine.DataAdapters.DataHandler, Tensorflow.OwnedIterator, System.Collections.Generic.Dictionary<string, float>> train_step_func) Line 660 C# Symbols loaded.
Tensorflow.Keras.dll!Tensorflow.Keras.Engine.Model.fit(Tensorflow.NumPy.NDArray x, Tensorflow.NumPy.NDArray y, int batch_size, int epochs, int verbose, System.Collections.Generic.List<Tensorflow.Keras.Engine.ICallback> callbacks, float validation_split, (Tensorflow.NumPy.NDArray val_x, Tensorflow.NumPy.NDArray val_y)? validation_data, bool shuffle, int initial_epoch, int max_queue_size, int workers, bool use_multiprocessing) Line 474 C# Symbols loaded.
KerasDotNet.dll!KerasDotNet.Program.Main(string[] args) Line 62 C# Symbols loaded.

Reproduction Steps

Run the code in the snipped as it's A self contained code (does not ready any files or configuration).

  • TensorFlow.NET v0.110.4
  • TensorFlow.Keras v0.11.4
  • SciSharp.TensorFlow.Redist-Windows-GPU v2.10.3
  • Dotnet 7
  • Visual Studio 2022

System specs:
Windows 10 x64
RTX 2080 Super

Known Workarounds

None

Configuration and Other Information

No response

@PavelBakurov
Copy link

Same here

@ThirdStreetDev
Copy link

I'm having the same problem.

@PederHP
Copy link

PederHP commented Mar 10, 2024

I have the same problem but in a slightly different situation where I get it for batch sizes greater than 1, but epoch counts greater than 1 do not trigger it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants