votes up 7

Inputs should have rank 3. Received input shape: input_shape

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

    raise ValueError('Stride ' + str(self.strides) + ' must be '
                           'greater than output padding ' +
                           str(self.output_padding))

  def build(self, input_shape):
    input_shape = tf.TensorShape(input_shape)
    if len(input_shape) != 3:
      raise ValueError('Inputs should have rank 3. Received input shape: ' +
                       str(input_shape))
    channel_axis = self._get_channel_axis()
    if input_shape.dims[channel_axis].value is None:
      raise ValueError('The channel dimension of the inputs '
                       'should be defined. Found `None`.')
    input_dim = int(input_shape[channel_axis])
    self.input_spec = InputSpec(ndim=3, axes={channel_axis: input_dim})
    ke
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

When doing convolution using Conv1DTranspose the input tensor should have rank 3. Any 4D tensor given to the layer causes this error.

Reproducing the error:

pipenv install tensorflow

from tensorflow.keras.layers import Conv1DTranspose
import numpy as np
filters=16
kernel_size=7
conv_layer = Conv1DTranspose(filters, 
                             kernel_size, 
                             strides=1, 
                             padding='valid')
inp = np.arange(15).reshape((1,5, 3, 1)).astype(np.float32)
output = conv_layer(inp)
print(output.shape)

The error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-94d4abacd6b4> in <module>()
      8                              padding='valid')
      9 img = np.arange(15).reshape((1,5, 3, 1)).astype(np.float32)
---> 10 output = conv_layer(img)
     11 print(output.shape)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
   1021       with ops.name_scope_v2(name_scope):
   1022         if not self.built:
-> 1023           self._maybe_build(inputs)
   1024 
   1025         if self._autocast:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2623         # operations.
   2624         with tf_utils.maybe_init_scope(self):
-> 2625           self.build(input_shapes)  # pylint:disable=not-callable
   2626       # We must set also ensure that the layer is marked as built, and the build
   2627       # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/convolutional.py in build(self, input_shape)
    965     if len(input_shape) != 3:
    966       raise ValueError('Inputs should have rank 3. Received input shape: ' +
--> 967                        str(input_shape))
    968     channel_axis = self._get_channel_axis()
    969     if input_shape.dims[channel_axis].value is None:

ValueError: Inputs should have rank 3. Received input shape: (1, 5, 3, 1)

Fix:

The input tensor to the convolution layer Conv1DTranspose should be a 3D tensor in the form of (batch_size, steps, channels).

from tensorflow.keras.layers import Conv1DTranspose
import numpy as np
filters=16
kernel_size=7
conv_layer = Conv1DTranspose(filters, 
                             kernel_size, 
                             strides=1, 
                             padding='valid')
img = np.arange(15).reshape((5, 3, 1)).astype(np.float32)
output = conv_layer(img)
print(output.shape)

Output:

(5, 9, 16)

Jul 16, 2021 kellemnegasi answer
kellemnegasi 30.0k

Add a possible fix

Please authorize to post fix