隐私计算实训营:SplitRec:当拆分学习遇上推荐系统

news2024/9/20 1:10:54

拆分学习的概念

拆分学习的核心思想是拆分网络结构。每一个参与方拥有模型结构的一部分,所有参与方的模型合在一起形成一个完整的模型。训练过程中,不同参与方只对本地模型进行正向或者反向传播计算,并将计算结果传递给下一个参与方。多个参与方通过联合模型进行训练直至最终收敛。

一个典型的拆分学习例子:

Alice持有数据和基础模型。Bob只有数据、基础模型和fuse模型。

  1. Alice使用自己的数据和基础模型得到 hidden0,然后发送给Bob。
  2. Bob使用自己的数据和基础模型得到 hidden1
  3. Agg Layer使用 hidden_0 和 hidden_1 作为输入,并输出聚合后的隐层。
  4. Bob把聚合后的隐层作为fuse模型的输入,计算得到梯度。
  5. 梯度被拆分成两部分,分别返回给Alice和Bob。
  6. Alice和Bob使用各自收到的梯度更新基础模型。

SplitRec

SplitRec是隐语拆分学习针对跨域推荐场景中的模型训练所提供的一系列优化算法和策略。

在传统推荐场景中,用户的数据通常需要上传到中央服务器进行模型训练。而跨域推荐场景是指联合分布在不同域的数据进行分布式训练的推荐场景。例如一个用户在一个短视频平台看了很多短视频,在另一个电商平台被推荐相关的广告,电商平台除了自有数据外,也希望从短视频平台的数据中挖掘相关的信息。同时出于数据安全考虑,各平台数据不能被上传到中央服务器进行集中式的机器学习训练,这种联合分布在不同域的数据进行模型训练的场景很适合用联邦学习中的拆分学习。

跨域推荐模型将不同域的用户数据联合起来建模,相比传统推荐系统收集到的数据更多更丰富,同时由于数据分布在不同域,在精度、效率和安全性上都对模型的训练提出了很多挑战,主要有以下三点:

  • 模型效果上,例如DeepFM等复杂模型能否直接放到拆分框架中使用?
  • 训练效率上,模型训练中每个 batch 的前反向计算中的通信是否会严重降低训练效率?
  • 安全性上,通信的中间数据是否会造成信息泄露,引起安全性问题?

SplitRec 在效果、效率和安全方面对拆分模型训练做了很多优化。

  • 模型效果上,SplitRec 提供了拆分 DeepFM、BST、MMoe 等模型的封装。
  • 训练效率上,SplitRec 借由隐语拆分学习框架的能力,提供了压缩、流水并行等策略来提升训练效率。
  • 安全性上,SplitRec提供了安全聚合、差分隐私等安全策略。同时也提供了一些针对拆分学习的攻击方法,来验证不同攻击手段对拆分模型的影响,后续也会更新相关防御方法。

实践:在隐语中使用拆分 DeepFM 算法

DeepFM算法结合了FM和神经网络的长处,可以同时提升低维和高维特征,相比Wide&Deep模型还免去了特征工程的部分。

整体上来看。这个模型可以分成两个部分,分别是FM部分以及Deep部分。这两个部分的输入是一样的,并没有像Wide & Deep模型那样做区分。Deep的部分用来训练这些特征的高维的关联,而FM模型会通过隐藏向量V的形式来计算特征之间的二维交叉的信息。

隐语中的DeepFM

拆分的详细过程可以来看这里:

SplitRec:在隐语中使用拆分 DeepFM 算法(Tensorflow 后端) | SecretFlow v1.9.0b1 | 隐语 SecretFlow

环境设置

import secretflow as sf

# Check the version of your SecretFlow
print('The version of SecretFlow: {}'.format(sf.__version__))

# In case you have a running secretflow runtime already.
sf.shutdown()
sf.init(['alice', 'bob', 'charlie'], address="local", log_to_driver=False)
alice, bob, charlie = sf.PYU('alice'), sf.PYU('bob'), sf.PYU('charlie')

数据集介绍

我们这里将使用最经典的MovieLens数据集来进行演示。 MovieLens是一个开放式的推荐系统数据集,包含了电影评分和电影元数据信息。

我们对数据进行了切分:

- alice: “UserID”, “Gender”, “Age”, “Occupation”, “Zip-code”

- bob: “MovieID”, “Rating”, “Title”, “Genres”, “Timestamp”

下载并处理数据

数据拆分处理

%%capture
%%!
wget https://secretflow-data.oss-accelerate.aliyuncs.com/datasets/movielens/ml-1m.zip
unzip ./ml-1m.zip
# Read the data in dat format and convert it into a dictionary
def load_data(filename, columns):
    data = {}
    with open(filename, "r", encoding="unicode_escape") as f:
        for line in f:
            ls = line.strip("\n").split("::")
            data[ls[0]] = dict(zip(columns[1:], ls[1:]))
    return data
fed_csv = {alice: "alice_ml1m.csv", bob: "bob_ml1m.csv"}
csv_writer_container = {alice: open(fed_csv[alice], "w"), bob: open(fed_csv[bob], "w")}
part_columns = {
    alice: ["UserID", "Gender", "Age", "Occupation", "Zip-code"],
    bob: ["MovieID", "Rating", "Title", "Genres", "Timestamp"],
}
for device, writer in csv_writer_container.items():
    writer.write("ID," + ",".join(part_columns[device]) + "\n")
f = open("ml-1m/ratings.dat", "r", encoding="unicode_escape")

users_data = load_data(
    "./ml-1m/users.dat",
    columns=["UserID", "Gender", "Age", "Occupation", "Zip-code"],
)
movies_data = load_data("./ml-1m/movies.dat", columns=["MovieID", "Title", "Genres"])
ratings_columns = ["UserID", "MovieID", "Rating", "Timestamp"]

rating_data = load_data("./ml-1m/ratings.dat", columns=ratings_columns)


def _parse_example(feature, columns, index):
    if "Title" in feature.keys():
        feature["Title"] = feature["Title"].replace(",", "_")
    if "Genres" in feature.keys():
        feature["Genres"] = feature["Genres"].replace("|", " ")
    values = []
    values.append(str(index))
    for c in columns:
        values.append(feature[c])
    return ",".join(values)


index = 0
num_sample = 1000
for line in f:
    ls = line.strip().split("::")
    rating = dict(zip(ratings_columns, ls))
    rating.update(users_data.get(ls[0]))
    rating.update(movies_data.get(ls[1]))
    for device, columns in part_columns.items():
        parse_f = _parse_example(rating, columns, index)
        csv_writer_container[device].write(parse_f + "\n")
    index += 1
    if num_sample > 0 and index >= num_sample:
        break
for w in csv_writer_container.values():
    w.close()

到此就完成了数据的处理和拆分

得到

alice: alice_ml1m.csv

bob: bob_ml1m.csv

! head alice_ml1m.csv
! head bob_ml1m.csv

构造data_builder_dict

# alice
def create_dataset_builder_alice(
    batch_size=128,
    repeat_count=5,
):
    def dataset_builder(x):
        import pandas as pd
        import tensorflow as tf

        x = [dict(t) if isinstance(t, pd.DataFrame) else t for t in x]
        x = x[0] if len(x) == 1 else tuple(x)
        data_set = (
            tf.data.Dataset.from_tensor_slices(x).batch(batch_size).repeat(repeat_count)
        )

        return data_set

    return dataset_builder


# bob
def create_dataset_builder_bob(
    batch_size=128,
    repeat_count=5,
):
    def _parse_bob(row_sample, label):
        import tensorflow as tf

        y_t = label["Rating"]
        y = tf.expand_dims(
            tf.where(
                y_t > 3,
                tf.ones_like(y_t, dtype=tf.float32),
                tf.zeros_like(y_t, dtype=tf.float32),
            ),
            axis=1,
        )
        return row_sample, y

    def dataset_builder(x):
        import pandas as pd
        import tensorflow as tf

        x = [dict(t) if isinstance(t, pd.DataFrame) else t for t in x]
        x = x[0] if len(x) == 1 else tuple(x)
        data_set = (
            tf.data.Dataset.from_tensor_slices(x).batch(batch_size).repeat(repeat_count)
        )

        data_set = data_set.map(_parse_bob)

        return data_set

    return dataset_builder


data_builder_dict = {
    alice: create_dataset_builder_alice(
        batch_size=128,
        repeat_count=5,
    ),
    bob: create_dataset_builder_bob(
        batch_size=128,
        repeat_count=5,
    ),
}
from secretflow.ml.nn.applications.sl_deep_fm import DeepFMbase, DeepFMfuse
from secretflow.ml.nn import SLModel

