question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declare batch input shape independently of first layer with Sequential API

See original GitHub issue

I know the functional API supports declaring input shape in an Input layer, and then passing that to a Model() object, but what’s the equivalent for Sequential()? I’d expect something like this to work:

model = Sequential()
model.add(Input(batch_shape=(batch_size, height, width, channels)))
model.add(Convolutions/RNNs/Dense/Whatever)
model.compile(...)

but it doesn’t.

AttributeError: ‘TensorVariable’ object has no attribute ‘inbound_nodes’

What am I missing? Shouldn’t the API’s work similarly?

I’d even expect something like (my preferred API, because all models always have at least one input, right?):

model = Sequential((batch_size, height, width, channels))
model.add(Convolutions/RNNs/Dense/Whatever)
model.compile(...)

Also, why is it called batch_input_shape in some layers, and batch_shape in others? It’s a bit confusing, honestly.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
farizrahman4ucommented, Oct 25, 2016

I know the functional API supports declaring input shape in an Input layer, and then passing that to a Model() object, but what’s the equivalent for Sequential()?

InputLayer or just Layer.

model = Sequential()
model.add(InputLayer(batch_input_shape=(batch_size, height, width, channels)))
model.add(Convolutions/RNNs/Dense/Whatever)
model.compile(...)

What am I missing? Shouldn’t the API’s work similarly?

Input is a function which returns a tensor. You add layers to Sequential models. Not tensors

I’d even expect something like (my preferred API, because all models always have at least one input, right?):

model = Sequential([Layer(batch_input_shape=(batch_size, height, width, channels))])
model.add(...)
model.add(...)
...

Also, why is it called batch_input_shape in some layers, and batch_shape in others? It’s a bit confusing, honestly.

Its not. All layers take batch_input_shape and input_shape arguments. There is “input” in the argument names because they specify the shapes of the tensors which will be “input” to the layer.

The Input function takes batch_shape and shape arguments. No “input” here. Since here the arguments specify the shape of the tensor (placeholder to be more precise) itself. We are simply asking Keras to create a placeholder of a given shape.

2reactions
fcholletcommented, Oct 26, 2016

@farizrahman4u got it right.

Basically Input is the equivalent of instantiating an InputLayer and calling it in one go. Reading the guide “getting started with the functional API” should clear up any questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Making new layers and models via subclassing - Keras
A layer encapsulates both a state (the layer's "weights") and a transformation from inputs to outputs (a "call", the layer's forward pass).
Read more >
Custom layers | TensorFlow Core
Layer class and implementing: __init__ , where you can do all input-independent initialization; build , where you know the shapes of the input...
Read more >
Guide to the Sequential model - Keras Documentation
Specifying the input shape. The model needs to know what input shape it should expect. For this reason, the first layer in a...
Read more >
tf.keras.models.Sequential | TensorFlow - API Manual
Input (shape=(10,)) y3 = model(x3) self.assertEqual(len(model.updates), 4) # But if you call the inner BN layer independently, you don't affect # the model's ......
Read more >
How to Use the Keras Functional API for Deep Learning
The sequential API allows you to create models layer-by-layer for most problems. It is limited in that it does not allow you to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found