Home > Mobile >  Keras: Better Way to expand dimensions of 2D numpy array
Keras: Better Way to expand dimensions of 2D numpy array

Time:09-27

I got an ìnput_shape=[1, 7, 6, 1] on my first layer because on each run my batch is 1, the shape of the input array is (7, 6) and I got one channel.
My current solution to expand one 2D numpy array to match this input shape is really ugly:

input_array: np.ndarray = np.array([[ 1,  1,  0,  0,  0,  0,], \
       [ -1,  0,  0,  0,  0,  0,], \
       [1,  0,  0,  0,  0,  0,], \
       [-1, -1,  0,  0,  0,  0,], \
       [-1,  1,  1,  0,  0,  0,], \
       [ 0,  1,  0,  0,  0,  0,], \
       [ -1,  1,  0,  0,  0,  0,]])
np.expand_dims(np.expand_dims(np.expand_dims(input_array, axis=0), axis=-1), axis=0)

How can I expand my array to match the input shape without doing such an atrocity?

CodePudding user response:

Use np.reshape. If you want the shape (1, 7, 6, 1) for example:

reshaped_array = np.reshape(input_array, (1, 7, 6, 1))
print(reshaped_array.shape)
(1, 7, 6, 1)
  • Related