【问题标题】:Flipping the labels of a TF dataset翻转 TF 数据集的标签
【发布时间】:2021-06-23 16:51:22
【问题描述】:

我想为 CIFAR-100 创建一个恶意数据集,以测试类似于 EMNIST 恶意数据集的联合学习攻击:

url_malicious_dataset = 'https://storage.googleapis.com/tff-experiments-public/targeted_attack/emnist_malicious/emnist_target.mat'
filename = 'emnist_target.mat'
path = tf.keras.utils.get_file(filename, url_malicious_dataset)
emnist_target_data = io.loadmat(path)

我尝试以下方法将提取的示例数据集中的标签 0 翻转为 4,但此方法不起作用:

cifar_train, cifar_test = tff.simulation.datasets.cifar100.load_data(cache_dir=None)
example_dataset = cifar_train.create_tf_dataset_for_client(cifar_train.client_ids[0])
for example in example_dataset:
  if example['label'].numpy() == 0:
    example['label'] = tf.constant(4,dtype=tf.int64)

知道如何通过正确翻转标签为 CIFAR-100 而不是 EMNIST 创建类似版本的恶意数据集吗?

【问题讨论】:

    标签: tensorflow tensorflow-federated


    【解决方案1】:

    一般来说,tf.data.Dataset 对象可以使用它们的.map 方法进行修改。所以举个例子,一个简单的标签翻转可以这样完成:

    def flip_label(example):
      return {'image': example['image'], 'label': 99-example['label']}
    
    flipped_dataset = example_dataset.map(flip_label)
    

    这会颠倒标签 0-99。您可以执行类似的操作来发送 0 到 4 并修复所有其他标签。

    请注意,如果您想将此应用于cifar_train 中的所有客户端数据集,则必须使用tff.simulation.datasets.ClientData.preprocess 方法。也就是说,您可以执行 cifar_train.preprocess(lambda x: x.map(flip_label)) 之类的操作。

    【讨论】:

    • 感谢您的回答。但是,我仍然无法弄清楚如何仅将“flip_label”函数应用于标签为 0 的条目。事实上,当我在“flip_label”函数中执行print(example['label']) 时,输出是Tensor("args_2:0", shape=(), dtype=int64),它不是一个 numpy 张量。如何检查标签是否为0?
    • 对于 TensorFlow 中的条件,您可以使用tf.cond。您可以简单地检查张量是否等于 0 并采取相应措施。
    猜你喜欢
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多