自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Python部署機器學(xué)習(xí)模型的10個實踐經(jīng)驗

開發(fā) 后端
有時候,作為數(shù)據(jù)科學(xué)家,我們會忘記公司付錢讓我們干什么。我們首先是開發(fā)人員,然后是研究人員,然后可能是數(shù)學(xué)家。我們的首要責(zé)任是快速開發(fā)無bug的解決方案。

 有時候,作為數(shù)據(jù)科學(xué)家,我們會忘記公司付錢讓我們干什么。我們首先是開發(fā)人員,然后是研究人員,然后可能是數(shù)學(xué)家。我們的首要責(zé)任是快速開發(fā)無bug的解決方案。

[[333173]]

我們能做模型并不意味著我們就是神。它沒有給我們寫垃圾代碼的自由。

從一開始,我就犯了很多錯誤,我想和大家分享一下我所看到的ML工程中最常見的技能。在我看來,這也是目前這個行業(yè)最缺乏的技能。

我稱他們?yōu)?ldquo;軟件文盲”,因為他們中的很多人都是非計算機科學(xué)課程學(xué)習(xí)平臺(Coursera)的工程師。我自己曾經(jīng)就是

如果要在一個偉大的數(shù)據(jù)科學(xué)家和一個偉大的ML工程師之間招聘,我會選擇后者。讓我們開始吧。

1. 學(xué)會寫抽象類

一旦你開始編寫抽象類,你就會知道它能給你的代碼庫帶來多大的清晰度。它們執(zhí)行相同的方法和方法名稱。如果很多人都在同一個項目上工作,每個人都會開始使用不同的方法。這可能會造成無效率的混亂。

 

  1. import os 
  2. from abc import ABCMeta, abstractmethod 
  3.  
  4.  
  5. class DataProcessor(metaclass=ABCMeta): 
  6.     """Base processor to be used for all preparation.""" 
  7.     def __init__(self, input_directory, output_directory): 
  8.         self.input_directory = input_directory 
  9.         self.output_directory = output_directory 
  10.  
  11.     @abstractmethod 
  12.     def read(self): 
  13.         """Read raw data.""" 
  14.  
  15.     @abstractmethod 
  16.     def process(self): 
  17.         """Processes raw data. This step should create the raw dataframe with all the required features. Shouldn't implement statistical or text cleaning.""" 
  18.  
  19.     @abstractmethod 
  20.     def save(self): 
  21.         """Saves processed data.""" 
  22.  
  23.  
  24. class Trainer(metaclass=ABCMeta): 
  25.     """Base trainer to be used for all models.""" 
  26.  
  27.     def __init__(self, directory): 
  28.         self.directory = directory 
  29.         self.model_directory = os.path.join(directory, 'models') 
  30.  
  31.     @abstractmethod 
  32.     def preprocess(self): 
  33.         """This takes the preprocessed data and returns clean data. This is more about statistical or text cleaning.""" 
  34.  
  35.     @abstractmethod 
  36.     def set_model(self): 
  37.         """Define model here.""" 
  38.  
  39.     @abstractmethod 
  40.     def fit_model(self): 
  41.         """This takes the vectorised data and returns a trained model.""" 
  42.  
  43.     @abstractmethod 
  44.     def generate_metrics(self): 
  45.         """Generates metric with trained model and test data.""" 
  46.  
  47.     @abstractmethod 
  48.     def save_model(self, model_name): 
  49.         """This method saves the model in our required format.""" 
  50.  
  51.  
  52. class Predict(metaclass=ABCMeta): 
  53.     """Base predictor to be used for all models.""" 
  54.  
  55.     def __init__(self, directory): 
  56.         self.directory = directory 
  57.         self.model_directory = os.path.join(directory, 'models') 
  58.  
  59.     @abstractmethod 
  60.     def load_model(self): 
  61.         """Load model here.""" 
  62.  
  63.     @abstractmethod 
  64.     def preprocess(self): 
  65.         """This takes the raw data and returns clean data for prediction.""" 
  66.  
  67.     @abstractmethod 
  68.     def predict(self): 
  69.         """This is used for prediction.""" 
  70.  
  71.  
  72. class BaseDB(metaclass=ABCMeta): 
  73.     """ Base database class to be used for all DB connectors.""" 
  74.     @abstractmethod 
  75.     def get_connection(self): 
  76.         """This creates a new DB connection.""" 
  77.     @abstractmethod 
  78.     def close_connection(self): 
  79.         """This closes the DB connection.""" 

2. 在最前面設(shè)置你的隨機數(shù)種子

