ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape


在進行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。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM