有没有办法投射联合价值?

Is there a way to cast a federated value?

如果我有一个联合值,比如 {int32}@CLIENTS 我想转换为 {float32}@CLIENTS 有没有简单的方法可以做到这一点?谢谢!

张量操作通常需要在用 tff.tf_computation. Since the types mentioned have placements (@CLIENTS) this likely is inside a tff.federated_computation decorated function, so the casting method would need to be called with tff.federated_map.

装饰的函数内部进行

像这样:

@tff.tf_computation
def cast_to_float(x):
  return tf.cast(x, tf.float32)

@tff.federated_computation(tff.FederatedType(int32, tff.CLIENTS))
def my_func(a):
  a_float = tff.federated_map(cast_to_float, a)
  return a_float

print(my_func.type_signature)

>>> ({int32}@CLIENTS -> {float32}@CLIENTS)

可以使用 tf.cast() 完成,但是使用 @tff.federated_computation()。