Slice of 20 elements of rank1 tensor then reshaping throws "Input to reshape is tensor with 10272 values, but requested shape requires multiple of 20"

Viewed 43

My input tensor Data = Input(shape=(856,)) is a vector of float32 values concatenated from many different devices. I am trying to apply different TensorFlow functions to different subslices of each input chunk. Some of these functions include a 1D Convolution which requires a reshape.

slice = Data[:20]
reshape = tf.reshape(slice, (-1, 20, 1))
...

Doing this crashes after trying to fit my model. It throws the following errors:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Input to reshape is a tensor with 10272 values, but the requested shape requires a multiple of 20
         [[node model/tf.reshape_1/Reshape
 (defined at /home/.local/lib/python3.8/site-packages/keras/layers/core/tf_op_layer.py:261)
]] [Op:__inference_train_function_1858]

Errors may have originated from an input operation.
Input Source operations connected to node model/tf.reshape_1/Reshape:
In[0] model/tf.__operators__.getitem_1/strided_slice:
In[1] model/tf.reshape_1/Reshape/shape:

I am not sure how slicing 20 elements from a tensor of 856 could result in a tensor of 10272 values.

I have also tried using the tf.slice function a couple of different ways; both fail. Referencing the docs: https://www.tensorflow.org/guide/tensor_slicing

slice = tf.slice(Data, begin=[0], size=[20]) 
...

And fails, stating:

Shape must be rank 1 but is rank 2 for '{{node tf.slice/Slice}} = Slice[Index=DT_INT32, T=DT_FLOAT](Placeholder, tf.slice/Slice/begin, tf.slice/Slice/size)' with input shapes: [?,856], [1], [1].

For reference, here is what some of the values look like in the input data

array([-9.55784683e+01, -1.70557899e+01,  2.95967350e+01,  7.81378937e+00,
        9.02729130e+00,  5.49621725e+00,  4.19811630e+00,  5.84186697e+00,
        4.90438080e+00,  3.73845983e+00,  5.12300587e+00,  2.61530232e+00,
        2.67061424e+00,  3.91038632e+00,  2.31110978e+00,  4.20644665e+00,
        4.50000000e+00,  9.87345278e-01,  1.59740388e+00,  6.30727148e+00,
...
1 Answers

When you slice data like in Data[:20] it will produce a sequence with length min(20, len(Data)). So I guess your data has length less than 20.

Other message says it has rank 2, so I guess it has one of the following shapes

       1   10272
       2    5136
       3    3424
       4    2568
       6    1712
       8    1284
      12     856
      16     642

Any of those result in a tensor with 10272 elements as your first message shows, and that's not a multiple of 20.

Related