Preprocess for x data, reshape, dimation of input for model
``` def preproc_x(x, flatten=True): x = x / 255. if flatten: x = x.reshape((len(x), -1)) return x ``` 以上為Day78 範例程式的X(feature)前處理 一開始看不懂為什麼要做flatten "x = x.reshape((len(x), -1))" 於是先跳過不做 但是後來發現 在model.fit會出現錯誤: Error when checking target: expected output to have 4 dimensions, but got array with shape (50000, 10) 於是研究了這行code 這段flatten會改變X的Dimation  我有兩個問題 1. python語法問題 為什麼x.reshape((len(x), -1)),會把 維度從(50000, 32, 32, 3) 變成(50000, 3072) 2. model.fit 的input 為什麼不能是(50000, 32, 32, 3)
回答列表
-
2020/07/30 下午 09:06張維元 (WeiYuan)贊同數:0不贊同數:1留言數:3
嗨,以下簡單回覆你的問題:
1. python 語法問題 為什麼x.reshape((len(x), -1)),會把 維度從(50000, 32, 32, 3) 變成(50000, 3072)
=> x.reshape 的用法是把 x 轉變成新的維度,-1 是不指定維度的意思,例如:reshape(3, 1) 就是將資料成 3*1,reshape(2, 3, 4) 就是將資料成 2*3*4。reshape((len(x), -1)) 的意思是把資料變成 len(x) * n ,n 會從原本的維度推算過來,也就是 32*32*3 。
如果這個回答對你有幫助請主動點選「有幫助」或「最佳解答」的按鈕,也可以追蹤我的GITHUB 帳號。若還有問題的話,也歡迎再開一個新的問題繼續發問,或者把你理解的部分整理上來,我都會提供你 Review 和 Feedback 😃😃😃 另外我目前有舉辦一個社群活動:學員限定!CUPOY 馬拉松線上小聚 👨🏻💻👨🏻💻,歡迎一起來玩玩!