我不是完美主義,但是至少,我在做實驗的時候不能容忍有 warning 的出現。
今天使用 tensorflow.keras.datasets中的 imdb 資料集,使用 imdb.load_data() 方法匯入資料的時候,報出一個警告,VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
經過查閱,得知這是因為建立 numpy包中 ndarray 類物件時,設若 np.array( some_object),如果傳入的引數 some_object 是個 ragged 的,或者維度不整齊,或者每維元素數量不固定的,那麼就會報出這個 warning。
這不是自己寫的程式碼有問題,而是人家寫好的程式碼模組有問題(現在能 被 numpy 相容,以後會被棄置的)。所以可以修改人家寫好的 imdb.load_data 的程式碼。
方法。
先 import warings, 然後 warnings.simplefilter("error"),使得 warning 不會通過檢查,報出warning位置,找到了關於建立 ndarray 的程式碼,這就在imdb.load_data 方法內部
和
一共兩處,
分別做如下修改:
然後就沒有 warning 了。
---------------------------------------------------------------------------------------
Conclusion
---------------------------------------------------------------------------------
哇,第一次改人家的庫,雖然並不是底層的庫,也是很爽。我 cao 勇 的(ww腔)~~
Above all, 浪費了很多的時間,只為了不見個warning,感覺很虧。
還是要抓主要矛盾,下次一定。 :)