'Tensorflow.InvalidArgumentError: Expected input[1] == 'TensorArrayV2_1/element_shape:output:0' to be a control input

I have a problem with recurrent cells in Tensorflow. I've created a model with Tensorflow 2.8 (Python 3.8) and saved it to file.

model = tf.keras.models.Sequential(
    [
        Input((3, 1)),
        GRU(5),
        Dense(1)
    ]
)
model.compile(Adam(), "mse")
model.save("Model")

Then I want to call it from .Net using:

  • Microsoft.ML (1.7.1)
  • Microsoft.ML.Tensorflow (1.7.1)
  • Microsoft.ML.Tensorflow.Redist (0.14.0)
  • Tensorflow.NET (0.40.1)

Code:

public class TestInput
{
   [ColumnName("serving_default_input_1")]
   [VectorType(3 * 1)]
   public float[] serving_default_input_1 { get; set; } = new float[3];
        
   [LoadColumn(1)]
   public string[] saver_filename = new string[] { "saver" };
};

public class TestPrediction
{
     [ColumnName("StatefulPartitionedCall")]
     [VectorType(1)]
     public float[] StatefulPartitionedCall { get; set; }
}
var mlContext = new MLContext();
var model = mlContext.Model.LoadTensorFlowModel(modelPath);
var score = model.ScoreTensorFlowModel(
    outputColumnNames: new[] { "StatefulPartitionedCall" }, 
    inputColumnNames: new[] { "serving_default_input_1" }
);
var autoSchema = SchemaDefinition.Create(typeof(TestInput));
var pipeline = mlContext.Transforms.SelectColumns("serving_default_input_1")
        .Append(score);
var input = new TestInput
{
    serving_default_input_1 = new float[] { 0, 0.2f, 0, }
};
var data = new TestInput[]
{
   input
};
var dataView = mlContext.Data.LoadFromEnumerable<TestInput>(data, autoSchema);
var transformer = pipeline.Fit(dataView);
var predictor = mlContext.Model.CreatePredictionEngine<TestInput, TestPrediction>(transformer);
var p = predictor.Predict(input)

During the prediction Tensorflow throws an exception:

Tensorflow.InvalidArgumentError: Expected input[1] == 'TensorArrayV2_1/element_shape:output:0' to be a control input.
In {{node TensorArrayV2Stack/TensorListStack}}
 [[{{node sequential/gru/PartitionedCall}}]]
 [[{{node StatefulPartitionedCall}}]]
 [[{{node StatefulPartitionedCall}}]]

at tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at partitioned_function_ops.cc:118

If I replace GRU with Dense, the code works properly.

Related question Tensorflow c_api (1.13.2) - Import LSTM .pb: Expected input[1] to be a control input

Thanks for any ideas how to fix it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source