tensorflow联合学习后的Keras模型预测



我正在使用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"是状态下模型权重的值

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教程中给出;聚合模型delta是通过使用服务器优化器的tf.keras.optimizers.Optimizer _apply_filteries方法在服务器上应用的">

这里有什么指导/建议吗?

我们可能需要退一步思考系统如何对联合计算建模,以理解";服务器模型";在某个时间点。SERVERCLIENTS概念存在于脚本执行的python运行时的不同抽象层中;外部";";联合上下文";有这些位置的概念。

# 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)

在上述代码中,CCD_;"服务器模型";,它最初将具有相同的权重,但它是而不是TFF API文档中引用的SERVER放置模型。文档仅在调用tff.Computation(例如initializenext(期间引用值。

换句话说,modelstate不连接。更新其中一个不会更新另一个。使用具有新训练的权重的model(例如,在next呼叫之后(。代码必须将state权重分配回model(如quesiton中所做(:

state.model.assign_weights_to(model)

相关内容

  • 没有找到相关文章

最新更新