SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.17k stars 506 forks source link

[BUG Report]: Build of multi-input Model fails #1195

Closed Utanapishtim31 closed 8 months ago

Utanapishtim31 commented 8 months ago

Description

I want to create a Keras model with several inputs. When I call model.Apply(inputs) where inputs is a Tensors containing several Tensor (i.e. a list-like Tensors object), it fails because it builds for the first time the layers and ends in Model.build(KerasShapesWrapper input_shape) where _inputshape contains only one shape instead of the many shapes of the inputs.

This is caused by Layer.MaybeBuild(Tensors inputs) which calls build() with the value new KerasShapesWrapper(inputs.shape), which creates a KerasShapesWrapper with only the shape of the first Tensor of inputs.

Reproduction Steps

Create a neural network with two Dense layers as input, a Concatenate intermediate layer and a final Dense layer as output.

Then call model.Apply(inputs) where inputs is a Tensors object built with two NDArrays as input for each input Dense layer.

Known Workarounds

I have been able to circumvent the problem by keeping the input in an override of Model.Apply() and passing its shape in an override of build() :

public override Tensors Apply(Tensors inputs, Tensors states = null, bool? training = false, IOptionalArgs optional_args = null)
{
    if (!this.built)
        this.buildInputTensors = inputs;

    Tensors result = base.Apply(inputs, states, training, optional_args);

    this.buildInputTensors = null;

    return result;
}

public override void build(KerasShapesWrapper input_shape)
{
    if (this.buildInputTensors != null)
    {
        IEnumerable<Shape> inputShapes = this.buildInputTensors.Select((Tensor x) => x.shape);
        input_shape = new KerasShapesWrapper(inputShapes);
    }

    base.build(input_shape);
}

Configuration and Other Information

Tensorflow.NET v0.110.4 Tensorflow.Keras v0.11.4 .NET Framework 4.7.2 Windows 11 (Version 10.0.22621)

Wanglongzhi2001 commented 8 months ago

Hello, here is an example which build model use multiple inputs: https://github.com/SciSharp/TensorFlow.NET/blob/9e3654bf9c7c954690d0914558338233d638fcd6/test/TensorFlowNET.Keras.UnitTest/MultiInputModelTest.cs#L13-L61

Utanapishtim31 commented 8 months ago

Using the interface keras.layers indeed solves the problem. Thank you for your help.