3 dimensional array as input for Keras - python

I got a 3 dimensional array and would like to use it as an input for a sequential model in Keras. The shape of the input array is (32, 32, 4). I want to get an array with the same shape as output. How should i make a feed forward neuronal network with one input, one output and one hidden layer, to make it work with such an array as input?

Related

LSTM input and output dimensions in Keras

I am confused about LSTM input/output dimensions, specifically in keras library. How do keras return 2D output while its input is 3D? I know it can return 3D output using “return_sequence = Trure,” but if return_sequence = False, how can it deal with 3D and produces 2D output?
For example, if input data of shape (32, 16, 20), 32 batch size, 16 timestep, 20 features, and output of shape (32, 100), 32 batch size, 100 hidden states; how keras processes input of 3d and returns output 2d.
Additionally, how can concatenate input and hidden state if they don’t have the exact dimensions?
I found the answer to my question in the link below:
https://mmuratarat.github.io/2019-01-19/dimensions-of-lstm
it's very helpful!

Using a convolution neural network with a non-image input

I want to use a convolutional neural network, but I have a 2D array for the input, not an image. I am trying to evaluate a board game state where shapes are important.
The board is 5x5 and the values can be between -1 and 1, stored as a list of lists ex:
[[-1,1.0,-1,1,-1],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[-1,0.6,-1,-1,1]]
the first layer of the model is
tf.keras.layers.Conv2D(32, (3,3), input_shape=(5,5,1))
I convert the board to a numpy array
np.array([[-1,1.0,-1,1,-1],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[-1,0.6,-1,-1,1]])
I gather the boards into a list.
Then I convert the list into an array of arrays to fit
model.fit(np.array(x_train_l), y_train, epochs=10)
I get the following error:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 5, 5]
Just reshape your numpy array to have shape (5,5,1). Currently it is with shape (5,5).
np.array([[-1,1.0,-1,1,-1],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[-1,0.6,-1,-1,1]]).reshape(5,5,1)

input and output shape in convulational neural network

ValueError: Input 0 of layer sequential_30 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, None, None]
First of all, the details that you have provided is very less. From the details that you have provided, I have interpreted that you are trying to pass a numpy array of dimension 3 to a CNN Layer. A CNN Layer only accepts 4-dimensional numpy arrays, but you have given a 3-dimensional numpy array as the input. In order to solve this problem, reshape the array to a 4D array. If you provide ur code, I will be able to provide you the exact lines that you have to use.

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! :)

What value do i pass in Input(shape=...) in keras

Suppose I have a numpy array with shape = (1303, 3988, 1). What value do I need to pass to Input() so that my ai learns or do I need it, do I need to reshape it?
I understand that your data is 1303 instances of vectors size (3988,1).
The answer depend on the layer goes after the input:
If you feed it after to Conv1D layer so the input layer should be:
Input(3988,1)
Otherwise you should squeeze the layer with:
np.squeeze(your_numpy_array)
or just flatten the input after the first layer:
x=Input(3988,1)
x=Flatten()(x)

Categories

Resources