Skip to content

About Sequential API and Functional API #960

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

Closed
lionwu opened this issue Dec 2, 2022 · 1 comment
Closed

About Sequential API and Functional API #960

lionwu opened this issue Dec 2, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@lionwu
Copy link

lionwu commented Dec 2, 2022

@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! ^^

@Oceania2018 Oceania2018 self-assigned this Dec 3, 2022
@Oceania2018 Oceania2018 added discussion A topic needed to discuss bug Something isn't working and removed discussion A topic needed to discuss labels Dec 3, 2022
@Oceania2018
Copy link
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.

Beacontownfc pushed a commit to Beacontownfc/TensorFlow.NET that referenced this issue Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants