Week1, Assignment 2 Sequential Model

In the Sequential model, documentation for TFL suggests, e.g. ZeroPadding2D takes an input of 4D tensor with shape (batch_size, rows, cols, channels) .

However, in the assignment, we can only specify a 3-D input (64,64,3) and it results in the first argument automatically (and mandatorily) set as None. If I give a 4-D input I get an error saying now it receives a ndims=5. Why is that?

Hey, are you using the input_shape argument to set a 4-D tensor as an input? The thing is you cannot use that argument to specify batch_size for input. So what its doing is adding the batch_size parameter by itself and considering the shape to have 5-D input.

Yep that’s what I was doing. Very helpful! So even though it takes a 4-D input, it will automatically add a batch_size parameter so when we stipulate the input size we should still do a 3-D input? I suppose it applies for all TFL functions then?

It boils down to what input_shape argument is for, specifying a fixed dimension for the inputs. Batches can have different sizes, and the layer will accomodate them. For any tensorflow layer, if you do define a 3-D input shape, you can pass 4-D input into it still as long as one of the extreme axes is used for batching.

To add to this though, you can hardcode batch sizes using tf.keras.Input.

2 Likes