NUM_USERS = 6040
NUM_MOVIES = 3952
GENDER_VOCAB = ["F", "M"]
AGE_VOCAB = [1, 18, 25, 35, 45, 50, 56]
OCCUPATION_VOCAB = [i for i in range(21)]
GENRES_VOCAB = [
    "Action",
    "Adventure",
    "Animation",
    "Children's",
    "Comedy",
    "Crime",
    "Documentary",
    "Drama",
    "Fantasy",
    "Film-Noir",
    "Horror",
    "Musical",
    "Mystery",
    "Romance",
    "Sci-Fi",
    "Thriller",
    "War",
    "Western",
]

DeepFMBase有4个参数:

-dnn_units_size: 这个参数需要提供一个list来对dnn部分进行定义,比如[256,32]意思是中间两个隐层分别是256,和32

-dnn_activation: dnn 的激活函数,eg:relu

-preprocess_layer: 需要对输入进行处理,传入一个定义好的keras.preprocesslayer

-fm_embedding_dim: fm vector的维度是多少

# Define alice's basenet
def create_base_model_alice():
    # Create model
    def create_model():
        import tensorflow as tf

        def preprocess():
            inputs = {
                "UserID": tf.keras.Input(shape=(1,), dtype=tf.string),
                "Gender": tf.keras.Input(shape=(1,), dtype=tf.string),
                "Age": tf.keras.Input(shape=(1,), dtype=tf.int64),
                "Occupation": tf.keras.Input(shape=(1,), dtype=tf.int64),
            }
            user_id_output = tf.keras.layers.Hashing(
                num_bins=NUM_USERS, output_mode="one_hot"
            )
            user_gender_output = tf.keras.layers.StringLookup(
                vocabulary=GENDER_VOCAB, output_mode="one_hot"
            )

            user_age_out = tf.keras.layers.IntegerLookup(
                vocabulary=AGE_VOCAB, output_mode="one_hot"
            )
            user_occupation_out = tf.keras.layers.IntegerLookup(
                vocabulary=OCCUPATION_VOCAB, output_mode="one_hot"
            )

            outputs = {
                "UserID": user_id_output(inputs["UserID"]),
                "Gender": user_gender_output(inputs["Gender"]),
                "Age": user_age_out(inputs["Age"]),
                "Occupation": user_occupation_out(inputs["Occupation"]),
            }
            return tf.keras.Model(inputs=inputs, outputs=outputs)

        preprocess_layer = preprocess()
        model = DeepFMbase(
            dnn_units_size=[256, 32],
            preprocess_layer=preprocess_layer,
        )
        model.compile(
            loss=tf.keras.losses.binary_crossentropy,
            optimizer=tf.keras.optimizers.Adam(),
            metrics=[
                tf.keras.metrics.AUC(),
                tf.keras.metrics.Precision(),
                tf.keras.metrics.Recall(),
            ],
        )
        return model  # need wrap

    return create_model
# Define bob's basenet
def create_base_model_bob():
    # Create model
    def create_model():
        import tensorflow as tf

        # define preprocess layer
        def preprocess():
            inputs = {
                "MovieID": tf.keras.Input(shape=(1,), dtype=tf.string),
                "Genres": tf.keras.Input(shape=(1,), dtype=tf.string),
            }

            movie_id_out = tf.keras.layers.Hashing(
                num_bins=NUM_MOVIES, output_mode="one_hot"
            )
            movie_genres_out = tf.keras.layers.TextVectorization(
                output_mode='multi_hot', split="whitespace", vocabulary=GENRES_VOCAB
            )
            outputs = {
                "MovieID": movie_id_out(inputs["MovieID"]),
                "Genres": movie_genres_out(inputs["Genres"]),
            }
            return tf.keras.Model(inputs=inputs, outputs=outputs)

        preprocess_layer = preprocess()

        model = DeepFMbase(
            dnn_units_size=[256, 32],
            preprocess_layer=preprocess_layer,
        )
        model.compile(
            loss=tf.keras.losses.binary_crossentropy,
            optimizer=tf.keras.optimizers.Adam(),
            metrics=[
                tf.keras.metrics.AUC(),
                tf.keras.metrics.Precision(),
                tf.keras.metrics.Recall(),
            ],
        )
        return model  # need wrap

    return create_model

定义Fusenet

