How to reverse the shape of a numpy array - python

I have a numpy array with a shape of (3, 4096). However, I need it's shape to be (4096, 3). How do I accomplish this?

Related

ValueError: cannot reshape array of size 708434944 into shape (224,224,3)

I'm trying to reshape my array x_train to (-1, 224, 224, 3) for VGG16 model.
When the code is run, an error
ValueError: cannot reshape array of size 708434944 into shape (224,224,3)
appears, noting that the amount of data is (14119).
But this error does not appear when I run x_valid and the amount of data (660).
This is the reference:
https://www.kaggle.com/code/shiv28/ocr-handwritten/notebook
I am trying to apply to VGG16.
My dataset is like this, is it affected in reshape?
cannot reshape array of size 708434944 into shape (224,224,3)
because the product of 244*244*3 is not equal to 708434944.
Example:
If you have a 1D array of shape (100,) you can reshape it to (10,10) or (2,50) or (50,2) or (2,10,5) etc because its product is equal to 100.

Return a numpy array with third dimension representing multiple feature to only have the feature I want

I have a numpy array of shape (samples, sequence_length, number_of_features) e.g. (10000, 1024, 2)
I want to break this down into (10000, 1024, 1) where I am only taking the first feature - what is the most efficient way of doing this with numpy without unravelling the array?
Try this:
np.take(arr, indices=[0], axis=2)

How to convert image tensor in to numpy array in tensorflow?

I am training a CNN to generate images. The type of all the images are tensors. I want them to be converted into numpy arrays then I can process them using opencv.
I know about the .numpy() method, it converts my tensor into an numpy array but the shape is still tensor. I can't get it to work in cv2.
Here is my code:
p=model_(x)
s=p.numpy()
print(s.shape)
cv2.imwrite("hello.jpg",s)
(1, 183, 275, 3), this is the shape of the array generated using .numpy(), how can I change its shape to retain output image?
You need to get rid of the first dim (batch), just use slicing with reshape.
s=p.numpy()
print(s.shape)
cv2.imwrite("hello.jpg",s.reshape(s.shape[1:]))

ValueError: Error when checking input: expected lstm_12_input to have shape (5793993, 7) but got array with shape (7, 1)

keras.layers.LSTM(150, activation='sigmoid',return_sequences=True,input_shape=(X_train.shape)),
keras.layers.Dropout(0.2),
above is the first layer
(5793993, 7, 1) this is the input shape
ValueError: Error when checking input: expected lstm_12_input to have shape (5793993, 7) but got array with shape (7, 1)
What is fix for this tensorflow bug
You shouldn't include the number of samples for input_shape. Use this instead:
input_shape=(X_train.shape[1:])
I'm also fairly certain that your input should have shape (n_samples, 1, 7) to have it run.

Tensorflow Keras Conv2D error with 2D numpy array input

I would like to train a CNN using a 2D numpy array as input, but I am receiving this error: ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (21, 21).
My input is indeed a 21x21 numpy array of floats. The first layer of the network is defined as Conv2D(32, (3, 3), input_shape=(21, 21, 1)) to match the shape of the input array.
I have found some similar questions but none pertaining to a 2D input array, they mostly deal with images. According to the documentation, Conv2D is expecting an input of a 4D tensor containing (samples, channels, rows, cols), but I cannot find any documentation explaining the meaning of these values. Similar questions pertaining to image inputs suggest reshaping the input array using np.ndarray.reshape(), but when trying to do that I receive an input error.
How can I train a CNN on such an input array? Should input_shape be a different size tuple?
Your current numpy array has dimensions (21, 21). However, TensorFlow expects input tensors to have dimensions in the format (batch_size, height, width, channels) or BHWC implying that you need to convert your numpy input array to 4 dimensions (from the current 2 dimensions). One way to do so is as follows:
input = np.expand_dims(input, axis=0)
input = np.expand_dims(input, axis=-1)
Now, the numpy input array has dimensions: (1, 21, 21, 1) which can be passed to a TF Conv2D operation.
Hope this helps! :)

Categories

Resources