[英]AttributeError: 'MapDataset' object has no attribute 'client_ids' in tensorflow_federated TFF
[英]AttributeError: 'MapDataset' object has no attribute 'preprocess' in tensorflow_federated tff
我正在使用非 IID 分布測試本教程以進行聯合學習: https://www.tensorflow.org/federated/tutorials/tff_for_federated_learning_research_compression
在此發布的問題TensorFlow Federated:How to tune non-IIDness in federated dataset? 它建議使用 tff.simulation.datasets.build_single_label_dataset() 作為為數據集生成非 IID 分布的方法。
我嘗試先應用它(請參閱代碼)並出現錯誤!
emnist_train, emnist_test = tff.simulation.datasets.emnist.load_data(
only_digits=False)
emnist_train1 = tff.simulation.datasets.build_single_label_dataset(
emnist_train.create_tf_dataset_from_all_clients(),
label_key='label', desired_label=1)
print(emnist_train1.element_spec)
OrderedDict([('label', TensorSpec(shape=(), dtype=tf.int32, name=None)), ('pixels', TensorSpec(shape=(28, 28), dtype=tf.float32, name=沒有任何))])
print(next(iter(emnist_train1))['label'])
tf.Tensor(1, shape=(), dtype=int32)
MAX_CLIENT_DATASET_SIZE = 418
CLIENT_EPOCHS_PER_ROUND = 1
CLIENT_BATCH_SIZE = 20
TEST_BATCH_SIZE = 500
def reshape_emnist_element(element):
return (tf.expand_dims(element['pixels'], axis=-1), element['label'])
def preprocess_train_dataset(dataset):
return (dataset
.shuffle(buffer_size=MAX_CLIENT_DATASET_SIZE)
.repeat(CLIENT_EPOCHS_PER_ROUND)
.batch(CLIENT_BATCH_SIZE, drop_remainder=False)
.map(reshape_emnist_element))
emnist_train1 = emnist_train1.preprocess(preprocess_train_dataset)
>> ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-cda96c33a0f6> in <module>()
15 .map(reshape_emnist_element))
16
---> 17 emnist_train1 = emnist_train1.preprocess(preprocess_train_dataset)
AttributeError: 'MapDataset' object has no attribute 'preprocess'
由於數據集被過濾,因此無法進行預處理,那么,在這種情況下? 它是根據什么標簽過濾的?
... label_key='label', desired_label=1)
EMNIST 中的哪個 label 需要 label = 1?
我的問題是:
在這個特定的教程中,我如何應用這個 function tff.simulation.datasets.build_single_label_dataset() 來獲取非 IID 數據集(每個客戶端的樣本數量不同) ! https://www.tensorflow.org/federated/tutorials/tff_for_federated_learning_research_compression關於過濾數據集的詳細信息沒有錯誤!
感謝任何幫助!
非常感謝!
tff.simulation.datasets.ClientData
和tf.data.Dataset
API 之間可能存在一些混淆,這些混淆將有助於涵蓋。
tf.data.Dataset
沒有preprocess
方法,而tff.simulation.datasets.ClientData.preprocess
確實存在。
但是, tff.simulation.datasets.build_single_label_dataset
使用tf.data.Dataset
實例:輸入參數和 output 結果都作為tf.data.Dataset
實例。 在這種情況下, emnist_train1
是一個沒有preprocess
方法的tf.data.Dataset
。
然而,一切並沒有丟失! preprocess_train_dataset
function 采用tf.data.Dataset
參數,並返回tf.data.Dataset
結果。 這應該意味着更換:
emnist_train1 = emnist_train1.preprocess(preprocess_train_dataset)
和
emnist_train1 = preprocess_train_dataset(emnist_train1)
將創建一個只有一個tf.data.Dataset
(“標簽非獨立同分布”)的 tf.data.Dataset,它被打亂、重復、批處理和重塑。 請注意,單個tf.data.Dataset
通常用於表示聯合算法中的一個用戶。 要使用隨機數量的批次創建更多,可以使用類似以下的方法:
client_datasets = [
emnist_train1.take(random.randint(1, MAX_BATCHES))
for _ in range(NUM_CLIENTS)
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.