在進行mnist識別時,https://www.charleychai.com/blogs/2018/ai/NN/lenet.html中,
X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)
為什么要設置最后一維為1呢?
可以查看Conv2D中對input_shape的說明,https://keras.io/layers/convolutional/
Input shape
4D tensor with shape: (batch, channels, rows, cols)
if data_format
is "channels_first"
or 4D tensor with shape: (batch, rows, cols, channels)
if data_format
is "channels_last"
.
對於代碼中,-1是指batch,共有多少個樣本,28分別指rows和cols,最后一個是指通道數,由於不是RGB,所以是1。