一、單機部署
單機版提供3種部署方式,這里選擇在主機中安裝FATE(官方建議使用Docker鏡像,但不熟悉Docker的人容易找不到FATE路徑)
1.1虛擬機配置
使用虛擬機VMware進行實驗,實驗過程中隨時拍攝快照,節(jié)約重裝時間。
項目 | Value |
---|---|
虛擬機配置 | 內(nèi)存4G + 硬盤150G |
操作系統(tǒng) | centos 7 |
1.2安裝python
- 這里不重復寫了,請參考(Ubuntu安裝python)(CentOS安裝python)
- 或者安裝anconda–linux版本
1.3端口檢查
檢查端口8080、9360、9380是否被占用
netstat -apln|grep 8080
netstat -apln|grep 9360
netstat -apln|grep 9380
1.4獲取安裝包,并解壓
sudo wget https://webank-ai-1251170195.cos.ap-guangzhou.myqcloud.com/fate/1.8.0/release/standalone_fate_install_1.8.0_release.tar.gz --no-check-certificate
tar -xzvf standalone_fate_install_1.8.0_release.tar.gz
1.5安裝
- 進入解壓后的目錄并使用init.sh進行安裝
- 該腳本將自動完成:
安裝必要的操作系統(tǒng)依賴包
安裝python36環(huán)境
安裝pypi依賴包
安裝jdk環(huán)境
配置FATE環(huán)境變量腳本
配置fateflow
配置fateboard
安裝fate client
cd standalone_fate_install_1.8.0_release
bash init.sh init
1.6啟動
bash init.sh status
bash init.sh start
加載環(huán)境變量
source bin/init_env.sh
1.7測試
部署成功后,通過網(wǎng)頁訪問 localhost:8080 可以進入FATE Board頁面,賬號密碼默認為:admin
返回終端,開始測試
- Toy測試
flow test toy -gid 10000 -hid 10000
-
如果成功,屏幕顯示類似下方的語句:
success to calculate secure_sum, it is 2000.0(我的顯示的為1999.999999999) -
之后進入FATE Broad,點擊右上角JOBS,看到任務欄出現(xiàn)兩項任務,并且狀態(tài)為success則說明成功。
- 單元測試
fate_test unittest federatedml --yes
如果成功,屏幕顯示類似下方的語句:
there are 0 failed test
1.8安裝FATE-Client、FATE-Test、FATE-Flow、jupyter notebook
1.8.1FATE-Client、FATE-Test
- 為方便使用FATE,安裝便捷的交互工具FATE-Client以及測試工具FATE-Test.
- 請在環(huán)境內(nèi)使用以下指令安裝:
python -m pip install fate-client
python -m pip install fate-test
1.8.2FATE-Flow
- 安裝FATE-Client的過程會安裝好FATE-Flow,這個工具是聯(lián)邦學習端到端流水線的多方聯(lián)邦任務安全調(diào)度平臺,是執(zhí)行任務的核心
- 初始化
flow init --ip 127.0.0.1 --port 9380
1.8.3FATE中的Jupyter Notebook
參考:在Juypter Notebook中構建聯(lián)邦學習任務
上邊如果安裝anaconda的則只需要執(zhí)行下方第二步
- 安裝jupyter notebook
pip install notebook fate-client
啟動 Juypter Notebook 服務并監(jiān)聽 20000 端口,待服務啟動完畢后則可以通過的方式 “IP:Port” 的方式訪問 Notebook
jupyter notebook --ip=0.0.0.0 --port=20000 --allow-root --debug --no-browser --NotebookApp.token= ''--NotebookApp.password= ''
- 保持終端運行的狀態(tài),可以通過"IP:Port"的方式訪問Juypter Notebook,此時notebook中的目錄,就是Ubuntu中fate安裝目錄下的內(nèi)容(按本教程的安裝過程,fate根目錄位于/home/fate/standalone_fate_master_1.8.0/)
二、用FATE從零實現(xiàn)橫向邏輯回歸
在開始本章之前,請確保已經(jīng)安裝Python和FATE單機版
2.1 數(shù)據(jù)集獲取
from sklearn.datasets import load_breast_cancer
import pandas as pd
breast_dataset = load_breast_cancer()
breast = pd.DataFrame(breast_dataset.data, columns=breast_dataset.feature_names)
breast['y'] = breast_dataset.target
breast.head()
2.2 橫向聯(lián)邦數(shù)據(jù)集切分
為了模擬橫向聯(lián)邦建模的場景,我們首先在本地將乳腺癌數(shù)據(jù)集切分為特征相同的橫向聯(lián)邦形式,當前的breast數(shù)據(jù)集有569條樣本,我們將前面的469條作為訓練樣本,后面的100條作為評估測試樣本。
- 從469條訓練樣本中,選取前200條作為公司A的本地數(shù)據(jù),保存為breast_1_train.csv,將剩余的269條數(shù)據(jù)作為公司B的本地數(shù)據(jù),保存為breast_2_train.csv。
- 測試數(shù)據(jù)集可以不需要切分,兩個參與方使用相同的一份測試數(shù)據(jù)即可,文件命名為breast_eval.csv。
from sklearn.datasets import load_breast_cancer
import pandas as pd
breast_dataset = load_breast_cancer()
breast = pd.DataFrame(breast_dataset.data, columns=breast_dataset.feature_names)
breast = (breast-breast.mean())/(breast.std())
col_names = breast.columns.values.tolist()
columns = {}
for idx, n in enumerate(col_names):
columns[n] = "x%d"%idx
breast = breast.rename(columns=columns)
breast['y'] = breast_dataset.target
breast['idx'] = range(breast.shape[0])
idx = breast['idx']
breast.drop(labels=['idx'], axis=1, inplace = True)
breast.insert(0, 'idx', idx)
breast = breast.sample(frac=1)
train = breast.iloc[:469]
eval = breast.iloc[469:]
breast_1_train = train.iloc[:200]
breast_1_train.to_csv('breast_1_train.csv', index=False, header=True)
breast_2_train = train.iloc[200:]
breast_2_train.to_csv('breast_2_train.csv', index=False, header=True)
eval.to_csv('breast_eval.csv', index=False, header=True)
運行完此程序之后會得到三個新的scv格式的數(shù)據(jù)集
2.3 上傳數(shù)據(jù)-修改文件
在數(shù)據(jù)處理完之后,我們需要進行對數(shù)據(jù)進行上傳。我們采用json格式進行上傳。在FATE項目中有寫好的上傳json文件,在examples/dsl/v2/upload文件夾中,upload_conf.json此文件。在我的實驗中,我將此文件復制到了我在FATE目錄下新建的工作目錄下,并對其進行了修改。
{
"file": "examples/data/breast_1_train.csv",#指定數(shù)據(jù)文件
"head":1,
"partition": 10,
"work_mode": 0,
"table_name": "homo_breast_1_train",#指定DTable表名
"namespace": "breast_1_train.csv" #指定DTable表名的命名空間
}
“file”:需要改為自己的csv路徑
“work_model”:0 表示為單機部署模式
“table_name”:需要改為自己的table名
2.4 上傳數(shù)據(jù)-跳轉(zhuǎn)目錄
為了方便后面的敘述統(tǒng)一,我們假設讀者安裝的FATE單機版本目錄為:
fate_dir=/data/projects/fate-1.8.0-experiment/standalone-fate-master-1.8.0/
做完上述準備之后,我們需要進行以下操作
- 跳轉(zhuǎn)到standalone-fate-master-1.8.0目錄下
cd /data/projects/fate-1.8.0-experiment/standalone-fate-master-1.8.0
- 加載環(huán)境變量
source bin/init_env.sh
- 在加載環(huán)境變量的基礎上,在在當前目錄下($fate_dir/examples/federatedml-1.x-examples),在命令行中執(zhí)行下面的命令,即可自動完成上傳和格式轉(zhuǎn)換:
python /opt/standalone_fate_install_1.8.0/fateflow/python/fate_flow/fate_flow_client.py -f upload -c /opt/standalone_fate_install_1.8.0/fate/upload_data/upload_train1.json
或者
flow data upload -c /opt/standalone_fate_install_1.8.0/fate/upload_data/upload_train1.json
出現(xiàn)如下界面說明成功:
2.5 模型訓練與評估
在數(shù)據(jù)完成上傳之后,下面需要對訓練任務進行配置,在FATE項目中已經(jīng)給出了很多寫好的配置,我們可以在此基礎上進行修改就能直接用了。
2.5.1 修改conf和dsl文件參數(shù)
書中的版本與v1.8.0完全發(fā)生了改變,我們需要重新配置dsl和conf文件,且書中是使用訓練集建模,訓練集評估,與建模流程不符,我們改變書中流程,使用訓練集建模,訓練集與測試集分開進行評估。
在這里我使用的是homo_lr_train_conf.json和homo_lr_train_dsl.json,這兩個文件在examples/dsl/v2/homo_logistic_regression目錄中能夠找到。接下來我們需要對其進行修改
-
homo_lr_train_conf.json:用來設置各個組件的參數(shù),比如輸入模塊的數(shù)據(jù)表名,算法模塊的學習率,batch大小,迭代次數(shù)。
一般使用默認值即可,需要修改的地方包括以下幾處。
- role字段:該字段包括guest和host分別對應兩個參與方。需要修改三個參數(shù)。首先是name和namespace,代表訓練數(shù)據(jù)的DTable表名和命名空間;此外,label_name表示的是標簽列對應的屬性名,
{
"role": {
"host": {
"0": {
"reader_0": {
"table": {
"name": "mnist_host_train", #注意此處換成對應的表名,在復制此代碼時需要刪除此注釋
"namespace": "experiment"
}
},
"evaluation_0": {
"need_run": false
}
}
},
"guest": {
"0": {
"reader_0": {
"table": {
"name": "mnist_guest_train",#注意此處換成對應的表名,在復制此代碼時需要刪除此注釋
"namespace": "experiment"
}
}
}
}
}
}
}
- component_parameters字段:用來設置模型訓練的超參數(shù)信息,包括優(yōu)化函數(shù)與學習率,迭代次數(shù)
"component_parameters": {
"common": {
"data_transform_0": {
"with_label": true,
"output_format": "dense"
},
"homo_lr_0": {
"penalty": "L2",
"tol": 1e-05,
"alpha": 0.01,
"optimizer": "sgd",
"batch_size": -1,
"learning_rate": 0.15,
"init_param": {
"init_method": "zeros"
},
"max_iter": 30,
"early_stop": "diff",
"encrypt_param": {
"method": null
},
"cv_param": {
"n_splits": 4,
"shuffle": true,
"random_seed": 33,
"need_cv": false
},
"decay": 1,
"decay_sqrt": true
},
"evaluation_0": {
"eval_type": "binary"
}
}
- homo_lr_train_dsl.json:用來描述任務模塊,將任務模塊以有向無環(huán)圖的形式組合在一起。以下組件模塊構成了最基本的橫向聯(lián)邦模型流水線。
- homo_lr_0:橫向邏輯回歸組件
- evaluation_0:模型評估組件,如果沒有提供測試數(shù)據(jù)集,將自動使用訓練數(shù)據(jù)進行模型評估。
2.5.2 修改結束之后,在命令行輸入以下命令(submit_ job)執(zhí)行模型訓練
python /opt/standalone_fate_install_1.8.0/fateflow/python/fate_flow/fate_flow_client.py -f submit_job -d /opt/standalone_fate_install_1.8.0/fate/dsl_conf/homo_lr_train_dsl.json -c /opt/standalone_fate_install_1.8.0/fate/dsl_conf/homo_lr_train_conf.json
或者文章來源:http://www.zghlxwxcb.cn/news/detail-413947.html
flow job submit -d /opt/standalone_fate_install_1.8.0/fate/dsl_conf/homo_lr_train_dsl.json -c /opt/standalone_fate_install_1.8.0/fate/dsl_conf/homo_lr_train_conf.json
成功后顯示如圖
通過箭頭所指地址我們可以在瀏覽器中查看任務進度和信息文章來源地址http://www.zghlxwxcb.cn/news/detail-413947.html
到了這里,關于2023年3月版聯(lián)邦學習(fate)從主機安裝到實現(xiàn)聯(lián)邦學習的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!