tensorflow - Shape of tensor for 2D image in Keras -


i newbie keras (and somehow tf) have found shape definition input layer confusing.

so in examples, when have 1d vector of length 20 input, shape gets defined as

...input(shape=(20,)...) 

and when 2d tensor greyscale images needs defined mnist, defined as:

...input(shape=(28, 28, 1)...) 

so question why tensor not defined (20) , (28, 28)? why in first case second dimension added , left empty? in second, number of channels have defined?

i understand depends on layer conv1d, dense or conv2d take different shapes seems first parameter implicit?

according docs, dense needs (batch_size, ..., input_dim) how related example:

dense(32, input_shape=(784,))

thanks

tuples vs numbers

input_shape must tuple, (20,) can satisfy it. number 20 not tuple. -- there parameter input_dim, make life easier if have 1 dimension. parameter can take 20. (but really, find confusing, work input_shape , use tuples, keep consistent understanding).

dense(32, input_shape=(784,)) same dense(32, input_dim=784).

images

images don't have pixels, have channels (red, green, blue).
black/white image has 1 channel.

so, (28pixels, 28pixels, 1channel)

but notice there isn't obligation follow shape images everywhere. can shape them way like. kinds of layers demand shape, otherwise couldn't work.

some layers demand specific shapes

it's case of 2d convolutional layers, need (size1,size2,channels). need shape because must apply convolutional filters accordingly.

it's case of recurrent layers, need (timesteps,featuresperstep) perform recurrent calculations.

mnist models

again, there isn't obligation shape image in specific way. must according first layer choose , intend achieve. it's free thing.

many examples don't care image being 2d structured thing, , use models take 784 pixels. that's enough. start dense layers, demand shapes (size,)

other examples may care, , use shape (28,28), these models have reshape input fit needs of next layer.

convolutional layers 2d demand (28,28,1).

the main idea is: input arrays must match input_shape or input_dim.

tensor shapes

be careful, though, when reading keras error messages or working custom / lambda layers.

all these shapes defined before omit important dimension: the batch size or number of samples.

internally tensors have additional dimension first dimension. keras report none (a dimension adapt batch size have).

so, input_shape=(784,) reported (none,784).
, input_shape=(28,28,1) reported (none,28,28,1)

and actual input data must have shape matches reported shape.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -