我正在使用TensorFlow联邦框架,并为二进制分类问题设计了一个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‘是状态模型权重中的值。
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=[])tff.learning.build_federated_averaging_process
。
在这里有什么指导/建议,因为我哪里出错了?
发布于 2022-04-21 12:53:05
我们可能需要后退一步,考虑一下系统模型是如何联合计算的,以便在某个时候理解“服务器模型”的含义。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)https://stackoverflow.com/questions/71748346
复制相似问题