如何合并Tensorflow 2中策略的结果

如何合并Tensorflow 2中策略的结果,tensorflow,gpu,distribution,tpu,Tensorflow,Gpu,Distribution,Tpu,我试图在Tensorflow中使用策略。我知道如何从每个副本获得结果的总和和平均值。但是,如果每个副本的结果是数组,如何将这些数组从每个副本合并到一个数组 以下是获取丢失的代码: # val def distributed_val(ds): total_loss = tf.cast(0.0, tf.float32) num_train_batches = tf.cast(0.0, tf.float32) for one_batch

我试图在Tensorflow中使用策略。我知道如何从每个副本获得结果的总和和平均值。但是,如果每个副本的结果是数组,如何将这些数组从每个副本合并到一个数组

以下是获取丢失的代码:

    # val
    def distributed_val(ds):
        total_loss = tf.cast(0.0, tf.float32)
        num_train_batches = tf.cast(0.0, tf.float32)

        for one_batch in ds:

            per_replica_loss = strategy.experimental_run_v2(
                self.loss, args=(one_batch,))

            total_loss += strategy.reduce(
                tf.distribute.ReduceOp.SUM, per_replica_loss, axis=None)

            num_train_batches += 1

        return total_loss, num_train_batches
我使用函数strategy.reduce()得到损失的总和

如果每个副本丢失为阵列,如何将这些阵列合并到一个阵列中


非常感谢。

使用策略。展开(张量)。据我所知,应该有一个
Strategy.concat()
函数,但它似乎还没有得到有效的实现。是的。它尚未实施。