天天看點

Scikit-Learn 和大模型 LLM 強強聯手!

作者:資料派THU

我們以前介紹Pandas和ChaGPT整合,這樣可以不了解Pandas的情況下對DataFrame進行操作。

現在又有人開源了Scikit-LLM,它結合了強大的語言模型,如ChatGPT和scikit-learn。但這個并不是讓我們自動化scikit-learn,而是将scikit-learn和語言模型進行整合,scikit-learn也可以處理文本資料了。

Scikit-learn

Scikit-learn(簡稱sklearn)是一個用于機器學習的開源Python庫,它提供了豐富的工具和函數,用于建構和應用各種機器學習模型。作為一個功能強大且易于使用的工具,scikit-learn已經成為機器學習領域中最受歡迎的庫之一。

Scikit-learn庫提供了包括分類、回歸、聚類、降維和模型選擇等常見機器學習任務的算法和工具。它支援各種監督學習和無監督學習方法,例如支援向量機(SVM)、随機森林(Random Forests)、邏輯回歸(Logistic Regression)、K均值聚類(K-Means Clustering)、主成分分析(PCA)等。這些算法都經過優化和實作,以便在大規模資料集上高效運作。

除了算法和模型外,scikit-learn還提供了資料預處理、特征選擇和評估等工具。它具有廣泛的資料轉換和特征提取功能,可以幫助您處理和準備資料集。此外,scikit-learn還提供了用于模型評估和參數選擇的常用名額和技術,例如交叉驗證和網格搜尋。

Scikit-learn的設計理念之一是提供一緻且易于使用的API接口。這使得使用者可以輕松地在不同的機器學習任務之間切換和嘗試不同的模型。它還具有豐富的文檔和示例代碼,為使用者提供了學習和使用的資源。

除了上述功能之外,scikit-learn還與其他Python庫和工具緊密內建,例如NumPy、SciPy和Matplotlib,使得使用者可以友善地與這些庫進行互動和擴充。

LLM

大模型LLM(Large Language Model)是指基于深度學習的大規模語言模型。這些模型通過訓練大量的文本資料,例如網際網路上的海量文本,可以生成具有語義和文法正确性的人類語言。這些模型的訓練過程依賴于深度神經網絡和強大的計算資源。

大模型LLM的代表性示例是OpenAI的GPT(Generative Pre-trained Transformer)系列,其中包括最新的GPT-3。這些模型具有數十億個參數,并且在多個語言任務上表現出色,例如文本生成、自動問答、文本分類和機器翻譯等。

大模型LLM的訓練通常分為兩個階段:預訓練和微調。在預訓練階段,模型使用大規模文本資料進行無監督學習,通過預測下一個單詞或填充遮罩等任務來學習語言的統計結構和上下文資訊。在微調階段,模型使用特定任務的有監督資料集進行有針對性的訓練,以适應該任務的要求。這種兩階段訓練的方式使得大模型LLM可以在各種語言任務上展現出強大的通用性。

大模型LLM的優勢在于它們可以了解和生成複雜的語言結構,具備較強的語言了解和生成能力。它們可以自動生成連貫的文本、回答自然語言問題,并在某些情況下甚至能夠表現出創造性。這使得它們在自然語言處理、智能對話系統、内容生成等領域具有廣泛的應用潛力。

在這裡給大家分享一篇來自Deephub Imba的文章,如何結合使用scikit和大模型LLM。

安裝

pip install scikit-llm           

既然要與Open AI的模型整合,就需要他的Key,從Scikit-LLM庫中導入SKLLMConfig子產品,并添加openAI密鑰:

# importing SKLLMConfig to configure OpenAI API (key and Name)
 from skllm.config import SKLLMConfig


 # Set your OpenAI API key
 SKLLMConfig.set_openai_key("<YOUR_KEY>")


 # Set your OpenAI organization (optional)
 SKLLMConfig.set_openai_org("<YOUR_ORGANIZATION>")           

ZeroShotGPTClassifier

通過整合ChatGPT不需要專門的訓練就可以對文本進行分類。ZeroShotGPTClassifier,就像任何其他scikit-learn分類器一樣,使用非常簡單。

# importing zeroshotgptclassifier module and classification dataset
 from skllm import ZeroShotGPTClassifier
 from skllm.datasets import get_classification_dataset


 # get classification dataset from sklearn
 X, y = get_classification_dataset()


 # defining the model
 clf = ZeroShotGPTClassifier(openai_model="gpt-3.5-turbo")


 # fitting the data
 clf.fit(X, y)


 # predicting the data
 labels = clf.predict(X)           

