Mnist中數據,輸入n個樣本,每個樣本是784個列構成的向量。所以輸入的是n*784的矩陣。但是輸入到CNN中需要卷積,需要每個樣本都是矩陣。
x = tf.reshape(x, shape=[-1, 28, 28, 1])
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
解釋:如果等於-1的話,那麼Numpy會根據剩下的維度計算出數組的另外一個shape屬性值。
如何理解?舉個栗子:
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
Z = z.reshape([-1, 2, 2, 1])
最終輸出結果:
[[[[ 1]
[ 2]]
[[ 3]
[ 4]]]
[[[ 5]
[ 6]]
[[ 7]
[ 8]]]
[[[ 9]
[10]]
[[11]
[12]]]
[[[13]
[14]]
[[15]
[16]]]]
這樣做的目的是:將n個784個向量,變成n個28*28的
參考文章