def create_fuse_model():
    # Create model
    def create_model():
        import tensorflow as tf

        model = DeepFMfuse(dnn_units_size=[256, 256, 32])
        model.compile(
            loss=tf.keras.losses.binary_crossentropy,
            optimizer=tf.keras.optimizers.Adam(),
            metrics=[
                tf.keras.metrics.AUC(),
                tf.keras.metrics.Precision(),
                tf.keras.metrics.Recall(),
            ],
        )
        return model

    return create_model
base_model_dict = {alice: create_base_model_alice(), bob: create_base_model_bob()}
model_fuse = create_fuse_model()
from secretflow.data.vertical import read_csv as v_read_csv

vdf = v_read_csv(
    {alice: "alice_ml1m.csv", bob: "bob_ml1m.csv"}, keys="ID", drop_keys="ID"
)
label = vdf["Rating"]

data = vdf.drop(columns=["Rating", "Timestamp", "Title", "Zip-code"])
data["UserID"] = data["UserID"].astype("string")
data["MovieID"] = data["MovieID"].astype("string")

sl_model = SLModel(
    base_model_dict=base_model_dict,
    device_y=bob,
    model_fuse=model_fuse,
)
history = sl_model.fit(
    data,
    label,
    epochs=5,
    batch_size=128,
    random_seed=1234,
    dataset_builder=data_builder_dict,
)

到这里,我们已经使用隐语提供的deepfm封装完成了movieLens数据集上的推荐任务训练。

总结

我们通过movieLens数据集上的推荐任务来演示了如何通过隐语来实现DeepFM。

1.下载并拆分数据集;

2.定义好数据处理的dataloader;

3.定义好数据预处理的preprocesslayer,定义好dnn结构,调用DeepFMBase,DeepFMFuse来进行模型定义;

4.使用SLModel进行训练,预测,评估即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2113554.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

文件操作与隐写

一、文件类型的识别 1、文件头完好情况: (1)file命令 使用file命令识别:识别出file.doc为jpg类型 (2)winhex 通过winhex工具查看文件头类型,根据文件头部内容去判断文件的类型 eg:JPG类型 &a…

Wni11 下 WSL 安装 CentOS

Wni11 下 WSL 安装 CentOS 方法一、安装包安装下载包安装安装打开 CentOS1. 从 Windows 终端 打开2. 从 PowerShell 打开 方法二、导入 CentOS 的 tar 文件进行安装0. 查看版本(可选)1. 导出 Docker 容器到 tar 文件2. 将 tar 文件导入 WSL2.1. 导入 tar…

macos安装ArgoCD

本文主要介绍如何在macos上安装并访问argo 我环境上已经安装了minikube,所以只需要启动minikube然后通过命令行安装argocd。 minikube start kubectl create namespace argocd kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/st…

OpenGL(二)-更详细版的三角形

在上篇blog中已经画了一个三角形了,这篇讲解一下一个三角形的渲染过程。 上篇blog中的glbegin搭配glend的流程,在OpenGL3.2中已经被弃用了,3.3以后推荐使用VBOEBOVAO的流程。 图形渲染管线 作用:将三维坐标经过一系列变换&#x…

【Day09】

目录 Mybatis-基础操作-环境准备 Mybatis-基础操作-删除 Mybatis-基础操作-删除(预编译SQL) Mybatis-基础操作-新增 Mybatis-基础操作-新增(主键返回) Mybatis-基础操作-更新 Mybatis-基础操作-查询(根据ID查询) Mybatis-基…

YOLOv8改进 | Conv篇 | YOLOv8引入DWR

1. DWR介绍 1.1 摘要:当前的许多工作直接采用多速率深度扩张卷积从一个输入特征图中同时捕获多尺度上下文信息,从而提高实时语义分割的特征提取效率。 然而,这种设计可能会因为结构和超参数的不合理而导致多尺度上下文信息的访问困难。 为了降低多尺度上下文信息的绘制难度…

【系统分析师】-2024-2010年系统分析师历年论文题目

目录 2024.5月 2023 2022 2021 2020 2019 预测2024年11月 2024.5月 信息系统工程 论基于架构的软件设计方法信息系统工程 论性能测试方法及其应用信息系统工程 论云原生应用开发数据库及应用 论多源数据集成方法及其应用 2023 信息系统工…

HTB-Pennyworth(cve查询 和 exp使用)

前言 各位师傅大家好,我是qmx_07,今天给大家讲解Pennyworth靶场 渗透过程 信息搜集 服务器端口开放了8080http端口 访问网站 服务器使用jenkins cms系统,版本是2.289.1 通过弱口令爆破,账户是root,密码是password 通过命令执行nday 连…

Leetcode面试经典150题-76.最小覆盖子串

解法都在代码里&#xff0c;不懂就留言或者私信 理论上提交这个就是最优解 class Solution {public String minWindow(String s, String t) {if(s.length() < t.length()) {return "";}/**转成字符数组 */char[] sArr s.toCharArray();char[] tArr t.toCharAr…

Docker编译环境的使用(ubuntu)

目录 Ubuntu安装docker 重启docker 拉取镜像 进入docker安装软件 提交docker 添加用户到docker组 进入docker 添加build用户 停止容器 保存docker镜像 load镜像 删除容器 Ubuntu安装docker sudo apt install docker.io 国内可用的源 Welcome to nginx! (tence…

git使用基础教程

(一)Git下载 git官网 - downloads 创建本地数据仓 1.创建文件夹 2.当前目录 cmd ---git init gitee.com注册登录 git创建项目 ide 项目地址本地数据库 ide项目上传云端 (一)git 1.git下载 2.新建仓库 3. 配置Configure---Version Control---Git----path to Git Get from…

某里227逆向分析

声明: 该文章为学习使用,严禁用于商业用途和非法用途,违者后果自负,由此产生的一切后果均与作者无关。 本文章未经许可禁止转载,禁止任何修改后二次传播,擅自使用本文讲解的技术而导致的任何意外,作者均不负责,若有侵权,请联系作者立即删除! 前言 这次会简单的讲解…

【中国国际航空-注册/登录安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 1. 暴力破解密码&#xff0c;造成用户信息泄露 2. 短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉 3. 带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造…

【LeetCode】最接近的三数之和

题目要求 解题思路 这道题解题方法和三数之和解题思路一样&#xff0c;可以参考上一篇博客 代码实现 class Solution { public:int threeSumClosest(vector<int>& nums, int target) {//排序sort(nums.begin(),nums.end());int lennums.size();//固定一个&#x…

流媒体协议RTSP(其二)

欢迎诸位来阅读在下的博文~ 在这里&#xff0c;在下会不定期发表一些浅薄的知识和经验&#xff0c;望诸位能与在下多多交流&#xff0c;共同努力 文章目录 前期博客一、RTSP简介二、请求消息结构三、应答消息结构四、RTSP交互流程 前期博客 流媒体与直播的基础理论&#xff08…

Datawhle X 李宏毅苹果书AI夏令营深度学习笔记之——卷积神经网络的前世今生

一、卷积神经网络简介 卷积神经网络&#xff08;Convolutional Neural Network, CNN&#xff09;是一种深度学习模型&#xff0c;尤其擅长处理图像和视频等高维度的数据。CNN 通过模仿人类视觉系统的工作方式&#xff0c;自动学习数据中的空间层次结构&#xff0c;使得它在计算…

启动第一个docker容器

1 、 docker pull ubuntu:20.04 下载镜像 2、 docker image ls 查看镜像 3、 docker run --nametest -itd 9df6d6105df2 创建并运行一个容器 4、 查看容器 docker ps -a 5、 登录容器 docker exec -it test /bin/bash 6 退出容器 exit 7 删除容器 docker rm 238514292c…

JVM面试真题总结(二)

文章收录在网站&#xff1a;http://hardyfish.top/ 文章收录在网站&#xff1a;http://hardyfish.top/ 文章收录在网站&#xff1a;http://hardyfish.top/ 文章收录在网站&#xff1a;http://hardyfish.top/ volatile关键字能防止指令重排序吗?如何实现? volatile关键字可…

2024国赛数学建模B题完整分析参考论文38页(含模型和可运行代码)

2024 高教社杯全国大学生数学建模完整分析参考论文 B 题 生产过程中的决策问题 目录 摘要 一、问题重述 二、问题分析 三、 模型假设 四、 模型建立与求解 4.1问题1 4.1.1问题1思路分析 4.1.2问题1模型建立 4.1.3问题1样例代码&#xff08;仅供参考&#xff09; 4.…

基于人工智能的图像风格迁移系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像风格迁移是一种计算机视觉技术&#xff0c;它可以将一种图像的风格&#xff08;如梵高的绘画风格&#xff09;迁移到另一幅图像上…