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 the original one. What is the right way to do so?
import keras.backend as K
def flatten_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. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With