当前位置:   article > 正文

SPDZ基础使用手册(深度学习视角)

SPDZ基础使用手册(深度学习视角)

基本类型

深度学习中最常使用的便是秘密定点数sfix,有关定点数的高级运算协议请参阅Paper: Secure Computation With Fixed-Point Numbers.

容器类型

SPDZ的深度学习框架主要基于TensorFlow实现,其中使用的容器是张量Tensor,在库中的定义如下:

  1. def Tensor(cls, shape):
  2. """
  3. Type-dependent tensor of any dimension::
  4. a = sfix.Tensor([10, 10])
  5. """
  6. if len(shape) == 1:
  7. return Array(shape[0], cls)
  8. else:
  9. return MultiArray(shape, cls)

可以看出使用Tensor在大多数情况就是在使用MultiArray,类比Python基础类型的多维列表。

这里举一个例子:

  1. a = sint.Tensor([3, 10, 10]) #创建张量,类型为sint,内容是3个10×10的矩阵
  2. a[0].input_from(0) #第一个矩阵从第0方读取数据
  3. a[1].input_from(1) #第二个矩阵从第1方读取数据
  4. a[2][:] = a[0][:] * a[1][:] #第三个矩阵由前两个矩阵元素对应相乘实现;[:] 表示选择这个矩阵的所有元素。

MultiArray的其它用法

assign(other, base=0)

        Assign container to content. Not implemented for floating-point.

                Parameters other – container of matching size and type

assign_all(value)

        Assign the same value to all entries.

                Parameters value – convertible to relevant basic type

assign_part_vector(vector, base=0)

        Assign vector from range of the first dimension, including all entries in further dimensions.                 Parameters

                        • vector – updated entries

                        • base – index in first dimension (regint/cint/int)

assign_vector(vector, base=0)

               Assign vector to content. Not implemented for floating-point.

                        Parameters

                                • vector – vector of matching size convertible to relevant basic type

                                • base – compile-time (int)

direct_mul(other, reduce=True, indices=None, res_type=None)

        Matrix multiplication in the virtual machine. Unlike dot(), this only works for sint and sfix, and it returns a vector instead of a data structure.

                Parameters

                        • self Matrix / 2-dimensional MultiArray

                        • other Matrix / 2-dimensional MultiArray

                        • indices – 4-tuple of regint vectors for index selection (default is complete multiplication)

                Returns Matrix as vector of relevant type (row-major)

PS: 不常用,因为泛用性很差,只能针对二维数组;也是库里自带函数的通病,以至于很多tf的很多高级函数在库中都是不存在的。

dot(other, res_params=None, n_threads=None)

        Matrix-matrix and matrix-vector multiplication.

                Parameters

                • self – two-dimensional

                • other – Matrix or Array of matching size and type

                • n_threads – number of threads (default: all in same thread)

        Return type Matrix or Array of appropriate size and type

input_from(player, budget=None, raw=False, **kwargs)

        Fill with inputs from player if supported by type.

                Parameters player – public (regint/cint/int)

randomize(*args, n_threads=None)

        Randomize according to data type. If it is sfix, the following will sample an individual         uniformly random entry of the multi-array M roughly in the range [

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/315808
推荐阅读
相关标签