Skip to content

How to train model that takes mutiple tensors as input #939

@ZIWaters

Description

@ZIWaters

I code a model for testing which takes 2 differnt tensors as input as follows:

            Tensor input_wide = keras.Input(5);
            Tensor input_deep = keras.Input(6);

            var hidden1 = keras.layers.Dense(30, activation: keras.activations.Relu).Apply(input_deep);
            var hidden2 = keras.layers.Dense(30, activation: keras.activations.Relu).Apply(hidden1);
            var concat = keras.layers.Concatenate().Apply(new Tensors(input_wide, hidden2));

            var output1 = keras.layers.Dense(1, activation: keras.activations.Relu).Apply(concat);
            var output2 = keras.layers.Dense(1, activation: keras.activations.Relu).Apply(hidden2);

            var model = keras.Model(new Tensors(input_wide, input_deep),
                new Tensors(output1, output2)
            );

            model.compile(optimizer: keras.optimizers.Adam(),
                loss: keras.losses.MeanSquaredError(),
                new[] { "accuracy" });

Here I construct a Tensors with 2 tensor, which is assigned to the inputs of model. This model can be compiled without problems. Then I try to predict by this model:

                // Just test data
                var x1 = np.array(new float[,] { { 1, 2, 3, 4, 5 }, { 1, 3, 5, 7, 9 } });
                var x2 = np.array(new float[,] { { 1, 2, 3, 4, 5, 6 }, { 1, 3, 5, 7, 9, 11 } });
                var x = new Tensors(x1, x2);
                var pred = model.predict(x);

Here I got an exception System.Collections.Generic.KeyNotFoundException:“The given key '3' was not present in the dictionary.”.Did I make a mistake or is it a bug? Many thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiNew API or change APIenhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions