为什么在将数据馈入张量流之前将其展平? [英] Why do we flatten the data before we feed it into tensorflow?
问题描述
我正在关注 udacity MNIST教程,而MNIST数据最初是 28 * 28
矩阵。但是,在馈入数据之前,它们将数据平整为784列(784 = 28 * 28)
的1d数组。
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)
.
例如,
的原始训练集形状为(200000,28,28)。
200000行(数据)。每个数据都是28 * 28矩阵
For example,
original training set shape was (200000, 28, 28).
200000 rows (data). Each data is 28*28 matrix
他们将其转换为形状为(200000,784)的训练集
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?
推荐答案
因为当您添加完全连接的图层,您始终希望数据是(1或)2维矩阵,其中每一行都是代表数据的矢量。这样,完全连接的图层就是输入(大小为(batch_size,n_features)
)和权重(形状为( n_features,n_outputs)
)(加上偏差和激活函数),您将得到形状为(batch_size,n_outputs)
的输出。另外,您真的不需要完全连接的层中的原始形状信息,因此可以将其丢失。
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).
这篇关于为什么在将数据馈入张量流之前将其展平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!