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的
参考文章