0

我使用 tensorflow-federated learning api。最近,我将 tensorflow-federated 更新为 0.8.0 版本。然后我运行 federated_learning_for_image_classification.ipynb 文件。state = iterative_process.initialize()但它在“ ”代码行上不起作用。

发生了一些错误,我下一步不能做。

为什么会这样?以前版本的联合 api 0.6.0 运行良好。

您可以参考下面的代码和错误图片。

import nest_asyncio
nest_asyncio.apply()

from __future__ import absolute_import, division, print_function

import collections

import warnings
from six.moves import range
import numpy as np
import six
import tensorflow as tf
import tensorflow_federated as tff

warnings.simplefilter('ignore');

tf.compat.v1.enable_v2_behavior();

np.random.seed(0);

if six.PY3:
tff.framework.set_default_executor(tff.framework.create_local_executor());

tff.federated_computation(lambda: 'Hello, World!')();

emnist_train, emnist_test = tff.simulation.datasets.emnist.load_data();

len(emnist_train.client_ids);

print(emnist_train.client_ids, emnist_test.client_ids);

emnist_train.output_types, emnist_train.output_shapes;

example_dataset = emnist_train.create_tf_dataset_for_client(
emnist_train.client_ids[0]);

example_element = iter(example_dataset).next();

example_element['label'].numpy();

from matplotlib import pyplot as plt;
plt.imshow(example_element['pixels'].numpy(), cmap='gray', aspect='equal');
plt.grid('off')
_ = plt.show()

NUM_CLIENTS = 5
NUM_EPOCHS = 10
BATCH_SIZE = 20
SHUFFLE_BUFFER = 500

def preprocess(dataset):

  def element_fn(element):
    return collections.OrderedDict([
        ('x', tf.reshape(element['pixels'], [-1])),
        ('y', tf.reshape(element['label'], [1])),
    ])

  return dataset.repeat(NUM_EPOCHS).map(element_fn).shuffle(
  SHUFFLE_BUFFER).batch(BATCH_SIZE)

preprocessed_example_dataset = preprocess(example_dataset)

sample_batch = tf.nest.map_structure(
    lambda x: x.numpy(), iter(preprocessed_example_dataset).next())

sample_batch

def make_federated_data(client_data, client_ids):
  return [preprocess(client_data.create_tf_dataset_for_client(x))
      for x in client_ids]

sample_clients = emnist_train.client_ids[0:NUM_CLIENTS]

print(sample_clients)

federated_train_data = make_federated_data(emnist_train, sample_clients)

len(federated_train_data), federated_train_data[0]

def create_compiled_keras_model():
  model = tf.keras.models.Sequential([
      tf.keras.layers.Dense(
          10, activation=tf.nn.softmax, kernel_initializer='zeros', 
input_shape=(784,))])

  model.compile(
      loss=tf.keras.losses.SparseCategoricalCrossentropy(),
      optimizer=tf.keras.optimizers.SGD(learning_rate=0.02),
      metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
  return model

def model_fn():
  keras_model = create_compiled_keras_model()
  return tff.learning.from_compiled_keras_model(keras_model, sample_batch)

iterative_process = tff.learning.build_federated_averaging_process(model_fn)

str(iterative_process.initialize.type_signature)

state = iterative_process.initialize()

错误图片-1

错误图片-2

4

2 回答 2

1

您必须在笔记本中添加这些行:

tff.framework.set_default_executor(tff.framework.create_local_executor(n)) 
tff.framework.set_default_executor(tff.framework.create_local_executor())

其中 n 是本地执行程序中的客户端数量。

于 2020-05-04T06:18:27.093 回答
0

请在此处查看 Github 问题;本质上,master 与 pip 包不同步。如果您在本地执行程序中明确设置客户端的数量,这应该会自行解决。

感谢您对 TFF 的关注!

于 2019-09-24T20:37:23.937 回答