Scikit-LLM在結果上經過了特殊處理,確定響應隻包含一個有效的标簽。如果響應缺少标簽,它還可以進行填充,根據它在訓練資料中出現的頻率為你選擇一個标簽。

對于我們自己的帶标簽的資料,隻需要提供候選标簽的清單,代碼是這個樣子的:

# importing zeroshotgptclassifier module and classification dataset
 from skllm import ZeroShotGPTClassifier
 from skllm.datasets import get_classification_dataset


 # get classification dataset from sklearn for prediction only


 X, _ = get_classification_dataset()


 # defining the model
 clf = ZeroShotGPTClassifier()


 # Since no training so passing the labels only for prediction
 clf.fit(None, ['positive', 'negative', 'neutral'])


 # predicting the labels
 labels = clf.predict(X)
MultiLabelZeroShotGPTClassifier
多标簽也類似


 # importing Multi-Label zeroshot module and classification dataset
 from skllm import MultiLabelZeroShotGPTClassifier
 from skllm.datasets import get_multilabel_classification_dataset


 # get classification dataset from sklearn
 X, y = get_multilabel_classification_dataset()


 # defining the model
 clf = MultiLabelZeroShotGPTClassifier(max_labels=3)


 # fitting the model
 clf.fit(X, y)


 # making predictions
 labels = clf.predict(X)           

建立MultiLabelZeroShotGPTClassifier類的執行個體時,指定要配置設定給每個樣本的最大标簽數量(這裡:max_labels=3)

資料沒有沒有标簽怎麼辦?可以通過提供候選标簽清單來訓練沒有标記資料的分類器。y的類型應該是List[List[str]]。下面是一個沒有标記資料的訓練示例:

# getting classification dataset for prediction only
 X, _ = get_multilabel_classification_dataset()


 # Defining all the labels that needs to predicted
 candidate_labels = [
     "Quality",
     "Price",
     "Delivery",
     "Service",
     "Product Variety"
 ]


 # creating the model
 clf = MultiLabelZeroShotGPTClassifier(max_labels=3)


 # fitting the labels only
 clf.fit(None, [candidate_labels])


 # predicting the data
 labels = clf.predict(X)           

文本向量化

文本向量化是将文本轉換為數字的過程,Scikit-LLM中的GPTVectorizer子產品,可以将一段文本(無論文本有多長)轉換為固定大小的一組向量。

# Importing the necessary modules and classes
 from sklearn.pipeline import Pipeline
 from sklearn.preprocessing import LabelEncoder
 from xgboost import XGBClassifier


 # Creating an instance of LabelEncoder class
 le = LabelEncoder()


 # Encoding the training labels 'y_train' using LabelEncoder
 y_train_encoded = le.fit_transform(y_train)


 # Encoding the test labels 'y_test' using LabelEncoder
 y_test_encoded = le.transform(y_test)


 # Defining the steps of the pipeline as a list of tuples
 steps = [('GPT', GPTVectorizer()), ('Clf', XGBClassifier())]


 # Creating a pipeline with the defined steps
 clf = Pipeline(steps)


 # Fitting the pipeline on the training data 'X_train' and the encoded training labels 'y_train_encoded'
 clf.fit(X_train, y_train_encoded)


 # Predicting the labels for the test data 'X_test' using the trained pipeline
 yh = clf.predict(X_test)           

文本摘要

GPT非常擅長總結文本。在Scikit-LLM中有一個叫GPTSummarizer的子產品。

# Importing the GPTSummarizer class from the skllm.preprocessing module
 from skllm.preprocessing import GPTSummarizer


 # Importing the get_summarization_dataset function
 from skllm.datasets import get_summarization_dataset


 # Calling the get_summarization_dataset function
 X = get_summarization_dataset()


 # Creating an instance of the GPTSummarizer
 s = GPTSummarizer(openai_model='gpt-3.5-turbo', max_words=15)


 # Applying the fit_transform method of the GPTSummarizer instance to the input data 'X'.
 # It fits the model to the data and generates the summaries, which are assigned to the variable 'summaries'
 summaries = s.fit_transform(X)           

需要注意的是,max_words超參數是對生成摘要中單詞數量的靈活限制。雖然max_words為摘要長度設定了一個粗略的目标,但摘要器可能偶爾會根據輸入文本的上下文和内容生成略長的摘要。

總結

ChaGPT的火爆使得泛化模型有了更多的進步,這種進步也給我們日常的使用帶來了巨大的變革,Scikit-LLM就将LLM整合進了Scikit的工作流,如果你有興趣,這裡是源碼:

https://github.com/iryna-kondr/scikit-llm