一次由默認參數引起的思考

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"題圖來自 Pexels 上的 "},{"type":"link","attrs":{"href":"https://www.pexels.com/zh-cn/@shvetsa?utmcontent=attributionCopyText&utmmedium=referral&utm_source=pexels","title":""},"content":[{"type":"text","text":"Anna Shvets"}]},{"type":"text","text":" 拍攝的圖片"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是我對本人在語雀上書寫的原始文檔("},{"type":"link","attrs":{"href":"https://www.yuque.com/lart/blog/qhllk3","title":""},"content":[{"type":"text","text":"https://www.yuque.com/lart/blog/qhllk3"}]},{"type":"text","text":")的搬運。"}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"緣起"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近換了新機器。需要將舊設備上的代碼和數據遷移過來。關鍵的一部分是自己論文使用的代碼。爲了確保代碼遷移的完整性,在新機器上對其進行了驗證。但是出乎意料,這部分python代碼竟然輸出了不一樣的結果。經過多番排查,終於確定了問題所在,那就是其中使用的一個圖像處理庫——pillow,新機器上安裝的是版本7.2.0,但是舊機器上確實6.2.1。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在pillow的release note("},{"type":"link","attrs":{"href":"https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#default-resampling-filter","title":""},"content":[{"type":"text","text":"https://pillow.readthedocs.io/en/stable/releasenotes/7.0.0.html#default-resampling-filter"}]},{"type":"text","text":")中可以看到,在7.0.0版本中,這個庫進行了不兼容的修改。將用來放縮圖像尺寸的 "},{"type":"codeinline","content":[{"type":"text","text":"resize()"}]},{"type":"text","text":" 函數默認的插值策略改成了雙三次插值:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"The default resampling filter has been changed to the high-quality convolution "},{"type":"codeinline","content":[{"type":"text","text":"Image.BICUBIC"}]},{"type":"text","text":"  instead of "},{"type":"codeinline","content":[{"type":"text","text":"Image.NEAREST"}]},{"type":"text","text":" , for the "},{"type":"codeinline","content":[{"type":"text","text":"resize()"}]},{"type":"text","text":"  method and the "},{"type":"codeinline","content":[{"type":"text","text":"pad()"}]},{"type":"text","text":" , "},{"type":"codeinline","content":[{"type":"text","text":"scale()"}]},{"type":"text","text":"  and "},{"type":"codeinline","content":[{"type":"text","text":"fit()"}]},{"type":"text","text":"  functions."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而我原始的代碼因爲沒有考慮這個問題,直接使用了默認參數,所以在新的環境下也就出現了這樣“讓人出乎意料”的問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣的一次經歷,讓我對於代碼中的默認參數有了一些新的看法。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一點想法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先就需要問自己一個問題,我們爲什麼需要“默認參數”?我覺得,歸根到底,還是爲了方便。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一切的優化、預設,在我看來,都是爲了一個目的,“節省時間、珍愛生命”。爲了儘可能避免開發者使用這些功能時需要考慮太多,儘可能的“開箱即用”,以將思考的時間用到更加重要的地方去。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,這樣就將我們對代碼可以如期的、穩健的運行的“信任”和“期望”託付給了我們使用的第三方工具的開發者。雖然大多數情況下,我們可以對他們保持足夠的信任,但是我們不得不考慮,第三方工具開發者對於所處領域的認識、對於相關知識的掌握程度、對於用戶的可能行爲的推測,都可能與我們這些使用者的實際情況有所偏差。所以我們真的很難、也不能完全,將我們的代碼這輛“貨車”的“未來”依附到一個具有着不可控因素的“發動機”上。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以我們應該怎麼做呢?難道真的要放棄默認參數帶來的便利麼?雖然放棄就在很大程度上可以避免這樣的問題(解決問題的一種直接的方法就是直接省掉出現問題的步驟 :
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章