Reshape 2D into 4D array - numpy

Hello question regarding reshaping an array.
I have an array train_x (2D) which content is (103,784)
In this case 103 is the amount of examples.
784 is the input of my neural network.
Now I want to reshape from 2D to 4D
I use the following command:
train_x = np.reshape(train_x, (103, 28, 28, 1))
Is it correct that in this case 103 is still the amount of training examples and that in this case my input 784 is devided into a matrix of 28x28? 1 in this case is my channel, not using RGB (otherwhise the channel should be 3).
If my assumption is not correct please can somebody advice how to reshape from 2D to 4D to archive the above? tnx

Your assumption is correct. The NumPy doc about reshape states:
You can think of reshaping as first raveling the array (using the given index order), then inserting the elements from the raveled array into the new array using the same kind of index ordering as was used for the raveling.
train_x with shape (103, 784) would ravel to:
[img_0[0], ..., img_0[783], img_1[0], ..., img_1[783], ..., img_102[0], img_102[783]]
Which is then reshaped to 103 images of 28x28x1 with the reshape command from your question, as intended.
You should make sure that the flat 784 values have been raveled in the same order that you are using to unravel them, row-major or column-major. If you are unsure, a quick sanity check would be to plot one of the images after reshaping.

Alternatively, you can use the following.
train_X = X_train.reshape(X_train.shape[0],28,28,1)
provided X-train has shape of (103,784)

Related

Input shape for Conv 1D

I am trying to use Conv1D to classify malware based on an 1D array of data.
The shape of the X_train is (7200, 1000).
The shape of the Y_train is (1800, 1000).
The shape of X_test is (7200, 1).
The shape of Y_test(1800, 1).
I have tried doing
model.add(Conv1D(5, 5, input_shape=(X_train[1], 1)))
Does anyone know what input shape I should change it to so it will fit my data?
TensorFlow's Conv1D doesn't have an "input_shape" attribute. Try adding an Input layer before this one. Also, that 1 after the "input_shape" would cause an error. You must mention the parameter name too.

Looping over Ragged Tensors in Tensorflow

I wondered if there was any way too loop over Ragged Tensors, similarly to tf.map_fn. My Ragged Tensor has a different amount of rows but contains 4 points which I would like to retrieve.
The input looks as follows:
ragged_tensor[0] equals (100, 4)
ragged_tensor[1] equals (50, 4)
For now I can retrieve all of the points by looping over the first tensor inside the RaggedTensor:
test = tf.map_fn(lambda box: tf.image.crop_to_bounding_box(img, box[0], box[1], box[2], box[3]), tf.cast(boxes, tf.int32), dtype=tf.float32)
Does anyone have any experience with this, or might give me some tips&tricks? All help is appreciated.
This is one way to get the whole array of points:
points = tf.reshape(ragged_tensor.flat_values, [-1, 4])

Extract multiple patches around landmarks in TensorFlow

I'm trying to extract multiple patches around a set of 2D landmarks in the same image using TensorFlow.
Given an input tensor of 2D landmarks of shape[batch_size, num_landmarks, 2] and an input image tensor of shape [batch_size, num_rows, num_cols, num_channels] I would like to return a tensor containing [batch_size, num_landmarks, patch_rows, patch_cols, num_channels].
For now we can assume that batch_size=1 and if so, the following code will do the above:
im = tf.tile(im, (num_landmarks, 1, 1, 1))
patches = tf.image.extract_glimpse(im, (patch_cols, patch_rows), landmarks, centered=False, normalized=False)
I basically repeat the input image as many times as I have landmarks and then extract the glimpses. This is of course insane, when I have a lot of landmarks, so I was wondering if there exists a better way.
EDIT:
I think tf.gather_nd could do the trick, so I'm working on building the indices I need to extract the patches.

feeding a convolutional neural network with variable sized inputs in tensorflow

I am trying to pass a list of 2d numpy arrays with different sizes to a convolutional neural network using feed_dict parameter.
x = tf.placeholder(tf.float32, [batch_size, None, None, None])
y = tf.placeholder(tf.float32, [batch_size, 1])
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
optimizer.run(feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5})
and I am getting the following error :
ValueError: setting an array element with a sequence.
I understood that batch[0] has to contain arrays with the same size.
I am trying to find a way to apply the optimization using variable sized batch of arrays but all the suggested solutions ask to resize the arrays which is not possible in my case because these arrays are not images and contain DNA Fragments with different sizes (any modifications on any element of the array will cause a lost of important information)
Anyone has an idea ?
The matrix provided needs to have a consistent size across rows and columns. One row, or column, can not be a different size than any other.
Matrix #1 Matrix #2
1 2 3 1 2 3
None 4 5 6
None 7 8 9
No operations will work on Matrix #1, which is essentially what you have. If you want to feed in vairable size matrices (different sizes among matices, but size size with in rows and columns) this
may solve your problem
Args:
shape: The shape of the tensor to be fed (optional). If the shape is
not specified, you can feed a tensor of any shape.
Or you if you are looking for a sparse tensor (tf.sparse_placeholder() -- undefined elements are set to zero), this question may help.

How to convert a list of tensors of dim N to a tensor of dim N+1

I need to convert a list of tensors of dimensionality N to a new tensor with dimensionality N+1 so that the new dimension would be the right most dimension.
For example if x and y would be tensors of shape (4,3) both then I am trying to create a new tensor z of shape (4,3,2) by forming z and setting tensor x as the 0th element along the third dimension and setting tensor y as the 1st element along the third dimension. In pseudocode:
z = tf.fromList([x,y],3)
What's the best way to do that in Tensorflow. I was unable to figure it out from the documentation of TF 0.7.1.
If I'm reading you correctly, you want to interleave the data of the two tensors.
You want to tf.pack() them together, which would form a tensor of shape [2, 4, 3] and then tf.transpose([1, 2, 0]) that resulting tensor to get to the interleaving you want.
dga's method works, but tf.pack() has been removed from TensorFlow V1.0 onwards.
You can use tf.stack() to achieve the same.
Docs: https://www.tensorflow.org/api_docs/python/tf/stack