Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

tensorflow federated - How to make prediction with TFF?

My question is : How can I predict a label of such image with Tensorflow Federated ?

After completing the evaluation of the model, I would like to predict the label of a given image. Like in Keras we do this :

# new instance where we do not know the answer
Xnew = array([[0.89337759, 0.65864154]])
# make a prediction
ynew = model.predict_classes(Xnew)
# show the inputs and predicted outputs
print("X=%s, Predicted=%s" % (Xnew[0], ynew[0]))

Output:

X=[0.89337759 0.65864154], Predicted=[0]

here is how state and model_fn was created:


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, server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0),client_weight_fn=None)
state = iterative_process.initialize()

I find this error :

list(self._name_to_index.keys())[:10]))
AttributeError: The tuple of length 2 does not have named field "assign_weights_to". Fields (up to first 10): ['trainable', 'non_trainable']

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Requires TFF 0.16.0 or newer)

Since the code is building a tff.learning.Model from a tf.keras.Model you may be able to use the assign_weights_to method on the tff.learning.ModelWeights object (the type of state.model). This method is used in the Federated Learning for Text Generation tutorial.

This might look something like (near the bottom, the early portions are an example FL training loop):


def create_keras_model() -> tf.keras.Model:
  ...

def model_fn():
  ...
  return tff.learning.from_keras_model(create_keras_model())

training_process = tff.learning. build_federated_averaging_process(model_fn, ...)

state = training_process.initialize()
for _ in range(NUM_ROUNDS):
  state, metrics = training_process.next(state, ...)

model_for_inference = create_keras_model()
state.model.assign_weights_to(model_for_inference)

Once the weights from state have been assigned back into the Keras model, the code can use the standard Keras APIs, such as tf.keras.Model.predict_on_batch

predictions = model_for_inference.predict_on_batch(batch)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...