【发布时间】:2022-03-10 21:35:39
【问题描述】:
我是联邦学习的新手 我目前正在按照官方 TFF 文档试验一个模型。但我遇到了一个问题,希望我能在这里找到一些解释。
我正在使用自己的数据集,数据分布在多个文件中,每个文件都是一个客户端(因为我正计划构建模型)。并且已经定义了因变量和自变量。
现在,我的问题是如何在联邦学习中将数据拆分为每个客户端(文件)中的训练和测试集?就像我们通常在集中式 ML 模型中所做的那样 以下代码是我迄今为止实现的: 注意 我的代码受到官方文档和 post 的启发,这与我的应用程序几乎相似,但它旨在将客户端拆分为训练和测试客户端本身,而我的目标是拆分数据在这些客户端中。
dataset_paths = {
'client_0': '/content/drive/MyDrive/Colab Notebooks/1.csv',
'client_1': '/content/drive/MyDrive/Colab Notebooks/2.csv',
'client_2': '/content/drive/MyDrive/Colab Notebooks/3.csv'
}
record_defaults = [int(), int(), int(), int(), float(),float(),float(),
float(),float(),float(), int(), int(),float(),float(),int()]
@tf.function
def create_tf_dataset_for_client_fn(dataset_path):
return tf.data.experimental.CsvDataset(dataset_path,
record_defaults=record_defaults,
header=True)
@tf.function
def add_parsing(dataset):
def parse_dataset(*x):
## x defines the dependant varable & y defines the independant
return OrderedDict([('x', x[-1]), ('y', x[1:-1])])
return dataset.map(parse_dataset, num_parallel_calls=tf.data.AUTOTUNE)
source = tff.simulation.datasets.FilePerUserClientData(
dataset_paths, create_tf_dataset_for_client_fn)
source = source.preprocess(add_parsing)
## Creat the the datasets from client data
dataset_creation=source.create_tf_dataset_for_client(source.client_ids[0-2])
print(dataset_creation)
>>> _VariantDataset element_spec=OrderedDict([('x', TensorSpec(shape=(), dtype=tf.int32, name=None)), ('y', (TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(), dtype=tf.float32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None)))])>
## Convert the x into array(I think it is necessary for spliting to training and testing sets )
test= tf.nest.map_structure(lambda x: x.numpy(),next(iter(dataset_creation)))
print(test)
>>> OrderedDict([('x', 1), ('y', (0, 1, 9, 85.0, 7.75, 85.0, 95.0, 75.0, 50.0, 6))])
我对监督机器学习的理解是将数据拆分为训练集和测试集,如下面的代码所示,我不确定在联邦学习中如何做到这一点以及它是否会以这种方式工作?
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state = 42)
所以,我正在寻找这个问题的解释,以便我可以进入培训阶段。
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
-
您是否有示例说明要拆分哪些数据?
-
嗨,@AloneTogether 我已经更新了问题,希望现在清楚了!
标签: python tensorflow tensorflow-datasets tensorflow-federated