Does input to neural network always need to convert to Transpose? How can we decide if it needs to convert to Transpose/AT?

Why there is .T in below line of code?

#
Select rows corresponding to the random indices and

```
# reshape the image
X_random_reshaped = X[random_index].reshape((20,20)).T
```

Hi!

In lab we read X from load_data() as a 5000 x 400 matrix where every row is a training example of a handwritten digit.

That section of code is used to reshape the data back into it’s original form of a 20 x 20 grayscale image and it is transposed so that the numbers are displayed correctly for people to read it.

1 Like

Thank you.

Does input to neural network always need to convert to Transpose? How can we decide if it needs to convert to Transpose/AT?

Hi @aimlgyani, if your dataset has m samples and each sample has n features, please make sure its shape to be (m, n). If your dataset has m photos and each photo has a size of x by x and with a depth of 3 (for RGB channels), and you are going to using a CNN to model your data, then please make sure its shape to be (m, x, x, 3).

I think Sam explained very well why transpose was used in that particular case. In general, as long as the shape is right, it’s ok to be used as an input to a neural network, and it doesn’t matter whether you transpose it or not transpose it - the key is the shape has to be correct and match the input requirement by the neural network that you define.

Raymond

1 Like

Thank you.

I will finish remaining learning in this course and hopefully I will understand further depth on reshaping input and deciding layers and what happens to prediction by those changes.

Sure, Aiml.

I also have a suggestion. When you come across a lab/assignment that uses neural network, do two things:

- if there is an
`InputLayer`

, write down the (A.) input_shape that is passed to the `InputLayer`

as an argument. If there is no `InputLayer`

, then ignore this step.
- go to the step that calls
`model.fit(...)`

and there you will find the `X`

that’s passed into the training process. Please print (B.) `X.shape`

and (C.) `X[0].shape`

and write them down.

You should see that (A.) equals to (C.), if you can write down (A.).

You should also see that (B.) has the shape of (m, n) or (m, x, x, 3) that I described in my previous reply.

Then if you go back, you **may or may not** find any `reshape`

or `transpose`

because they are **NOT** the key. They are used to transform X into the correct shape **only when needed**.

Raymond