-
Notifications
You must be signed in to change notification settings - Fork 509
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
Comments
Same here |
I'm having the same problem. |
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. |
Hi all, I faced the same problem when implementing the custom loss function. It's very confusing when the two loss functions below, one works, and the other one threw the null exception at the GetDataType().
public class CustomLoss : ILossFunc
{
public string Reduction => "auto";
public string Name => "custom_loss";
public Tensor Call(Tensor y_true, Tensor y_pred, Tensor sample_weight = null)
{
var mse_loss = tf.reduce_mean(tf.square(y_pred - y_true), axis: -1);
return mse_loss;
}
}
public class CustomLoss : ILossFunc
{
public string Reduction => "auto";
public string Name => "custom_loss";
public Tensor Call(Tensor y_true, Tensor y_pred, Tensor sample_weight = null)
{
int batch_size = y_true.shape.as_int_list()[0]; //extract the first element of the shape of the tensor
//convert Tensor to 1D array
var array_true = y_true.ToArray<float>();
var array_pred = y_pred.ToArray<float>();
float[] loss = new float[batch_size];
//perform some calculations here to compute the loss based on array_true and array_pred
//.........
var loss_tf = tf.convert_to_tensor(loss, dtype: TF_DataType.TF_FLOAT, shape: new Shape(batch_size));
return loss_tf;
}
} The returned Tensor I've spent hours, but no luck figuring out the solutions. Any help would be appreciated. Thank you. |
It seems to be a problem introduced in the latest version. But, I'm sorry, I don't have enough time to dig deeply into it now. |
Hi @AsakusaRinne, I followed your instructions and got the Call Stack below.
The Null data type happens within the public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info) From debugging, the For reference, this is how it looks like when it runs without error: As I'm quite new to Tensorflow, I can only trace back the problem to this far. Hopefully it gives you some idea where the issue is. |
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();
}`
The exception is thrown from GetDataType() since "data" is null.
Stack trace dump:
The text was updated successfully, but these errors were encountered: