我选择了这个数据集:https://www.kaggle.com/karangadiya/fifa19
现在,我想将此CSV文件转换为联邦数据集以适应模型。
Tensorflow提供了关于联邦学习的教程,其中他们使用了预定义的数据集。然而,我的问题是如何将这个特定的数据集用于联合学习场景?
发布于 2019-11-22 14:38:37
我将使用一个不同的CSV数据集,但这仍然应该解决这个问题的核心,即如何从CSV创建联合数据集。我们还假设数据集中有一列,您希望用它来表示数据的client_id。
import pandas as pd
import tensorflow as tf
import tensorflow_federated as tff
csv_url = "https://docs.google.com/spreadsheets/d/1eJo2yOTVLPjcIbwe8qSQlFNpyMhYj-xVnNVUTAhwfNU/gviz/tq?tqx=out:csv"
df = pd.read_csv(csv_url, na_values=("?",))
client_id_colname = 'native.country' # the column that represents client ID
SHUFFLE_BUFFER = 1000
NUM_EPOCHS = 1
# split client id into train and test clients
client_ids = df[client_id_colname].unique()
train_client_ids = client_ids.sample(frac=0.5).tolist()
test_client_ids = [x for x in client_ids if x not in train_client_ids]有几种方法可以做到这一点,但我将在这里说明的方法是使用tff.simulation.ClientData.from_clients_and_fn,它要求我们编写一个函数,该函数接受client_id作为输入并返回tf.data.Dataset。我们可以很容易地从数据帧中构造它。
def create_tf_dataset_for_client_fn(client_id):
# a function which takes a client_id and returns a
# tf.data.Dataset for that client
client_data = df[df[client_id_colname] == client_id]
dataset = tf.data.Dataset.from_tensor_slices(client_data.to_dict('list'))
dataset = dataset.shuffle(SHUFFLE_BUFFER).batch(1).repeat(NUM_EPOCHS)
return dataset现在,我们可以使用上面的函数为训练和测试数据创建一个ConcreteClientData对象:
train_data = tff.simulation.ClientData.from_clients_and_fn(
client_ids=train_client_ids,
create_tf_dataset_for_client_fn=create_tf_dataset_for_client_fn
)
test_data = tff.simulation.ClientData.from_clients_and_fn(
client_ids=test_client_ids,
create_tf_dataset_for_client_fn=create_tf_dataset_for_client_fn
)要查看数据集的一个实例,请尝试:
example_dataset = train_data.create_tf_dataset_for_client(
train_data.client_ids[0]
)
print(type(example_dataset))
example_element = iter(example_dataset).next()
print(example_element)
# <class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'>
# {'age': <tf.Tensor: shape=(1,), dtype=int32, numpy=array([37], dtype=int32)>, 'workclass': <tf.Tensor: shape=(1,), dtype=string, numpy=array([b'Local-gov'], dtype=object)>, ...example_dataset的每个元素都是一个Python字典,其中键是表示特性名称的字符串,值是具有一批特性的张量。现在,您有了一个联邦数据集,可以对其进行预处理并用于建模。
发布于 2019-12-23 05:04:48
通过首先从CSV文件创建h5文件,可以将CSV文件转换为联合数据。
背景 h5文件是一种显示元数据的分层文件结构,这种分层结构很好地代表了联合用户的id
当您使用客户端数据对象创建联合数据时,客户端数据是使用h5文件实现的,
联合源代码: Client Data https://github.com/tensorflow/federated/blob/master/tensorflow_federated/python/simulation/hdf5_client_data.py
步骤
上的图像识别教程进行操作
创建h5文件
with h5py.File("student31.h5", 'a') as hdf:
example = hdf.create_group("examples")
for i in range(0,20):
# for data in myDataFrame:
# localList.append(str(data))
# print(type(myDataFrame))
# data.append(myDataFrame)
exampleGroup = example.create_group(str(i))
# myClientGroup = hdf.create_group(str(i))
# d1 = np.random.random(size = (100,33))
print("printing the type ")
print(type(train[i][0]))
exampleGroup.create_dataset('x',data=train[i])
exampleGroup.create_dataset('y',data=dataY[i])联合客户端数据实例化
myclient = HDF5ClientData("student31.h5")https://stackoverflow.com/questions/58965488
复制相似问题