# FederatedLearning **Repository Path**: elcucuycr/FederatedLearning ## Basic Information - **Project Name**: FederatedLearning - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-19 - **Last Updated**: 2021-10-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 典型工作流程 ![](.\Pic\WorkFlowOfBlockchainedFL.jpg) 图中展示出了迭代过程之前的准备工作和迭代过程的第1代,后续还将进行多次迭代,直至达到指定的训练轮次或者模型达到精度要求。 可以看到`ModelContract.go`中只有部分方法被调用,其他的方法旨在提供客户多种多样的查询方法。 ## 本地模型和全局模型 + 无论是本地模型(local model)还是全局模型(global model),都需要被提交到分布式账本(distributed ledger)中。 + 每个模型在账本中的存储形式,都是一个键值对(key-value pair)。 + 本地模型和全局模型的键值对结构是不同的,这将在下文中介绍。但是他们含有共同的有效荷载(payload),即在`metadata.go`内定义的`TrainingParameters`结构体: ```go //TrainingParameters is the metadata of a model type TrainingParameters struct { Weight [][]float64 `json:"layer.weight"` Bias []float64 `json:"layer.bias"` } ``` **这个结构体表征一个模型的所有参数,是由机器学习训练的模型类型来决定的,与区块链平台无关。** ### 本地模型在账本中的键值对表示 #### 键 在`ModelContract.go`内定义的结构体`ModelInformation`将作为本地模型在账本中的键: ```go //ModelInformation will be used as the key in the key-value pair of a local model transaction. //And the value in the key-value pair of a local model transaction is TrainingParameters. type ModelInformation struct { //if true, this is a local model.If false, this is a global model. IsLocalModel bool Iteration int Producer string CreatedTime string } ``` 该结构体提供了模型的基本信息。四个字段分别表征:是否是本地模型、作为第几代的模型、模型创造者、模型创造时间。 > 注:因为账本中的键只能是`string`类型,因而实际上还用到了官方`shim.ChaincodeStubInterface`所提供的`CreateCompositeKey`方法,将该结构体转化为`string`。之所以使用官方提供的`CreateCompositeKey`方法来将结构体转化为`string`,是因为通过该方法生成的键,官方还配套提供了便利的查找方法。 #### 值 本地模型的值为上文中提到的`TrainingParameters`结构体。 > 注:因为账本中的值必须为`[]byte`类型,所以实际上还用到了`json.Marshal`方法将结构体转化为`[]byte`类型。 ### 全局模型在账本中的键值对表示 #### 键 全局模型在账本中拥有唯一的键值对,在迭代更新过程中,该键值对的值会不断被修改。但是键不会改变,始终为字符串`"Iteration"`. #### 值 全局模型的值为在`ModelContract.go`中定义的`GlobalModel`结构体: ```go //GlobalModel will be used as the value in the key-value pair of a global model transaction. //And the key in the key-value of a global model transaction is the string "Iteration". //Thus, if a peer need to query information about latest global model or Iter, it will query key "Iteration" in the ledger. type GlobalModel struct { ModelInformation //TPAsString is the result of type TrainingParameters being marshalled and converted to string type. TPAsString string } ``` 它包含了两个字段,第一个字段是上文提到的`ModelInformation`结构体。第二个字段是一个`string`类型,该`string`类型实际上是上文中提到的`TrainingParameters`结构体经`json.Mashal()`和`string()`两个函数类型转化后的结果。 因此从逻辑上来说,全局模型的值即包含了表征模型基本信息的`ModelInformation`结构体,又包含了模型参数`TrainingParameters`结构体. > 注1:在本地模型中,这两个结构体分别对应着键和值。而在全局模型中,把这两个结构体全部放在了值中,目的是为了让全局模型有一个固定的键"Iteration".这样做的好处是让对全局模型的查询变得简便,迭代过程中,节点始终只需要查询"Iteration"这个键即可查询到最新的全局模型。 > 注2:`GlobalModel`结构体中第二个字段并非`TrainingParameters`结构体而是由该结构体转化而来的`string`类型`TPAsString`,原因是:第二个字段中含有的信息量较大,处理起来耗费时间较长,如果节点只需要查询`GlobalModel`结构体中的`ModelInformation`字段,将第二个字段视作`string`类型即可。只有当需要读取其中的模型参数时,才进一步将第二个字段重新转化回`TraningParameters`.