【问题标题】:Keras model prediction after tensorflow federated learningTensorFlow 联邦学习后的 Keras 模型预测
【发布时间】:2022-06-16 21:23:21
【问题描述】:

我正在使用 TensorFlow Federated 框架并为二进制分类问题设计了一个 keras 模型。我用tff.learning.build_federated_averaging_process 定义了迭代过程并用 state, metrics = iterative_process.next(state, train_data)

执行完上述步骤后,我尝试运行预测,

    model_test=create_keras_model() # function defining the binary classification model
    model_test.compile(optimizer='adam',            
    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                metrics=['accuracy'])
    pred_out=model_test.predict(a[0].take(20)) # a[0] is the dataset constructed with the function 
                                             create_tf_dataset_for_client()
    classes =( pred_out >0.5 ).astype("int32")
    np.unique(classes)

    array([[0],
       [1],
       [0],
       [0],
       [1],
       [1],
       [1],
       [0],
       [0],
       [1],
       [1],
       [0],
       [1],
       [1],
       [0],
       [0],
       [0],
       [1],
       [1],
       [0]], dtype=int32)

但是在将状态的 tff 学习模型权重应用于模型后,预测并没有按预期工作。它对所有行显示相同的值。

    model_test=create_keras_model() # function defining the binary classification model
    state.model.assign_weights_to(model_test)
    pred_out=model_test.predict(a[0].take(20)) # a[0] is the dataset constructed with the function 
                                             create_tf_dataset_for_client()
    print(pred_out)

    array([[-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368],
       [-0.2798368]], dtype=float32)

经过不断的研究,我了解到上面的值'-0.2798368'是状态Modelweights中的值

    print(state.model.assign_weights_to(keras_model))
    ModelWeights(trainable=[array([[-4.984627  , -5.193449  , -5.790202  , 
    -5.5200233 , -5.5461893 ,
    -4.977145  , -5.4065394 , -5.619186  , -5.3337646 , -5.136057  ],
    [-0.5657665 , -5.8657775 , -5.3425145 , -5.2261133 , -5.330576  ,
    -5.9684296 , -5.4551187 , -5.3567815 , -4.8706098 , -5.7063856 ],
    [-5.6153154 , -5.9375963 , -5.4587545 , -5.689524  , -5.463484  ,
    -4.9066486 , -5.752383  , -0.3759068 , -5.4120364 , -5.8245053 ],
    [-5.2911777 , -5.42058   , -5.932811  , -5.4922986 , -0.41761395,
    -5.432293  , -5.309703  ,  0.31641293, -5.635701  , -5.7644367 ],
    [ 0.07086992, -5.0122833 , -5.2278    , -5.2102866 , -0.03762579,
    -0.43286362, -4.865974  , -0.3707862 , -5.9437294 , -5.1678157 ],
    [-5.6853213 , -5.467271  , -5.7508802 , -5.4324217 , -5.3518825 ,
    -5.033523  , -4.8834076 , -4.8871975 , -5.9014115 , -5.3266053 ],
    [-5.280035  , -5.763103  , -5.828321  , -5.780304  , -5.908666  ,
    -5.6955295 , -5.6714606 , -4.9686913 , -4.898386  , -5.12075   ],
    [-4.8388877 , -5.7745824 , -5.1134114 , -5.779592  , -5.616187  ,
    -4.870717  , -5.131807  , -5.9274936 , -5.345783  , -5.113287  ]],
    dtype=float32), array([-5.4049463, -5.4049444, -5.404945 , -5.404946 , 
    -5.404945 ,
    -5.4049444, -5.404945 , -5.404945 , -5.4049454, -5.4049444],
    dtype=float32), array([[ 4.972922 ],
    [-4.823935 ],
    [ 4.916144 ],
    [ 5.0096955],
    [-4.9212008],
    [-5.1436653],
    [ 4.8211393],
    [-4.8939514],
    [ 5.1752467],
    [-5.01398  ]], dtype=float32), **array([-0.2798368]**, dtype=float32)], 
    non_trainable=[])
  1. 我们是否需要明确地将状态模型权重应用于服务器模型,或者 tff.learning.build_federated_averaging_process api 会默认更新服务器模型?在 tff 教程中给出了“使用服务器优化器的 tf.keras.optimizers.Optimizer.apply_gradients 方法在服务器上应用聚合模型增量。”

