Is this code correct? Where is the input layer?

Hello everyone, I am learning the Course 2 week2, the attached image is the code that I copy from “Neural Network Training” video. I found that in the Sequential model, there is no “input layer”, I remember that there is a “input layer” wrote in some codes previous. Is this code correct? or I need to put back the input layer? As I seldom saw “input layer” code in video, if I need to put it back, can you explain some more for me? I am confused here. Thanks!

Hi, you don’t need to put InputLayer inside Sequential because it will be automatically inferred when you call Tensorflow is able to use the shape of your training samples X as the input shapes and construct the network accordingly.


Oh… Interesting, I got it now, thank you!


Sorry for quality but,
Check these pages from “Deep Learning with Python” by Francois Chollet

7.2 is most relevant to your question.

1 Like

Thanks @SamReiswig , Wow, from your picture, It’s already the 7th chapter!! I can feel that learning from this course is so fast than reading book by myself. It’s hard for me to read book and understand everything… Thanks!

@rmwkwok Hello, I just faced the similar issue, in Lab exercise (Course 2 week 2), as the attached picture, it required the input layer with shape, I am confused now, I can’t sure when I should put it there and when should not. Can you help me to classify it? Thanks again!

1 Like

If you add an input layer, tensor flow doesn’t need to infer it from your sample. If you don’t add, tensorflow needs to.

You can always add an input layer to specify the input.

For example, if you want to access the weights of your neural network, without specifying the input shape, the neural network doesn’t know how many weights are needed, and thus can’t build it up for you to access its weights.

1 Like

A bit confused with ‘weight’… Is there any way that I can confirm the input shape? While trying on codes, I always confused about how to find the shape actually, and I can just keep on trying the number that I think it may be correct.

Take one single sample, its shape is the input shape.

In exercise 2, if you scroll down a few cells, you will see that it will be fit by the variable X. Take any one sample out and find out its shape, for example, by X[0].shape.

Btw, we don’t encourage learners to share assignment code, so I removed it.

ok thanks, let me try to understand it in later coding :slight_smile:

Hi SamReiswig! Can I have that book?