Keras Convolutional NN Input Shape - python

I have a pandas dataframe as shown below:
I want to give this into a Keras convolutional neural network as an input, however, the shape is given as (20,4). To give it as a Keras input, its shape must be (n_samples, 4, 4, 1). How can I reshape it into this? Is it possible to set the major axis as the index?
This was created from a flattened pandas.Panel, if there is an easier conversion possible.
Thanks,
Stephen

Related

Can 2D convolutional neural network be converted into 1D convolutional neural network?

I have designed a neural network using 2d convolutional layers and max-pooling layers with the input shape for input, one hot encoded sequences as 2d array. then it is reshaped before inputting the model.
data = np.zeros( (100, 21 * 1000), dtype=np.float32 )
#reshape
x_data = tf.reshape( data, [-1, 1, 1000, 21] )
However, I used the same dataset using 1D convolutional layers by changing the model and input array without reshaping as it is 1D
data = np.zeros( (100, 1000,21), dtype=np.float32 )
finally, the 1D convolutional model performed well with 96% act. and 2d CNN gave 93%. Can someone explain to me what actually happens there to increase the accuracy?
Can someone explain to me what actually happens there to increase the accuracy?
That's hard to tell and depends on your specific dataset, network, hyperparameters etc.
Generally, in a conv2D-Layer the filter shifts horizontal and vertical. In a conv1D-Layer the filter shifts only vertical in the convolution process.
So which one is the best? That depends on your problem. For time series conv1D could be better and for images conv2D could be the better choice.

Is it possible to see the output after Conv2D layer in Keras

I am trying to understand each layer of Keras while implementing CNN.
In Conv2D layer i understand that it creates different convolution layer depending on various feature map values.
Now, My question is that
Can i see different feature map matrix that are applied on input image to get the convolution layer
Can i see the value of matrix that is generated after completion of Conv2D step.
Thanks in advance
You can get the output of a certain convolutional layer in this way:
import keras.backend as K
func = K.function([model.get_layer('input').input], model.get_layer('conv').output)
conv_output = func([numpy_input]) # numpy array
where 'input' and 'conv' denote the names of your input layer and convolutional layer. And you can get the weights of a certain layer like this:
conv_weights = model.get_layer('conv').get_weights() # numpy array

3 dimensional array as input for Keras

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?

what is the difference between Conventional 1D or 2D in keras layers

I'm discovering keras library and i can't tell what does the dimention mean in keras layers and how to choose them ? (model.add(Convolution2D(...)) or model.add(Convolution1D(...)) ).
For example i have a set of 9000 train traces and 1000 of test traces and each trace has 1000 samples, so i created the arrays X_train with a size of 9000*1000, X_test has a size of 1000*1000, Y_train has a size of 9000, and Y_test has a size of 1000.
my question is how can i choose the first layer dimension ?.
I tried using the same example implemented in MNIST such :
model.add(Convolution2D(9000, (1, 1), activation='relu', input_shape(1,9000000,1),dim_ordering='th'))
but it didn't work, i don't even know what should i put in each argument of Convolution function.
The choice of dimension (1D, 2D, etc.) depends on the dimensions of your input. For example, since you're using the MNIST dataset, you would use 2D layers since your input is an image with height and width (two dimensions). Alternatively, if you were using text data, you might use a 1D layer because sentences are linear lists of words (one dimension).
I would suggest looking at Francois Chollet's example of a convolutional neural net with MNIST: https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py. (Note: Conv2D is the same as Convolution2D.)

Keras Error on Convolutional Layer and Input Data

I filtered the training and testing data from CIFAR-100, I take for fruit and vegetables superclass only. Now, I've 2,500 training and 500 testing data. But, I got an error said that wrong dimension input for Convolutional layer.
My array data form:
I hope someone can help me for this case, thank you.
Your input data should have shape (2500, 3, 32, 32), seems you lost two of the dimensions on your preprocessing steps, either fix those or reshape your data as:
inputData = inputData.reshape((2500, 3, 32, 32)).
In general the input for a convolutional layer is (numSamples, numChannels, width, height). Note that when using the tensorflow backend the number of channels dimension goes at the end.

Categories

Resources