in Education by
I am trying to understand the role of the Flatten function in Keras. Below is my code, which is a simple two-layer network. It takes in 2-dimensional data of shape (3, 2), and outputs 1-dimensional data of shape (1, 4): model = Sequential() model.add(Dense(16, input_shape=(3, 2))) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(4)) model.compile(loss='mean_squared_error', optimizer='SGD') x = np.array([[[1, 2], [3, 4], [5, 6]]]) y = model.predict(x) print y.shape This prints out that y has shape (1, 4). However, if I remove the Flatten line, then it prints out that y has shape (1, 3, 4). I don't understand this. From my understanding of neural networks, the model.add(Dense(16, input_shape=(3, 2))) function is creating a hidden fully-connected layer, with 16 nodes. Each of these nodes is connected to each of the 3x2 input elements. Therefore, the 16 nodes at the output of this first layer are already "flat". So, the output shape of the first layer should be (1, 16). Then, the second layer takes this as an input, and outputs data of shape (1, 4). So if the output of the first layer is already "flat" and of shape (1, 16), why do I need to further flatten it? Thanks! Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
A flatten operation on a tensor reshapes the tensor to have the shape that is equal to the number of elements contained in tensor not including the batch dimension. The following code Dense(16, input_shape=(5,3)) will output a Dense network with 3 inputs and 16 output that would be applied independently for each of 5 steps. So if D(x) transforms 3 dimensional vector to 16-d vector what you'll get as output from your layer would be a sequence of vectors: [D(x[0,:], D(x[1,:],..., D(x[4,:]] with shape (5, 16). In order to have the approach you specify you may first Flatten your input to a 15-d vector and then apply Dense: model = Sequential() model.add(Flatten(input_shape=(3, 2))) model.add(Dense(16)) model.add(Activation('relu')) model.add(Dense(4)) model.compile(loss='mean_squared_error', optimizer='SGD') Hope this answer helps. If you wish to learn more about neural network then visit this Neural Network Tutorial.

Related questions

0 votes
    What is the role of Flatten in Keras. I am executing the code below and it's a two layered network. The ... output is already flat? Select the correct answer from above options...
asked Jan 25, 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
    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'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
    I'm looking for some examples of robot/AI programming using Lisp. Are there any good online examples available ... in nature)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm teaching a kid programming, and am introducing some basic artificial intelligence concepts at the moment. To begin ... and boxes)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am searching for information on algorithms to process text sentences or to follow a structure when creating sentences ... be great. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm looking to try and write a chess AI. Is there something I can use on the .NET framework (or maybe ... making a chess game? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm writing a game that's a variant of Gomoku. Basically a tic tac toe on a huge board. Wondering if anyone ... [self put randomly]; } Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm Working on document classification tasks in java. Both algorithms came highly recommended, what are the ... Processing tasks? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I am a little confused about the Hill Climbing algorithm. I want to "run" the algorithm until I found the ... question is too simple. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...