Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we flatten the data before we feed it into tensorflow?

I'm following udacity MNIST tutorial and MNIST data is originally 28*28 matrix. However right before feeding that data, they flatten the data into 1d array with 784 columns (784 = 28 * 28).

For example, original training set shape was (200000, 28, 28).
200000 rows (data). Each data is 28*28 matrix

They converted this into the training set whose shape is (200000, 784)

Can someone explain why they flatten the data out before feeding to tensorflow?

like image 620
aerin Avatar asked Jun 15 '17 16:06

aerin


1 Answers

Because when you're adding a fully connected layer, you always want your data to be a (1 or) 2 dimensional matrix, where each row is the vector representing your data. That way, the fully connected layer is just a matrix multiplication between your input (of size (batch_size, n_features)) and the weights (of shape (n_features, n_outputs)) (plus the bias and the activation function), and you get an output of shape (batch_size, n_outputs). Plus, you really don't need the original shape information in a fully connected layer, so it's OK to lose it.

It would be more complicated and less efficient to get the same result without reshaping first, that's why we always do it before a fully connected layer. For a convolutional layer, on the opposite, you'll want to keep the data in original format (width, height).

like image 113
gdelab Avatar answered Nov 03 '22 01:11

gdelab