很多同学觉得使用jupyter来调试FATE很不习惯,那么如何使用pycharm来调试FATE,pycharm如何在linux中安装,环境如何选择,本文来进行一站式解答。
pycharm安装
操作环境: centos 7 (有桌面服务)
Pycharm 版本: pycharm-community-2022.3.2
具体思路:下载Pycharm Linux发行版本——放入 Linux虚拟机——解包——运行pycharm.sh脚本进行安装——创建桌面图标——选择本地环境——完成安装
具体操作:
1.进入Pycharm 官网 下载对应的Linux发行版本的 Pycharm.
下载链接:
https://www.jetbrains.com/zh-cn/pycharm/download/#section=linux
![](https://img-blog.csdnimg.cn/img_convert/cba566a8144ac19b8ce91e0d427d5027.png)
2.下载好后得到一个后缀为tar.gz的压缩包 (如:pycharm-community-2022.3.2.tar.gz),然后把这个压缩包通过共享文件夹放入 VM虚拟机的主文件夹下。
进入终端使用root权限解压该压缩包:
tar -zxvf pycharm-community-2022.3.2.tar.gz
解压后如图所示(pycharm-community-2022.3.2.tar.gz文件让我给删除了,所以没有该文件)
![](https://img-blog.csdnimg.cn/img_convert/b488f8ae5d368d1d15fc28a84602aa46.png)
在终端cd进入bin目录
cd pycharm-community-2022.3.2/bin
![](https://img-blog.csdnimg.cn/img_convert/ab020ef6afcd41a72e86751a73160beb.png)
在bin目录下终端运行脚本
. pycharm.sh
![](https://img-blog.csdnimg.cn/img_convert/4f357da2abda9147f05c70d8e839d0fa.png)
3.运行完毕后,进行环境设置
打开standalone_fate_install_1.10.0_release作为项目
之后进行环境的选择
![](https://img-blog.csdnimg.cn/img_convert/548e3fad02fbb43cd84c2f9bdc7d8781.png)
笔者的环境是在下方地址中,读者可根据自己的位置进行调整
/home/meng/standalone_fate_install_1.10.0_release/env/python/venv/bin/python3
4.创建桌面快捷方式
先对指定文件更改权限
# 调整文件权限为可读写删减
sudo chmod -R 777 /usr/share/applications/
创建桌面图标: 在终端输入命令
gedit /usr/share/applications/Pycharm.desktop
在弹出来的文本里输入代码(文件地址自行调整):
[Desktop Entry]
Type=Application
Name=Pycharm
GenericName=Pycharm3
Comment=Pycharm3:The Python IDE
Exec=sh /home/meng/pycharm-community-2022.3.2/bin/pycharm.sh
Icon=/home/meng/pycharm-community-2022.3.2/bin//pycharm.png
Terminal=pycharm
Categories=Pycharm;
![](https://img-blog.csdnimg.cn/img_convert/e5c2b03592a8e146a396018aa388fe82.png)
python文件配置
当我们运行一些案例时,发现有些模块不存在
![](https://img-blog.csdnimg.cn/img_convert/363fc3312453d6dd483f24563d8a75a5.png)
遇到这种问题是由于调用的模块在/home/meng/standalone_fate_install_1.10.0_release/fate/python/文件夹下
![](https://img-blog.csdnimg.cn/img_convert/f6d10a372107cfa817ffb72e1aaef8b6.png)
我们只需在该文件夹下编程即可,或者将该文件夹下的文件复制到自己所需的项目中去。
测试
在我们进行自定义模型时,需要将模型文件保存到/home/meng/standalone_fate_install_1.10.0_release/fate/python/federatedml/nn/model_zoo文件夹下
这里以自定义模型为例,模型文件image_net.py
(保存文件路径
/home/meng/standalone_fate_install_1.10.0_release/fate/python/federatedml/nn/model_zoo/)
import torch as t
from torch import nn
from torch.nn import Module
# the residual component
class Residual(Module):
def __init__(self, ch, kernel_size=3, padding=1):
super(Residual, self).__init__()
self.convs = t.nn.ModuleList([nn.Conv2d(ch, ch, kernel_size=kernel_size, padding=padding) for i in range(2)])
self.act = nn.ReLU()
def forward(self, x):
x = self.act(self.convs[0](x))
x_ = self.convs[1](x)
return self.act(x + x_)
# we call it image net
class ImgNet(nn.Module):
def __init__(self, class_num=10):
super(ImgNet, self).__init__()
self.seq = t.nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=12, kernel_size=5),
Residual(12),
nn.MaxPool2d(kernel_size=3),
nn.Conv2d(in_channels=12, out_channels=12, kernel_size=3),
Residual(12),
nn.AvgPool2d(kernel_size=3)
)
self.fc = t.nn.Sequential(
nn.Linear(48, 32),
nn.ReLU(),
nn.Linear(32, class_num)
)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x = self.seq(x)
x = x.flatten(start_dim=1)
x = self.fc(x)
if self.training:
return x
else:
return self.softmax(x)
![](https://img-blog.csdnimg.cn/img_convert/6de1ddcb292afc43749e50ddc32ff2c1.png)
主文件(与federatedml等文件同目录)
import torch as t
from torch import nn
from pipeline import fate_torch_hook
from pipeline.component import HomoNN
from pipeline.backend.pipeline import PipeLine
from pipeline.component import Reader, Evaluation, DataTransform
from pipeline.interface import Data, Model
t = fate_torch_hook(t)
import os
# bind data path to name & namespace
# fate_project_path = os.path.abspath('../../../../')
host = 10000
guest = 9999
arbiter = 10000
pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host,
arbiter=arbiter)
data_0 = {"name": "mnist_guest", "namespace": "experiment"}
data_1 = {"name": "mnist_host", "namespace": "experiment"}
# 路径根据自己得文件位置及名称进行调整,这里以FATE 1.10.0 版本为例
data_path_0 = '/mnt/mnist/'
data_path_1 = '/mnt/mnist/'
pipeline.bind_table(name=data_0['name'], namespace=data_0['namespace'], path=data_path_0)
pipeline.bind_table(name=data_1['name'], namespace=data_1['namespace'], path=data_path_1)
# 定义reader
reader_0 = Reader(name="reader_0")
reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=data_0)
reader_0.get_party_instance(role='host', party_id=host).component_param(table=data_1)
from pipeline.component.homo_nn import DatasetParam, TrainerParam
model = t.nn.Sequential(
# the class_num=10 is the initialzation parameter for your model
t.nn.CustModel(module_name='image_net', class_name='ImgNet', class_num=10)
)
nn_component = HomoNN(name='nn_0',
model=model, # your cust model
loss=t.nn.CrossEntropyLoss(),
optimizer=t.optim.Adam(model.parameters(), lr=0.01),
dataset=DatasetParam(dataset_name='image'), # use image dataset
trainer=TrainerParam(trainer_name='fedavg_trainer', epochs=3, batch_size=1024, validation_freqs=1),
torch_seed=100 # global random seed
)
pipeline.add_component(reader_0)
pipeline.add_component(nn_component, data=Data(train_data=reader_0.output.data))
pipeline.add_component(Evaluation(name='eval_0', eval_type='multi'), data=Data(data=nn_component.output.data))
pipeline.compile()
pipeline.fit()
![](https://img-blog.csdnimg.cn/img_convert/64d76e014e2d8db7a144b2e6f660bd8d.png)