Ошибка кода federated_learning_for_image_classification.ipynb - PullRequest
0 голосов
/ 22 сентября 2019

Я использую API-интерфейс изучения с участием тензорного потока.И недавно я обновляю tenorflow-federated до версии 0.8.0.А затем я запускаю файл federated_learning_for_image_classification.ipynb.Но это не сработало в строке кода "state = iterative_process.initialize()".

Произошла ошибка, которую я не могу сделать дальше.

Почему это произошло?Предыдущая версия federated 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()

error picture-1

ошибка изображения-2

1 Ответ

0 голосов
/ 24 сентября 2019

Пожалуйста, смотрите выпуск Github здесь ;по сути, мастер не синхронизирован с пакетом pip.Если вы явно указали количество клиентов в локальном исполнителе, это должно разрешиться само собой.

Спасибо за ваш интерес к TFF!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...