實驗的可重復(fù)性是非常重要的,而種子是我們的敵人。抓住它,否則會導(dǎo)致不同的訓(xùn)練/測試數(shù)據(jù)分割和不同的權(quán)值初始化神經(jīng)網(wǎng)絡(luò)。這導(dǎo)致了不一致的結(jié)果。

 

  1. def set_seed(args): 
  2.     random.seed(args.seed) 
  3.     np.random.seed(args.seed) 
  4.     torch.manual_seed(args.seed) 
  5.     if args.n_gpu > 0: 
  6.         torch.cuda.manual_seed_all(args.seed) 

3. 從幾行數(shù)據(jù)開始

如果你的數(shù)據(jù)太大,而你的工作是代碼的后面的部分,如清理數(shù)據(jù)或建模,那么可以使用nrows來避免每次加載巨大的數(shù)據(jù)。當(dāng)你只想測試代碼而不實際運行整個代碼時,請使用此方法。

當(dāng)你的本地PC配置無法加載所有的數(shù)據(jù)的時候,但你又喜歡在本地開發(fā)時,這是非常適用的,

 

  1. df_train = pd.read_csv(‘train.csv’, nrows=1000) 

4. 預(yù)見失敗(成熟開發(fā)人員的標(biāo)志)

一定要檢查數(shù)據(jù)中的NA,因為這些會給你以后帶來問題。即使你當(dāng)前的數(shù)據(jù)沒有,這并不意味著它不會在未來的再訓(xùn)練循環(huán)中發(fā)生。所以無論如何繼續(xù)檢查。

 

  1. print(len(df)) 
  2. df.isna().sum() 
  3. df.dropna() 
  4. print(len(df)) 

5. 顯示處理進度

當(dāng)你在處理大數(shù)據(jù)時,知道它將花費多少時間以及我們在整個處理過程中的位置肯定會讓你感覺很好。

選項 1 — tqdm

 

  1. from tqdm import tqdm 
  2. import time 
  3.  
  4. tqdm.pandas() 
  5.  
  6. df['col'] = df['col'].progress_apply(lambda x: x**2) 
  7.  
  8. text = "" 
  9. for char in tqdm(["a""b""c""d"]): 
  10.     time.sleep(0.25) 
  11.     text = text + char 

