用JavaScript學習機器學習的4個理由

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"過去的幾年中,Python已成爲機器學習和深度學習的首選編程語言。與機器學習和深度學習相關的大多數書籍和在線課程要麼只用Python,要麼再帶上R語言。Python有着豐富的機器學習和深度學習庫、專門優化的實現,具備可伸縮性和大量功能,因而廣受歡迎。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但Python並不是編寫機器學習應用程序的唯一選擇。社區中有越來越多的開發人員正在使用JavaScript來運行機器學習模型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"儘管JavaScript(目前)並不能在機器學習領域替代根基深厚的Python,但掌握JavaScript機器學習技能也是有很多不錯的理由的,本文就會介紹其中的四個。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"私有機器學習"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大多數機器學習應用程序都基於客戶端-服務器架構。用戶必須將數據發送到機器學習模型所運行的地方。客戶端-服務器架構有一些顯著優勢。開發人員可以在服務器上運行他們的模型,並通過Web API將其提供給用戶應用程序。這樣開發人員就可以使用沒法跑在用戶設備上的超大型神經網絡了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"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}},{"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但問題在於,許多用戶設備並不默認支持Python機器學習。MacOS和大多數Linux版本預裝了Python,但你還是需要單獨安裝各種機器學習庫。Windows用戶必須手動安裝Python。而移動操作系統對Python解釋器的支持非常差。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相比之下,所有現代移動和桌面瀏覽器都原生支持JavaScript。這意味着JavaScript機器學習應用程序可以確保在大多數臺式機和移動設備上運行。因此,如果你的機器學習模型運行在瀏覽器中的JavaScript代碼上,你就能肯定幾乎所有用戶都可以訪問它。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"市面上已經有一些JavaScript機器學習庫了。TensorFlow.js就是一個例子,它是谷歌著名的TensorFlow機器學習和深度學習庫的JavaScript版本。如果你使用智能手機、平板電腦或臺式計算機訪問TensorFlow.js演示頁面,會發現許多使用JavaScript機器學習的現成示例。它們在你的設備上運行機器學習模型,而無需將任何數據發送到雲端,而且你不需要安裝其他任何軟件。其他一些功能強大的JavaScript機器學習庫包括ML5.js、Synaptic和Brain.js。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/d3\/8e\/d34bd493eccfa2325b50c5df6371fd8e.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"上圖:TensorFlow.js應用程序的示例。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"快速和定製的ML模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隱私並不是設備端機器學習的唯一優勢。在某些應用程序中,從設備向服務器發送數據的往返過程可能會導致延遲,從而影響用戶體驗。在其他一些情況下,用戶可能希望在沒有互聯網連接的情況下也能夠運行機器學習模型。在這類場景中,在用戶設備上運行JavaScript機器學習模型會非常方便。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JavaScript機器學習的另一個重要用途是模型定製。例如,假設你要開發一個文本生成機器學習模型,可以適應每個用戶的語言偏好。一種解決方案是在服務器上爲每個用戶存儲一種模型,並根據用戶的數據對其進行訓練。隨着用戶的增長,這將給服務器增加額外的負載,並且還需要你將潛在的敏感數據存儲在雲端。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一種替代方法是在服務器上創建一個基本模型,並在用戶設備上創建副本,然後使用JavaScript機器學習庫來根據用戶數據微調模型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"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}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/b7\/e2\/b7dda7dd63ab29c9d3f61a91772c2de2.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"上圖:客戶端機器學習允許開發人員在用戶設備上運行自定義模型。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"輕鬆將機器學習集成到Web和移動應用程序中"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JavaScript機器學習的另一個好處是輕鬆與移動應用程序集成。移動操作系統對Python的支持仍處於初級階段。但是,市面上已經有了豐富的跨平臺JavaScript移動應用開發工具,例如Cordova和Ionic。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些工具非常受歡迎,因爲你可以用它們只編寫一次代碼就部署到iOS和Android設備上。爲了讓代碼在不同的操作系統之間保持兼容,跨平臺開發工具會啓用一個“Webview”,這是一個可以運行JavaScript代碼並能嵌入到目標操作系統的原生應用程序中的瀏覽器對象。這些瀏覽器對象支持JavaScript機器學習庫。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個例外是React Native,一種流行的跨平臺移動應用程序開發框架,它不依賴Webview來運行應用程序。但是,鑑於移動機器學習應用程序的普及,谷歌已經爲React Native發佈了TensorFlow.js的一個特別版本。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你是用原生代碼編寫移動應用,並且希望集成JavaScript機器學習代碼,則可以將自己的嵌入式瀏覽器對象(例如iOS中的WKWebView)添加到你的應用中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還有其他一些針對移動應用程序的機器學習庫可選,例如TensorFlow Lite和CoreML。但是,它們需要在移動應用的目標平臺中編寫原生代碼。相比之下,JavaScript機器學習兼容性極佳。如果你已經實現了機器學習應用程序的瀏覽器版本,則只需很少或不做任何更改即可輕鬆將其移植到移動應用程序中。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"服務器上的JavaScript機器學習"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"機器學習的主要挑戰之一是訓練模型,對於深度學習而言尤其如此。在深度學習中,學習過程需要在多個epoch上進行昂貴的反向傳播計算。雖然你可以在用戶設備上訓練深度學習模型,但如果神經網絡很大,這可能需要數週或數月的時間才能完成。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Python更適合機器學習模型的服務端訓練。它可以擴展並在服務器羣集上分配負載,以加快訓練過程。訓練完模型後,你可以對其進行壓縮並交付給用戶設備以推理。所幸,用不同語言編寫的機器學習庫是高度兼容的。例如,如果你使用TensorFlow或Keras for Python訓練深度學習模型,則可以將其保存爲幾種獨立於語言的格式,例如JSON或HDF5。然後,你可以將保存的模型發送到用戶的設備,並使用TensorFlow.js或其他JavaScript深度學習庫來加載。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但值得注意的是,服務端JavaScript機器學習也在日趨成熟。你可以在JavaScript應用服務器引擎Node.js上運行JavaScript機器學習庫。TensorFlow.js有一個適用於運行Node.js的服務器的特別版本。與TensorFlow.js交互的JavaScript代碼與在瀏覽器中運行的應用程序所使用的JavaScript代碼相同。但在後臺,這個庫利用服務器上的特殊硬件來加快訓練和推理速度。PyTorch是另一種流行的Python機器學習庫,目前還沒有正式的JavaScript實現,但開源社區已經爲這個庫開發了JavaScript綁定。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用Node.js進行機器學習是一個相當新的概念,但它正在快速發展,因爲人們越來越有興趣在Web和移動應用程序中添加機器學習功能。隨着JavaScript機器學習社區的不斷髮展和相關工具的不斷成熟,對於許多希望在自身技能組合中添加機器學習的Web開發人員來說,這種技術可能會成爲他們的首選。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"作者介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Ben Dickson是一名軟件工程師,也是TechTalks的創始人,這個博客探討了技術是如何解決和製造問題的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/venturebeat.com\/2021\/04\/23\/4-reasons-to-learn-machine-learning-with-javascript\/?fileGuid=CTPT8pVQqdv8DvD3","title":"","type":null},"content":[{"type":"text","text":"https:\/\/venturebeat.com\/2021\/04\/23\/4-reasons-to-learn-machine-learning-with-javascript\/"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章