我是靠谱客的博主 聪慧服饰,最近开发中收集的这篇文章主要介绍fedml 跨数据编程,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

grpc_fedavg_mnist_lr_example

custum_data_and_model

客户端:

import torch

import fedml
from fedml import FedMLRunner
from fedml.data.MNIST.data_loader import download_mnist, load_partition_data_mnist

def load_data(args):
    download_mnist(args.data_cache_dir)
    fedml.logging.info("load_data. dataset_name = %s" % args.dataset)

    (
        client_num,
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dict,
        class_num,
    )=load_partition_data_mnist(
        args,
        args.batch_size,
        train_path=args.data_cache_dir+"/MNIST/train",
        test_path=args.data_cache_dir+"/MNIST/test",
    )

    args.client_num_in_total=client_num
    dataset=[
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dict,
        class_num,
    ]
    return dataset,class_num

class LogisticRegression(torch.nn.Module):
    def __init__(self,input_dim,output_dim):
        super(LogisticRegression,self).__init__()
        self.linear=torch.nn.Linear(input_dim,output_dim)
    def forward(self,x):
        outputs=torch.sigmoid(self.linear(x))
        return outputs

if __name__=="__main__":
    args=fedml.init()
    device=fedml.device.get_device(args)
    dataset,output_dim=load_data(args)
    model=LogisticRegression(28*28,output_dim)
    
    fedml_runner=FedMLRunner(args,device,dataset,model)
    fedml_runner.run()

服务器:

import fedml
import torch

from fedml import FedMLRunner
from fedml.cross_silo import Server
from fedml.data.MNIST.data_loader import download_mnist, load_partition_data_mnist

def load_data(args):
    download_mnist(args.data_cache_dir)
    fedml.logging.info("load_data. dataset_name = %s" % args.dataset)
    (
        client_num,
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dcit,
        class_num,
    )=load_partition_data_mnist(
        args,
        args.batch_size,
        train_path=args.data_cache_dir+"/MNIST/train",
        test_path=args.data_cache_dir+"/MNIST/train",
    )
    args.client_num_in_total=client_num
    dataset=[
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dcit,
        class_num,
    ]
    return dataset,class_num

class LogisticRegression(torch.nn.Module):
    def __init__(self,input_dim,output_dim):
        super(LogisticRegression,self).__init__()
        self.linear=torch.nn.Linear(input_dim,output_dim)

    def forward(self,x):
        outputs=torch.sigmoid(self.linear(x))
        return outputs

if __name__=="__main__":
    args=fedml.init()
    device=fedml.device.get_device(args)
    dataset,output_dim=load_data(args)
    model=LogisticRegression(28*28,output_dim)
    
    fedml_runner=FedMLRunner(args,device,dataset,model)
    fedml_runner.run()

light_sec_agg_example

torch_client

import torch

import fedml
from fedml import FedMLRunner
from fedml.data.MNIST.data_loader import download_mnist, load_partition_data_mnist

def load_data(args):
    download_mnist(args.data_cache_dir)
    fedml.logging.info("load_data. dataset_name = %s" % args.dataset)
    (
        client_num,
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dict,
        class_num,
    )=load_partition_data_mnist(
        args,
        args.batch_size,
        train_path=args.data_cache_dir + "/MNIST/train",
        test_path=args.data_cache_dir + "/MNIST/test",
    )
    args.client_num_in_total=client_num
    dataset=[
        train_data_num,
        test_data_num,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dict,
        class_num,
    ]
    return dataset,class_num

class LogisticRegression(torch.nn.Module):
    def __init__(self,input_dim,output_dim):
        super(LogisticRegression,self).__init__()
        self.linear=torch.nn.Linear(input_dim,output_dim)
    
    def forward(self,x):
        outputs=torch.sigmoid(self.linear(x))
        return outputs

if __name__=="__main__":
    args=fedml.init()
    device=fedml.device.grt_device(args)
    dataset,output_dim=load_data(args)
    model=LogisticRegression(28*28,output_dim)
    
    fedml_runner=FedMLRunner(args,device,dataset,model)
    fedml_runner.run()

torch_sever

import torch

import fedml
from fedml import FedMLRunner
from fedml.data.MNIST.data_loader import download_mnist, load_partition_data_mnist

def load_data(args):
    download_mnist(args.data_cache_dir)
    fedml.logging.info("load_data. dataset_name = %s" % args.dataset)
    (
        client_num,
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dict,
        class_num,
    )=load_partition_data_mnist(
        args,
        args.batch_size,
        train_path=args.data_cache_dir + "/MNIST/train",
        test_path=args.data_cache_dir + "/MNIST/test",
    )
    args.client_num_in_total=client_num
    dataset=[
        train_data_num,
        test_data_num,
        train_data_global,
        test_data_global,
        train_data_local_num_dict,
        train_data_local_dict,
        test_data_local_dict,
        class_num,
    ]
    return dataset,class_num
class LogisticRegression(torch.nn.Module):
    def __init__(self,input_dim,output_dim):
        super(LogisticRegression,self).__init__()
        self.linear=torch.nn.Linear(input_dim,output_dim)
    def forward(self,x):
        outputs=torch.sigmoid(self.linear(x))
        return outputs
if __name__=="__main__":
    args=fedml.init()

    device=fedml.device.get_device(args)
    dataset,output_dim=load_data(args)
    model=LogisticRegression(28*28,output_dim)
    fedml_runner=FedMLRunner(args,device,dataset,model)
    fedml_runner.run()

最后

以上就是聪慧服饰为你收集整理的fedml 跨数据编程的全部内容,希望文章能够帮你解决fedml 跨数据编程所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部