建構一個大型AI系統,常常會在許多地方呼叫模型,但是模型佔用資源龐大,不可以無限制的實例。
當然可以透過傳遞的方式將模型傳給各別模組,但是可能會變成一個傳遞地獄;或是透過約定一個全域變數的方式來達成,這方法可行但仍然不夠好。
這時候就輪到單例模式登場了,單例模式只允許同個類別存在一個實例,若多次執行類別實例動作,則返回已經實例化的類別。沒有傳遞與管理全域變數的問題!
下面的範例使用一個裝飾器(decorator)來達到單例目的,而從輸出可以發現我們成功避免多次實例同個類別。
FROM python:3 as build-system | |
RUN pip install -U pip | |
COPY requirements.txt requirements.txt | |
### create temporary image used to download and vendor packages using private key ### | |
FROM build-system as intermediate | |
# add credentials on build |
對Python有進階認識的可能會知道可以直接從github repo安裝套件,對public repo可以這樣做
pip install git+https://github.com/USERNAME/REPO.git
那如果是一個private repo呢?這時候我們就必須使用ssh來進行身份認證
pip install [email protected]/username/private_repo.git
[{"_id": "0", "_models": ["qmst", "beam_search", "navie_qgg"], "article": "Hi, friends! Welcome to our Family Rock Band. There are four members in our band. They are Wangwang, Mimi, Yingying and I. Wangwang is my dog. It can sing and dance. Mimi is my cat. It can't dance but it can sing and do Chinese kung fu. Yingying is a parrot . It can sing very well. I can play the guitar. When I play the guitar, they sing and dance. We have a show in our home every Sunday evening. Many boys and girls come to my show. They like it very much.", "questionGroups": [["Who can dance?", "Wangwang, Mimi and Yingying can all _ .", "Please come and watch our show on _ .", "Mimi can _ .", "Yingying is a _ ."], ["_ is my dog.", "We have a show in our home every Sunday evening because _ .", "What does Yingying like?", "How many members are in Wangwang's band?", "Who can play the guitar?"], ["We have a show in our home every Sunday evening.", "Wangwang is _ .", "It can sing and do Chinese kung fu.", "What is Wangwang's |
最近做DL實驗發現除了主要研究的核心,最花心力的就是維護的你training pipline 從資料處理、訓練、預測與算分到加入中斷點恢復,各種超參數與模型版本管理。 如果要一直驗證與處理這些問題,實在是很力不從心,好在大家都有同樣的困擾,於是PL出現了,根據官方說法
PyTorch Lightning is just organized PyTorch You do the research. Lightning will do everything else.
就是這麼簡單!不過要體會第二點,我自己覺得是還有段距離,除了對框架本身要熟悉,目前PL也沒有到非常穩定(1.2.x),存在一些小BUG
model.zero_grad() # Reset gradients tensors | |
for i, (inputs, labels) in enumerate(training_set): | |
predictions = model(inputs) # Forward pass | |
loss = loss_function(predictions, labels) # Compute loss function | |
loss = loss / accumulation_steps # Normalize our loss (if averaged) | |
loss.backward() # Backward pass | |
if (i+1) % accumulation_steps == 0: # Wait for several backward steps | |
optimizer.step() # Now we can do an optimizer step | |
model.zero_grad() # Reset gradients tensors | |
if (i+1) % evaluation_steps == 0: # Evaluate the model when we... |