2
eval_model = None
for round_num in range(1, 51):
    state, tff_metrics = iterative_process.next(state, federated_train_data)
    eval_model = create_keras_model()
    eval_model.compile(optimizer=optimizers.SGD(learning_rate=0.3),loss=losses.SparseCategoricalCrossentropy(),metrics=[metrics.SparseCategoricalAccuracy()])
    tff.learning.assign_weights_to_keras_model(eval_model, state.model)
    ev_result = eval_model.evaluate(x_test, y_test, verbose=0)
    print('round {:2d}, metrics={}'.format(round_num, tff_metrics))
    print(f"Eval loss : {ev_result[0]} and Eval accuracy : {ev_result[1]}")
    tff_train_acc.append(float(tff_metrics.sparse_categorical_accuracy))
    tff_val_acc.append(ev_result[1])
    tff_train_loss.append(float(tff_metrics.loss))
    tff_val_loss.append(ev_result[0])

It throws an error that we cannot assign weights. Earlier it was working.

1
  • Please format your code and include the full traceback error. Commented Apr 27, 2022 at 2:39

2 Answers 2

1

tff.learning.assign_weights_to_keras_model was removed in version 0.17.0 and was replaced by tff.learning.ModelWeights.assign_weights_to

Try replacing

tff.learning.assign_weights_to_keras_model(eval_model, state.model)

with

state.model.assign_weights_to(eval_model)
Sign up to request clarification or add additional context in comments.

4 Comments

I tried but did not work. AttributeError: The tuple of length 2 does not have named field "assign_weights_to". Fields (up to first 10): ['trainable', 'non_trainable'] keras_model = create_keras_model() state = tff.learning.state_with_new_model_weights( state, trainable_weights=[v.numpy() for v in keras_model.trainable_weights], non_trainable_weights=[ v.numpy() for v in keras_model.non_trainable_weights ])
This then depends on how iterative_process was created and possibly what version of TFF is being used. Could the question be extended to include this information?
Hi @ZacharyGarrett, I have the same issue. state.model.assign_weights_to(eval_model) worked in tff 0.27 but does not work in 0.28. Has it been changed?
@ZacharyGarrett I faced same problem in 0.57.0 'state.model.assign_weights_to(eval_model)' didn't work.
0

You can follow this TFF documentation

model_weights = iterative_process.get_model_weights(state)
model_weights.assign_weights_to(eval_model)

However, need to change tff_metrics.sparse_categorical_accuracy to tff_metrics['client_work']['train']['sparse_categorical_accuracy'] and tff_metrics.loss to tff_metrics['client_work']['train']['loss'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.