机器学习 对模型进行惩罚_使用Streamlit对机器学习模型进行原型制作
機(jī)器學(xué)習(xí) 對模型進(jìn)行懲罰
GitHub Repo: ml-streamlit-demo
GitHub存儲庫: ml-streamlit-demo
Bringing a Machine Learning model outside of a notebook environment and turning it into a beautiful data product used to be a lot of work. Luckily, there’s a lot of tooling being developed in this area to make prototyping easier. A while ago, I came across Streamlit, an open source Python library for building custom web apps.
將機(jī)器學(xué)習(xí)模型帶到筆記本環(huán)境之外,然后將其轉(zhuǎn)變?yōu)槠恋臄?shù)據(jù)產(chǎn)品,這過去需要做很多工作。 幸運的是,該領(lǐng)域正在開發(fā)許多工具,以簡化原型設(shè)計。 前一段時間,我遇到了Streamlit ,這是一個用于構(gòu)建自定義Web應(yīng)用程序的開源Python庫。
It’s quick and easy to get started and took me less than 30 minutes to build an app with a pre-trained model. Since then, I have been using Streamlit for prototyping models and demonstrating their capabilities. The nice user interface is a refreshing change from using my console for predictions.
它快速且容易上手,用了不到30分鐘的時間我就構(gòu)建了具有預(yù)訓(xùn)練模型的應(yīng)用程序。 從那時起,我一直在使用Streamlit制作模型原型并展示其功能。 與使用我的控制臺進(jìn)行預(yù)測相比,漂亮的用戶界面是一個令人耳目一新的變化。
I’m going to be sharing the process in this article, using GPT-2 as an example.
我將以GPT-2為例分享本文中的過程。
在本文中,我們將: (In this article, we will:)
load GPT-2 from HuggingFace’s transformer library
從HuggingFace的變壓器庫中加載GPT-2
- serve the text generator in a simple web app using Streamlit 使用Streamlit在簡單的Web應(yīng)用程序中提供文本生成器
 
從變形金剛加載GPT-2 (Load GPT-2 from Transformers)
GPT-2 is a transformers model trained on a very large English corpus for the purpose of predicting the next word in a phrase. It’s recent successor, GPT-3, have shocked the world with what it is capable of doing.
GPT-2是在非常大型的英語語料庫上訓(xùn)練的變形模型,用于預(yù)測短語中的下一個單詞。 它最近的繼任者GPT-3 ,以其強大的功能震驚了世界。
Let’s begin by installing all of the Python prerequisites. This is what my requirements.txt looks like. While not referenced anywhere in the code, GPT-2 requires either TensorFlow 2.0 or PyTorch to be installed.
讓我們從安裝所有Python先決條件開始。 這就是我的requirements.txt樣子。 盡管代碼中未引用GPT-2,但需要安裝TensorFlow 2.0或PyTorch。
streamlit==0.56.0tensorflow==2.2.0
transformers==3.0.2
In this example, we are going to work out of a single Python script. Here is the class for loading GPT-2 and using it to generate text when given a starting phrase. The max_length attribute indicates the max length of the generated text.
在此示例中,我們將使用單個Python腳本。 這是用于加載GPT-2并在給定起始短語時使用它生成文本的類。 max_length屬性指示生成的文本的最大長度。
通過Streamlit服務(wù)模型 (Serve the Model with Streamlit)
I’m going to define another function for instantiating the generator. The purpose of this function is to help with caching the load_generator method to make subsequent predictions faster.
我將定義另一個用于實例化生成器的函數(shù)。 此函數(shù)的目的是幫助緩存load_generator方法,以使后續(xù)預(yù)測更快。
The st.cache decorator tells Streamlit to skip execution if the function has been executed already.
如果函數(shù)已經(jīng)執(zhí)行,則st.cache裝飾器告訴Streamlit跳過執(zhí)行。
The entry point for this file defines the layout for the UI.
該文件的入口點定義UI的布局。
快速演示 (A Quick Demo)
To start the app, run streamlit run filepath.py]. If you are using my repository, this would be streamlit run models/gpt_demo.py
要啟動該應(yīng)用程序,請運行streamlit run filepath.py] 。 如果您使用的是我的存儲庫,則可以streamlit run models/gpt_demo.py
The app should be automatically launched in the browser. If not, it will be at http://localhost:8501/. After the generator is loaded, predictions should be much faster!
該應(yīng)用程序應(yīng)在瀏覽器中自動啟動。 如果不是,它將位于http:// localhost:8501 / 。 生成器加載后,預(yù)測應(yīng)該快得多!
I haven’t used this yet, but if you click on the hamburger menu (3 horizontal lines in the top right corner), there is an option for recording your demo using Streamlit. Pretty neat.
我還沒有用過,但是如果您單擊漢堡菜單(右上角有3條水平線),則可以使用Streamlit錄制演示。 漂亮整齊。
讓GPT-2完成一些想法! (Let’s have GPT-2 finish some thoughts!)
我渴望... (I am craving …)
建立時間機(jī)器的秘訣... (The secret to building a time machine …)
And there you have it — a pretty web app built in under 5 minutes.
在那里,您可以在5分鐘之內(nèi)構(gòu)建一個漂亮的Web應(yīng)用程序。
感謝您的閱讀! (Thank you for reading!)
If you enjoyed this article, check out my other articles on Data Science, Math and Programming. Follow me on Medium for the latest updates. 😃
如果您喜歡這篇文章,請查看我關(guān)于數(shù)據(jù)科學(xué),數(shù)學(xué)和編程的其他文章。 按照我在Medium上的最新更新。 😃
I am also building a comprehensive set of free Data Science lessons and practice problems at www.dscrashcourse.com as a hobby project.
我還在業(yè)余愛好www.dscrashcourse.com上構(gòu)建了一套全面的免費數(shù)據(jù)科學(xué)課程,并練習(xí)了問題。
If you want to support my writing, consider using my affiliate link the next time you sign up for a Coursera course. Full disclosure — I receive a commission for every enrollment, but it comes at no extra cost for you.
如果您想支持我的寫作,請在下次注冊Coursera課程時考慮使用“我的會員”鏈接 。 完全披露-每次注冊我都會獲得傭金,但這對您沒有任何額外費用。
Thank you again for reading! 📕
再次感謝您的閱讀! 📕
翻譯自: https://towardsdatascience.com/prototyping-machine-learning-models-with-streamlit-1134c34e9620
機(jī)器學(xué)習(xí) 對模型進(jìn)行懲罰
總結(jié)
以上是生活随笔為你收集整理的机器学习 对模型进行惩罚_使用Streamlit对机器学习模型进行原型制作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 游戏名字女生霸气短一点怎么起(4399小
 - 下一篇: warframe侵袭任务是什么(Warf