Skip to content Skip to sidebar Skip to footer

What Is The Right Way To Manipulate The Shape Of A Tensor When There Are Unknown Elements In It?

Let's say that I have a tensor of shape (None, None, None, 32) and I want to reshape this to (None, None, 32) where the middle dimension is the product of two middle dimensions of

Solution 1:

import keras.backend as K

defflatten_pixels(x):
    shape = K.shape(x)
    newShape = K.concatenate([
                                 shape[0:1], 
                                 shape[1:2] * shape[2:3],
                                 shape[3:4]
                             ])

    return K.reshape(x, newShape)

Use it in a Lambda layer:

from keras.layers import Lambda

model.add(Lambda(flatten_pixels))

A little knowledge:

  • K.shape returns the "current" shape of the tensor, containing data - It's a Tensor containing int values for all dimensions. It only exists properly when running the model and can't be used in model definition, only in runtime calculations.
  • K.int_shape returns the "definition" shape of the tensor as a tuple. This means the variable dimensions will come containing None values.

Post a Comment for "What Is The Right Way To Manipulate The Shape Of A Tensor When There Are Unknown Elements In It?"