跳到主要內容

【Hugging Face】Ep.3 前往Datasets掏金趣

圖片來源

 

上一篇「【Hugging Face】Ep.2 初探新手村, 打造專屬的翻譯官」我們學習到如何架設起Hugging Face的環境了,接下來我們就介紹一下重要的Datasets吧!

AI模型訓練最重要的燃料就是「資料」了, 而HuggingFace的Dataset也公開了不少的資料集, 非常適合我們進行練功, 就讓我們先從dataset的部份開始吧!

鎖定目標,縮小範圍

掏金之前最重要的就是找到地點, 而如何快速的找到地點呢? HuggingFace也提供了非常友善的搜尋功能, 總共分成三塊, 左上角依照任務、模型大小…等主題選定, 而每種主題下方包含著不同的子類別,最終搭配關鍵字搜尋出自己需要的資料集。

圖片來源

假設我們選了情緒分類的資料集, 那麼可以來看看資料集的內容長怎樣, 可以觀察到其實資料集很簡單, 只有「文字」與對應的「標籤」。

圖片來源

玩轉資料集

安裝套件

!pip install datasets

載入資料集

使用load_dataset_builder()來載入資料集, 這次載入的資料集為「imdb

from datasets import load_dataset_builder
ds_builder = load_dataset_builder("imdb")

檢查資料集資訊

使用load_dataset_builder()來檢查資料集, 這次檢查的資料集為「imdb

from datasets import load_dataset_builder
ds_builder = load_dataset_builder("imdb")

基本資訊

可以得知這是一個電影的資料集, 包含正向與負向的標籤。

print(ds_builder.info.description)
# Large Movie Review Dataset.

print(ds_builder.info.features)
# This is a dataset for binary sentiment classification containing substantially more data than previous benchmark datasets. We provide a set of 25,000 highly polar movie reviews for training, and 25,000 for testing. There is additional unlabeled data for use as well.
{'text': Value(dtype='string', id=None), 'label': ClassLabel(names=['neg', 'pos'], id=None)}

索引值操作

from datasets import load_dataset

# 載入訓練的資料集
ds = load_dataset("imdb", split='train')
# 第一列
ds[0]
# 最後一列
ds[-1]

過濾

資料集雖然猶如黃金, 但其實夾雜著許多的雜訊, 我們可以透過「過濾」的方式, 篩選出我們需要的特定數據, 例如: 文字包含「U.S」且長度不要太長的資料才要進行訓練, 因此我們可以這樣做。

ds1 = ds.filter(lambda x: 'U.S' in x['text'] and len(x['text']) < 500)

ds1[:3]
{
'text': [
  'It is not un-common to see U.S. re-makes of foreign movies that fall flat on their face, but here is the flip side!!! This is an awful re-make of the U.S. movie "Wide Awake" by the British!<br /><br />"Wide Awake" is strange but entertaining and funny! "Liam" on the other hand is just strange. I must give credit to "Liam" for one thing, and that is making it clear that I made the right choice in changing my religion!',
  'I saw this movie on Comedy Central a few times. This movie was pretty good. It\\'s an interesting adventure with the life of Sunny Davis, who is arranged to marry the king of Ohtar, so that the U.S. can get an army base there to balance power in the Middle East. Some good jokes, including "Sunnygate." I also just loved the ending theme. It gave me great political spirit. Ten out of ten was my rating for this movie.',
  '"Antwone Fisher" tells of a young black U.S. Navy enlisted man and product of childhood abuse and neglect (Luke) whose hostility toward others gets him a stint with the base shrink (Washington) leading to introspection, self appraisal, and a return to his roots. Pat, sanitized, and sentimental, "Antwone Fisher" is a solid feel-good flick about the reconciliation of past regrets and closure. Good old Hollywood style entertainment family values entertainment with just a hint of corn. (B)'],
'label': [0, 1, 1]
}

更多的操作方式

上面我們介紹了基本用法,需要更多資料集的操作方式請參考「datasets/process」。

今天的範例都在這裡「📦huggingface/hugging_face_datasets.ipynb」歡迎自行取用。

如何使用請參閱「【Google Colab系列】台股分析預備式: Colab平台與Python如何擦出火花?」。

結語

看起來HuggingFace除了很好的管控資料集之外, 也設計了非常強大的資料集處理API, 最重要的是非常有遠見, 一開始就制定好規範, 大家只要照著標準化的方式上傳資料集就能夠使用標準的API進行資料集處理。

喜歡撰寫文章的你,不妨來了解一下:

Web3.0時代下為創作者、閱讀者打造的專屬共贏平台 - 為什麼要加入?

歡迎加入一起練習寫作,賺取知識!

更多關於【Hugging Face 系列】…

留言

這個網誌中的熱門文章

java西元民國轉換_各種不同格式

C#資料庫操作(新增、修改、刪除、查詢)

【Excel好好玩】 自己的資產自己管!善用Google Sheet來幫我們評估貸款

這次介紹的主題是關於Excel的貸款還款計畫試算,我們人生中總會遇到需要大筆金額的花費,但當資金不夠時就得進行貸款,而貸款之前如果我們能夠審慎評估,並分析自己的還款能力之後在進行凍作,相信風險會小很多,因此就自己動動手來使用Google Sheet進行試算吧! 基本資料 ● 貸款總額: 1000000 ● 貸款期數: 84月 ● 年利率: 2.11% ● 月利率: 0.18% P.S 月利率 = 年利率 / 12 重要函式 PMT : 這是Google Sheet內建的重要年金計算公式,我們可以善用這個公式來計算固定利率及期數的固定攤還本息。因為PMT函式計算出的結果為負數,所以前面加上-號轉成正數。 動手做 首先我們在Excel表上列出我們的基本資料 圖片來源 其中月利率的部分就使用公式「=B4/12」 接著我們填上第一列的期數跟餘額 圖片來源 =B2 =B3 使用關鍵PMT函數來計算本息的部分 因為PMT函式計算出的結果為負數,所以前面加上-號轉成正數。 -PMT(貸款利率(月利率), 貸款期數, 貸款總額) =-PMT($B$5,$B$3,$B$2) 圖片來源 計算利息 利息 = 貸款餘額 x 月利率 =B8*$B$5 圖片來源 計算本金 本金 = 本息 - 利息 =C8-D8 圖片來源 製作第二列餘額的部分 餘額的部分 = 上一期的餘額 - 上一期的本金 圖片來源 接著拖曳該兩列往下拉,即可查看每一期的利息與本金 圖片來源 結語 雖然市面上已經有很多貸款銀行都提供了試算功能,但如果我們想要進一步管理自己的資產時,就需要將每一期的金額給計算出來,因此才會將公式運用在Excel表,讓我們的資產管理表能夠結合負債,進一步評估我們理財行動的下一步,希望這樣的經驗可以幫助到正在理財道路上打拼的夥伴,讓我們透過有效的管理,幫助荷包長大吧! 喜歡撰寫文章的你,不妨來了解一下: Web3.0時代下為創作者、閱讀者打造的專屬共贏平台 — 為什麼要加入? 歡迎加入一起練習寫作,賺取知識,累積財富!