in Education by
What is the role of Flatten in Keras. I am executing the code below and it's a two layered network. The shape of it's 2-Dimensional data is (4,3) and the output is of 1-Dimensional data of shape (2,5): model = Sequential() model.add(Dense(16, input_shape=(4, 3))) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(4)) model.compile(loss='mean_squared_error', optimizer='SGD') c = np.array([[[2, 3], [4, 5], [6, 7]]]) u = model.predict(c) print u.shape with flatten function it prints that u has shape(2,5) but when I remove it the shape of y changes to (2,4,5). As I know, model.add(Dense(16, input_shape=(3, 2))) this function here is used to create a hidden fully-connected layer of 16 nodes. And every node is connected with every 4x3 input elements. Hence all the 16 nodes of first laser are flat. This means the output we should get from first layer should be (2,17) which is used by second layer as input and it gives data of shape (2,5) as output. So here my question is why am I further flatting it, when the first layer's output is already flat? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Flatten is used to reshape the tensor to such a shape which is equal to the number of elements present in the tensor. Ex- Suppose you have the output of a layer of shape(20,4,5,3) , flatten will unstack all the tensor values into a 1-D tensor with shape(20*4*5*3) As per your code, Dense(16, input_shape=(3,2)) The above statement actually means that it will result in a dense network having 2 inputs and 16 outputs which will be applied independently for the 3steps. If D(x) transforms a 3D vector to a 16D layer you will get the output of a sequence of vectors [D(x[0,:], D(x[1,:],..., D(x[4,:]] having shape (4, 16) but for that you will have to first flatten the input to a 15D vector and then apply: model = Sequential() model.add(Flatten(input_shape=(4,3 ))) model.add(Dense(16)) model.add(Activation('relu')) model.add(Dense(4)) model.compile(loss='mean_squared_error', optimizer='SGD')

Related questions

0 votes
    I am trying to understand the role of the Flatten function in Keras. Below is my code, which is a simple two ... flatten it? Thanks! Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Every time I use binary_crossentropy there's ~80% acc and when I use categorical_crossentrop there's ~50% acc. And I ... should I use? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to save the history to a file, in Keras I have model.fit history = model.fit(Q_train, W_train, ... =(Q_test, W_test)) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Currently, I have Keras with TensorFlow and CUDA at the backend. But, I want to force Keras to use the ... are accessible via Keras. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I've trained a sentiment classifier model using Keras library by following the below steps(broadly). Convert Text ... around this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I am working on the following code: model = Sequential() a = keras.layers.advanced_activations.PReLU(init='zero', ... solution to this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a simple NN model for detecting hand-written digits from a 28x28px image written in python using Keras: ... that actually means? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want my model to be trained with some classified images. They are of different sizes, so how can I train ... resizing the images? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I read that regularization terms are implemented by manually adding an additional term to loss value in neural network ... it manually? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to assign a value to a TensorFlow variable in Python and I am using this: import tensorflow as tf import ... do to correct this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I have trouble understanding the difference (if there is one) between roc_auc_score() and auc() in scikit-learn. I ... out why. Thanks! Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    Can someone tell me the difference between input_shape, units, dim, etc? I want to know the attributes referred to the ... I do that? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I'm wondering how to calculate precision and recall measures for multiclass multilabel classification, i.e. classification ... labels? Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    I was looking at the docs of TensorFlow about tf.nn.conv2d here. But I can't understand what it does or ... it raised the question. Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Is there a simple way to find the number of parameters of a keras model if I have CNN, LSTm etc. Just like we do it in FFN. Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
...