这里有什么指导/建议,我哪里出错了?

【问题讨论】:

  • 不知道create_keras_model 或create_tf_dataset_for_client 将很难回答;这些可以添加到问题中吗?进行了多少轮训练?

标签: python tensorflow keras tensorflow-federated


【解决方案1】:

我们可能需要退一步思考系统如何对联合计算进行建模,以便在某个时间点理解“服务器模型”的含义。 SERVER 和 CLIENTS 概念存在于执行脚本的 python 运行时的不同抽象层中。这意味着在 Python 中构建 Keras 模型的代码“在”具有这些概念的“联合上下文”之外位置。

# TFF doesn't know about this model, it doesn't exist at a "placement",
# i.e. it is neither SERVER nor CLIENTS placed.
model = create_keras_model()

learning_process = tff.learning.build_federated_averaging_process(...)
# During the call to `initialize` a "federated context" exists, which runs
# a `tff.Computation` called `initialize` that creates a value placed at 
# SERVER. However, once the function "returns back to Python", the "state"
# variable we have below no longer has any "placement", its just "in Python".
state = learning_process.initialize()
# When we pass "state" back into the `next` method, it is given placement again
# based on the type signature of `next`. In this case, its placed back at 
# SERVER and the placement is used _during_ the invocation of `next`. Again,
# once `next` returns, the notion of placements goes away; we're back "in 
# Python" without placement.
state, metrics = learning_process.next(state, data)

在上面的代码中model 可以称为“服务器模型”,它最初具有相同的权重,但它不是 TFF API 中提到的SERVER 放置模型文档。该文档仅在调用tff.Computation(例如initialize 和next)期间引用值。

换句话说,model 和 state 没有连接。更新一个不会更新另一个。将model 与新训练的权重一起使用(例如,在next 调用之后)。代码必须将state 权重分配回model(如问题中所做的那样):

state.model.assign_weights_to(model)

【讨论】:

  • 这似乎不起作用,我收到此错误Expected tensorflow_federated.python.learning.model.Model or keras.engine.training.Model, found function.,但我仍然无法看到模型预测。您能提供更多帮助吗?
  • 堆栈跟踪的其余部分是否包含在错误中?错误是来自assign_weights_to() 调用,还是来自其他地方?
  • 是的,错误来自assign_weights_to() 调用。我将其粘贴为答案,因为它太长,无法写在评论框中。
【解决方案2】:

这是Dr.Zachary在调用assign_weights_to()后指出错误信息的后续评论

Starting training
Round   0: OrderedDict([('sparse_categorical_accuracy', 0.12227074), ('loss', 1.3862942), ('num_examples', 916), ('num_batches', 46)])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-248d0d100295> in <module>()
      3 
      4 print(f'Starting training')
----> 5 data_frame = train(NUM_ROUNDS, data_frame)
      6 print()

2 frames
<ipython-input-16-4b5aa7d302de> in train(NUM_ROUNDS, data_frame)
      6     state, _= iterative_process.next(state, train_data)
      7     print(f'Round {round_num:3d}: {train_metrics}')
----> 8     state.model.assign_weights_to(model_fn)
      9     data_frame = data_frame.append({'Round': round_num,
     10                                       **train_metrics}, ignore_index=True)

/usr/local/lib/python3.7/dist-packages/tensorflow_federated/python/learning/model_utils.py in assign_weights_to(self, model)
     64         weights to.
     65     """
---> 66     py_typecheck.check_type(model, (model_lib.Model, tf.keras.Model))
     67     if isinstance(model, tf.keras.Model):
     68       tf.nest.map_structure(lambda var, t: var.assign(t),

/usr/local/lib/python3.7/dist-packages/tensorflow_federated/python/common_libs/py_typecheck.py in check_type(target, type_spec, label)
     40     raise TypeError('Expected {}{}, found {}.'.format(
     41         '{} to be of type '.format(label) if label is not None else '',
---> 42         type_string(type_spec), type_string(type(target))))
     43   return target
     44 

TypeError: Expected tensorflow_federated.python.learning.model.Model or keras.engine.training.Model, found function.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2021-07-08
    • 2020-05-27
    • 2021-06-03
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多