?? 作者:韓信子@ShowMeAI
?? 機器學習實戰系列:https://www.showmeai.tech/tutorials/41
?? 深度學習實戰系列:https://www.showmeai.tech/tutorials/42
?? 自然語言處理實戰系列:https://www.showmeai.tech/tutorials/45
?? 本文地址:https://www.showmeai.tech/article-detail/405
?? 宣告:著作權所有,轉載請聯系平臺與作者并注明出處
?? 收藏ShowMeAI查看更多精彩內容
?? 引言
??Galactica 是 Meta AI 開源的大型語言模型,基于 Transformer 架構構建,主要使用科學文章和研究論文作為資料,并使用 ??GROBID 庫將檔案從 pdf 轉換為文本作為語料進行學習,
Galactica 模型使用單個模型管理多個科學任務,可以完成推理、創建講義、預測資料參考等,有以下特點:
- 模型包括125M-120B引數之間的5種不同尺寸,
- 該模型使用 2048 長度的背景關系視窗,
- 用于管理特定資料型別的“專業”標記化方法,
Galactica 模型在面向科學的資料集上做到了最先進的性能,與升級后的 GPT-3 或 OPT 相比,它在 TruthfulQA 資料集中問題結果更少,可作為開源專案使用,在本篇內容中,ShowMeAI就帶大家一起體驗一下這個科學界的巨型語言模型,
?? 實踐
?? 安裝與加載
我們可以通過如下命令安裝Galactica模型:
pip install git+https://github.com/paperswithcode/galai
注意:Galactica 模型適用于 Python 版本 3.8 和 3.9,目前Python 3.10 及更高版本時模型安裝失敗,主要是由于 promptsource-library 依賴要求,
使用下述命令匯入模型:
import galai as gal
通過load_model函式加載模型,
model = gal.load_model("base", num_gpus = 1)
加載模型時可以指定加載的預訓練模型版本,我們在這里使用“base”版本,模型包括 1.3B(13億)引數,可選的版本包括“mini”,“base”,“standard”,“large” 和 “huge”,引數量從 125m 到 120b,
更大的模型需要更多記憶體與計算資源,我們在這里基于記憶體情況選擇“base”版本,它消耗大約 11GB 的記憶體,
load_model的第2個引數是可選的,它指定GPU的數量,
?? 模型使用示例
下面我們開始使用和體驗模型,下面是一個百科解釋類的示例:
model.generate("We can explain Brain as", new_doc=True, top_p=0.7, max_length=200)
模型包括其他引數,我們可以在引數設定中限制輸出文本長度,這些引數類似于 GPT-3 模型,
模型輸出的結果如下:
We can explain Brain as a computer program that takes in data from the external world, and produces an output as a result. The Brain is the machine that makes decisions about what to do. The Brain is the part of the brain that is made up of neurons, the basic building blocks of the brain. Neurons are the smallest units of the brain. Each neuron contains a membrane and a set of synapses that allow it to communicate with other neurons.\n\n[IMAGE]\n\nFigure Caption: Figure 10.2.110.2.1: Neurons are the smallest units of the brain.\n\n# What are the Functions of Neurons?\n\nNeurons are the basic building blocks of the brain. The brain is the part of the body that is made up of neurons. Neurons communicate with each other using chemical signals called neurotransmitters. The brain has many different types of neurons. The different types of neurons in the brain are called neurons of the different types. Neurons of different types'
?? HuggingFace+Galactica
Galactica 模型也可以使用 HuggingFace 加載和使用,我們來看看這個程序,首先我們匯入工具庫:
!pip install accelerate #to run with the gpu
from transformers import AutoTokenizer, OPTForCausalLM
注意:使用 GPU 運行模型時需要accelerate庫,當僅使用 CPU 運行模型時,我們可以跳過安裝“accelerate”庫,當僅使用 CPU 運行時,該模型很慢,因此,如果大家有 GPU 資源,我們盡量使用GPU運行它,
我們接下來選擇模型版本,不同大小的模型分別為“125m”、“1.3b”、“6.7b”、“30b”和“120b”,我們現在將使用以下代碼運行 1.25 億個引數的最小版本:
tokenizer = AutoTokenizer.from_pretrained("facebook/galactica-125m")
model = OPTForCausalLM.from_pretrained("facebook/galactica-125m", device_map="auto")
如果要使用其他版本,大家只需將125m換成其他的版本(“1.3b”、“6.7b”、“30b”和“120b”)即可,
加載完模型之后我們來測驗一下,這次我們來測驗一下模型的推理推斷能力,我們以文本形式提供輸入:
input_text = "Car 1 speed is 30km/h and Car 2 speed is 50km/h. Which car travels faster and how much? <work>"
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda") #when running with gpu is required to add the ".to("cuda")"
我們使用最小版本的Galactica 模型,就準確回傳此推理任務的正確答案,如下所示:
Car 1 travels faster than Car 2 (30km/h vs. 50km/h). calc_1.py
result = 30/50 with open(“output.txt”, “w”) as file: file.write(str(round(result)))<<run: “calc_1.py”>> <<read: “output.txt”>> 10 So 10 km. Car 1 travels faster than Car 2 (50km/h vs. 30km/h). calc_2.py ```result = 50/30 … Answer: 20
參考資料
- ?? Galactica 官方網站:https://www.galactica.org/
- ?? GROBID 官方網站:https://grobid.readthedocs.io/en/latest/Introduction/
- ?? Galactica: A Large Language Model for Science:https://galactica.org/static/paper.pdf
推薦閱讀
- ?? 資料分析實戰系列 :https://www.showmeai.tech/tutorials/40
- ?? 機器學習資料分析實戰系列:https://www.showmeai.tech/tutorials/41
- ?? 深度學習資料分析實戰系列:https://www.showmeai.tech/tutorials/42
- ?? TensorFlow資料分析實戰系列:https://www.showmeai.tech/tutorials/43
- ?? PyTorch資料分析實戰系列:https://www.showmeai.tech/tutorials/44
- ?? NLP實戰資料分析實戰系列:https://www.showmeai.tech/tutorials/45
- ?? CV實戰資料分析實戰系列:https://www.showmeai.tech/tutorials/46
- ?? AI 面試題庫系列:https://www.showmeai.tech/tutorials/48
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/539416.html
標籤:其他
上一篇:華為云發布CodeArts Req需求管理工具,讓需求管理化繁為簡
下一篇:快來給你的寵物視頻加個表情特效吧
