偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

[[333173]]

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

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

我稱他們?yōu)?ldquo;軟件文盲”,因?yàn)樗麄冎械暮芏嗳硕际欠怯?jì)算機(jī)科學(xué)課程學(xué)習(xí)平臺(tái)(Coursera)的工程師。我自己曾經(jīng)就是

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

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

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

 

  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è)置你的隨機(jī)數(shù)種子

實(shí)驗(yàn)的可重復(fù)性是非常重要的,而種子是我們的敵人。抓住它,否則會(huì)導(dǎo)致不同的訓(xùn)練/測(cè)試數(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ù)開(kāi)始

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

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

 

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

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

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

 

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

5. 顯示處理進(jìn)度

當(dāng)你在處理大數(shù)據(jù)時(shí),知道它將花費(fèi)多少時(shí)間以及我們?cè)谡麄€(gè)處理過(guò)程中的位置肯定會(huì)讓你感覺(jué)很好。

選項(xiàng) 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 

選項(xiàng) 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部署機(jī)器學(xué)習(xí)模型的10個(gè)實(shí)踐經(jīng)驗(yàn)

 

 

6. Pandas很慢

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

 

  1. import modin.pandas as pd 

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

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

即使整個(gè)代碼都能工作,也不意味著你寫的代碼很棒。一些軟件bug實(shí)際上會(huì)使你的代碼變慢,所以有必要找到它們。使用這個(gè)裝飾器來(lái)記錄函數(shù)的時(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. 不要在云上燒錢

沒(méi)有人喜歡浪費(fèi)云資源的工程師。

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

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

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

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

讓我們更負(fù)責(zé)任一點(diǎn),不要產(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)建和保存報(bào)告

在建模的某個(gè)特定點(diǎn)之后,所有偉大的見(jiàn)解都只來(lái)自錯(cuò)誤和度量分析。確保為自己和你的管理層創(chuàng)建和保存格式良好的報(bào)告。

管理層喜歡報(bào)告,對(duì)嗎?

 

  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é)果不好就是不好。

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

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

fasbut + uvicorn

  • Fastest — 使用fastapi編寫API,因?yàn)樗芸臁?/li>
  • Documentation — 用fastapi寫API讓我們不用操心文檔。
  • Workers — 使用uvicorn部署API

使用4個(gè)worker運(yùn)行這些命令進(jìn)行部署。通過(guò)負(fù)載測(cè)試優(yōu)化workers的數(shù)量。

 

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

 

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

 

 

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

2014-10-29 13:52:38

程序員

2018-09-10 15:25:29

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

2022-07-29 09:54:42

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

2024-12-04 14:52:46

2018-10-05 23:26:00

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

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ù)中心機(jī)房

2020-05-29 07:00:00

Python機(jī)器學(xué)習(xí)編程語(yǔ)言

2010-01-25 14:25:33

Android Int

2021-07-26 17:22:02

Java

2019-10-23 08:00:00

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

2024-10-12 08:00:00

機(jī)器學(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

云存儲(chǔ)存儲(chǔ)私有云
點(diǎn)贊
收藏

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