Skip to content

About Sequential API and Functional API #960

Closed

Description

@lionwu

@Oceania2018 Hi Haiping, I have learn Sequential API and Functional API via book "[TensorFlow.NET实战]".

In p.106 to 114, I am tring to build same model via Sequential API and Functional API and except there are equal.


public void SampleBuildModel()
{
        //UseFunctionalAPI to create Model
        Model model = null;
        var inputs = keras.Input(new Tensorflow.Shape(784), name: "I0");
        var outputs = layers.Dense(64, activation:keras.activations.Relu).Apply(inputs);
        outputs = layers.Dense(10, activation: keras.activations.Relu).Apply(outputs);
        model = keras.Model(inputs, outputs, name: "MyNN");

        Console.WriteLine("===== Build Model via Functional API =====");
        model.summary();

        //UseSequentialAPI to create Model
        var sModel = keras.Sequential();
        sModel.add(keras.Input(new Tensorflow.Shape(784), name: "I0"));
        sModel.add(keras.layers.Dense(64, activation: keras.activations.Relu));
        sModel.add(keras.layers.Dense(10, activation: keras.activations.Relu));

        Console.WriteLine("===== Build Model via Sequential API =====");
        sModel.summary();
}

But when I run my program, I got result below:



===== Build Model via Functional API =====
Model: MyNN
_________________________________________________________________
Layer (type)                  Output Shape              Param #
=================================================================
I0 (InputLayer)               (None, 784)               0
_________________________________________________________________
dense (Dense)                 (None, 64)                50240
_________________________________________________________________
dense_1 (Dense)               (None, 10)                650
=================================================================
Total params: 50890
Trainable params: 50890
Non-trainable params: 0
_________________________________________________________________
===== Build Model via Sequential API =====
Model: sequential
_________________________________________________________________
Layer (type)                  Output Shape              Param #
=================================================================
I0 (InputLayer)               (None, 784)               0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0

Dense layer is not add to model. Am I misunderstand or Sequential is error?

by the way, when I use SequentialAPI, load_weights() can't work correctly, FunctionalAPI can work right.

Thanks for your help! ^^

Activity

  1. self-assigned this
    on Dec 3, 2022
  2. added
    discussion
    bug
    and removed
    discussion
    on Dec 3, 2022
  3. Oceania2018 commented on Dec 3, 2022

    @Oceania2018
    Member

    @lionwu Thanks for reaching out regarding the model.summary bug. It's been fixed in latest commit. You can pull and check if it's working in your end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions