pygrid教程3--本地部署pygrid 网络

27 篇文章 7 订阅
11 篇文章 12 订阅
https://github.com/OpenMined/PyGrid/blob/dev/examples/Part%202%20-%20Launch%20a%20PyGrid%20Network%20Locally.ipynb
在本地启动网格网络
在本教程中,您将学习如何将网格网络部署到本地计算机,然后使用PySyft与之交互。

警告:网格节点在线发布数据集,仅供实验使用。部署节点需要您自担风险。不要将OpenGrid与您希望保持私有的任何数据/模型一起使用。

为了在本地运行网格网络,您将需要运行两个不同的应用程序:网格网关和一个或多个网格工作程序。在本教程中,我们将使用此处提供的websocket应用程序启动网格工作器。

 

启动网格网关

步骤1:下载代码库

git clone https://github.com/OpenMined/PyGrid/

 

步骤2:下载依赖项 您需要安装应用程序依赖项。

我们建议设置一个独立的conda环境,以避免库版本出现问题。

您可以通过运行以下命令安装依赖项:

cd PyGrid/gateway/
pip install -r requirements.txt
步骤3:启动网关应用
然后,要启动该应用程序,只需运行gateway.py脚本。 --start_local_db自动启动本地数据库,因此您无需自己配置。
python gateway.py --start_local_db --port=<port_number>

将在这个地址启动服务: http://0.0.0.0/<port_number>.

 

要检查在运行此应用程序时还可以使用哪些其他参数,请运行:

python gateway.py --help
让我们在端口5000上启动网格网关
python gateway.py --port=5000

 

太好了,因此,如果您的应用程序成功启动,脚本应该仍在运行。

 

启动Grid Worker应用
步骤4:启动Grid Worker应用
这与第1部分中已经描述的相同过程。但是,在启动名为--gateway_url的应用程序时,我们添加了一个新参数,该参数应等于Grid网络使用的地址,此处为“ http:// localhost:5000”。

让我们开始两个worker:

鲍勃在端口3000
爱丽丝在端口3001
python websocket_app.py --db_url=redis:///redis:6379 --id=bob --port=3000 --gateway_url=http://localhost:5000
python websocket_app.py --db_url=redis:///redis:6379 --id=alice --port=3001 --gateway_url=http://localhost:5000

 

 

启动Grid网关后,我们应该始终启动工作人员!! 太好了,因此,如果您的应用程序成功启动,脚本应该仍在运行。

 

步骤5:开始与Grid Gateway和工作人员进行通信 让我们开始与网关和工作人员进行通信。

 

In [4]:

# General dependencies
import torch as th
import syft as sy
from syft.grid.public_grid import PublicGridNetwork
from syft.workers.node_client import NodeClient
hook = sy.TorchHook(th)
WARNING:root:Torch was already hooked... skipping hooking process

In [2]:

GRID_ADDRESS = 'localhost'
GRID_PORT = '5000'

gateway = PublicGridNetwork(hook,"http://" + GRID_ADDRESS + ":" + GRID_PORT)

In [5]:

# WARNING: We should use the same id and port as the one used to start the app!!!
bob = NodeClient(hook, "ws://bob:3000")

# WARNING: We should use the same id and port as the one used to start the app!!!
alice = NodeClient(hook, "ws://alice:3001")

 

步骤6:像正常使用PySyft 现在,您可以像使用其他任何普通PySyft工人一样简单地使用创建的工人。有关PySyft如何工作的更多信息,请参阅PySyft教程:https://github.com/OpenMined/PySyft/tree/master/examples/tutorials

In [6]:

x = th.tensor([1,2,3,4]).send(bob)
x

Out[6]:

(Wrapper)>[PointerTensor | me:7311489525 -> Bob:28859851125]

In [7]:

y = x + x
y

Out[7]:

(Wrapper)>[PointerTensor | me:89355944812 -> Bob:43871047975]

In [8]:

y.get()

Out[8]:

tensor([2, 4, 6, 8])

 

步骤7:在网格网络上执行操作 到目前为止,我们并没有做任何不同的事情,但这就是魔术:我们可以与网络进行交互以查询有关它的常规信息。

In [9]:

x = th.tensor([1, 2, 3, 4, 5]).tag("#tensor").send(bob)

We can search for a tensor in the entire network, and get pointers to all tensors.

In [10]:

gateway.search("#tensor")

Out[10]:

{'Bob': [(Wrapper)>[PointerTensor | me:9865241336 -> Bob:2228316112]
  	Tags: #tensor 
  	Shape: torch.Size([5])]}

In [11]:

y = th.tensor([1, 2, 3, 4, 5]).tag("#tensor").send(alice)

In [12]:

gateway.search("#tensor")

Out[12]:

{'Bob': [(Wrapper)>[PointerTensor | me:39675731340 -> Bob:2228316112]
  	Tags: #tensor 
  	Shape: torch.Size([5])],
 'Alice': [(Wrapper)>[PointerTensor | me:69033538805 -> Alice:84105580423]
  	Tags: #tensor 
  	Shape: torch.Size([5])]}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值