選項 2 — fastprogress

 

  1. from fastprogress.fastprogress import master_bar, progress_bar 
  2. from time import sleep 
  3. mb = master_bar(range(10)) 
  4. for i in mb: 
  5.     for j in progress_bar(range(100), parent=mb): 
  6.         sleep(0.01) 
  7.         mb.child.comment = f'second bar stat' 
  8.     mb.first_bar.comment = f'first bar stat' 
  9.     mb.write(f'Finished loop {i}.'

 

使用Python部署機器學(xué)習(xí)模型的10個實踐經(jīng)驗

 

 

6. Pandas很慢

如果你使用過pandas,你就會知道有時它有多慢 —— 尤其是groupby。不用打破頭尋找“偉大的”解決方案加速,只需使用modin改變一行代碼就可以了。

 

  1. import modin.pandas as pd 

7. 統(tǒng)計函數(shù)的時間

不是所有的函數(shù)都是生而平等的

即使整個代碼都能工作,也不意味著你寫的代碼很棒。一些軟件bug實際上會使你的代碼變慢,所以有必要找到它們。使用這個裝飾器來記錄函數(shù)的時間。

 

  1. import time 
  2.  
  3.  
  4. def timing(f): 
  5.     """Decorator for timing functions 
  6.     Usage: 
  7.     @timing 
  8.     def function(a): 
  9.         pass 
  10.     ""
  11.  
  12.     @wraps(f) 
  13.     def wrapper(*args, **kwargs): 
  14.         start = time.time() 
  15.         result = f(*args, **kwargs) 
  16.         end = time.time() 
  17.         print('function:%r took: %2.2f sec' % (f.__name__,  end - start)) 
  18.         return result 
  19.     return wrapper 

8. 不要在云上燒錢

沒有人喜歡浪費云資源的工程師。

我們的一些實驗可以持續(xù)幾個小時。很難跟蹤它并在它完成時關(guān)閉云實例。我自己也犯過錯誤,也見過有人把實例開了好幾天。

這種情況發(fā)生在星期五,離開后,周一才意識到

只要在執(zhí)行結(jié)束時調(diào)用這個函數(shù),你的屁股就再也不會著火了!!

但是將主代碼包裝在try中,此方法也包裝在except中 —— 這樣如果發(fā)生錯誤,服務(wù)器就不會繼續(xù)運行。是的,我也處理過這些情況

讓我們更負責(zé)任一點,不要產(chǎn)生二氧化碳。

 

  1. import os 
  2.  
  3. def run_command(cmd): 
  4.     return os.system(cmd) 
  5.      
  6. def shutdown(seconds=0, os='linux'): 
  7.     """Shutdown system after seconds given. Useful for shutting EC2 to save costs.""" 
  8.     if os == 'linux'
  9.         run_command('sudo shutdown -h -t sec %s' % seconds) 
  10.     elif os == 'windows'
  11.         run_command('shutdown -s -t %s' % seconds) 

9. 創(chuàng)建和保存報告

在建模的某個特定點之后,所有偉大的見解都只來自錯誤和度量分析。確保為自己和你的管理層創(chuàng)建和保存格式良好的報告。

管理層喜歡報告,對嗎?

 

  1. import json 
  2. import os 
  3.  
  4. from sklearn.metrics import (accuracy_score, classification_report, 
  5.                              confusion_matrix, f1_score, fbeta_score) 
  6.  
  7. def get_metrics(y, y_pred, beta=2, average_method='macro', y_encoder=None): 
  8.     if y_encoder: 
  9.         y = y_encoder.inverse_transform(y) 
  10.         y_pred = y_encoder.inverse_transform(y_pred) 
  11.     return { 
  12.         'accuracy': round(accuracy_score(y, y_pred), 4), 
  13.         'f1_score_macro': round(f1_score(y, y_pred, average=average_method), 4), 
  14.         'fbeta_score_macro': round(fbeta_score(y, y_pred, beta, average=average_method), 4), 
  15.         'report': classification_report(y, y_pred, output_dict=True), 
  16.         'report_csv': classification_report(y, y_pred, output_dict=False).replace('\n','\r\n'
  17.     } 
  18.  
  19.  
  20. def save_metrics(metrics: dict, model_directory, file_name): 
  21.     path = os.path.join(model_directory, file_name + '_report.txt'
  22.     classification_report_to_csv(metrics['report_csv'], path) 
  23.     metrics.pop('report_csv'
  24.     path = os.path.join(model_directory, file_name + '_metrics.json'
  25.     json.dump(metrics, open(path, 'w'), indent=4) 

10. 寫好APIs

結(jié)果不好就是不好。

你可以進行很好的數(shù)據(jù)清理和建模,但最終仍可能造成巨大的混亂。我與人打交道的經(jīng)驗告訴我,許多人不清楚如何編寫好的api、文檔和服務(wù)器設(shè)置。我很快會寫另一篇關(guān)于這個的文章,但是讓我開始吧。

下面是在不太高的負載下(比如1000/min)部署經(jīng)典的ML和DL的好方法。

fasbut + uvicorn

  • Fastest — 使用fastapi編寫API,因為它很快。
  • Documentation — 用fastapi寫API讓我們不用操心文檔。
  • Workers — 使用uvicorn部署API

使用4個worker運行這些命令進行部署。通過負載測試優(yōu)化workers的數(shù)量。

 

  1. pip install fastapi uvicorn 
  2. uvicorn main:app --workers 4 --host 0.0.0.0 --port 8000 

 

使用Python部署機器學(xué)習(xí)模型的10個實踐經(jīng)驗

 

 

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2014-10-29 13:52:38

程序員

2022-07-29 09:54:42

數(shù)據(jù)庫分布式

2018-09-10 15:25:29

云計算云安全IT經(jīng)理

2018-10-05 23:26:00

機器學(xué)習(xí)算法數(shù)據(jù)

2024-12-04 14:52:46

2024-09-09 11:45:15

ONNX部署模型

2010-01-05 13:16:59

2015-05-08 10:39:10

InfoQ

2015-05-08 12:47:58

Docker

2015-06-03 14:14:17

dockeropenstackIaaS

2023-11-22 11:15:56

數(shù)據(jù)中心機房

2020-05-29 07:00:00

Python機器學(xué)習(xí)編程語言

2010-01-25 14:25:33

Android Int

2021-07-26 17:22:02

Java

2019-10-23 08:00:00

Flask機器學(xué)習(xí)人工智能

2024-10-12 08:00:00

機器學(xué)習(xí)Docker

2013-10-10 13:50:02

智能交通華為

2011-12-22 09:34:39

需求分析

2023-07-11 10:23:00

Lakehouse數(shù)據(jù)湖

2022-08-10 13:54:40

云存儲存儲私有云
點贊
收藏

51CTO技術(shù)棧公眾號