<p>Yes, but be careful.  You can average two tensors with <a href="https://js.tensorflow.org/api/latest/#mean" rel="nofollow noreferrer"><code>tf.mean</code></a> like <a href="https://stackoverflow.com/users/5069957/edkeveked">https://stackoverflow.com/users/5069957/edkeveked</a> said.  However, remember <code>axis=0</code> should be shortened to just <code>0</code> in JavaScript.</p>
<p>Just to rewrite his code in a second way:</p>
<pre class="lang-js prettyprint-override"><code>const x = tf.tensor([1, 2, 3, 2, 3, 4], [2, 3]);
x.mean(0).print()
</code></pre>
<p><em><strong>However,</strong></em> you asked if you're doing it right, and that depends on if you're averaging as you go or not. There's an issue with a rolling average.</p>
<h2 id="example">Example:</h2>
<p>If you average (10, 20) then 30, you get (22.5) a different number than averaging (20, 30) then 10 (17.5), which is of course different from averaging all three at the same time, which would give you 20.</p>
<p>Averages do not adhere to an order-irrelevance principle once they've been calculated.  It's the division part that removes the associative property.  So you'll need to either:</p>
<p><strong>A:</strong>  Store all model weights and calculate a new average each time based on all previous models</p>
<p>or</p>
<p><strong>B:</strong>  Add a weighting system to your federated average so more recent models do not significantly affect the system.</p>
<h2 id="which-makes-sense">Which makes sense?</h2>
<p>I recommend <strong>B</strong> in the situation that you:</p>
<ol>
<li>Don't want to or cannot store every model and weight ever submitted.</li>
<li>You know some models have seen more valid data, and should be weighted appropriately compared to blind models.</li>
</ol>
<p>You can computer a weighted average adjusting the denominator for your existing model vs your incoming model.</p>
<p>In JavaScript you can do something simple like this to computer a weighted average between two values:</p>
<pre class="lang-js prettyprint-override"><code>const modelVal1 = 0
const modelVal2 = 1

const weight1 = 0.5
const weight2 = 1 - weight1


const average = (modelVal1 * weight1) + (modelVal2 * weight2)
</code></pre>
<p>The above code is your common evenly weighted average, but as you adjust the weight1, you are rebalancing the scales to significantly adjust the outcome in favor of <code>modelVal1</code> or <code>modelVal2</code>.</p>
<p>Obviously, you'll need to convert the JavaScript I have shown into tensor mathematical functions, but that's trivial.</p>
<p>Iterate averaging (or weighted average) with weights decaying is often used in Federated learning.  See <a href="https://arxiv.org/abs/1802.08009" rel="nofollow noreferrer">Iterate averaging as regularization for stochastic gradient descent</a>, and <a href="https://arxiv.org/pdf/2103.11619.pdf" rel="nofollow noreferrer">Server Averaging for Federated Learning</a>.</p>
Federated learning with tensorflowjs
<p>I am implementing federated learning with tensorflowjs. But i am kind of stuck in the federated averaging process. The idea is simple: get updated weights from multiple clients and average it in the server.</p>

<p>I have trained a model on browser, got the updated weights via model.getWeights() method and sent the weights to server for averaging.</p>

<pre><code>
//get weights from multiple clients(happens i client-side)
w1 = model.getWeights(); //weights from client 1
w2 = model.getWeights(); //weights from client 2


//calculate average of the weights(server-side)
var mean_weights= [];
let length = w1.length; // length of all weights_array is same
for(var i=0; i&lt;length; i++){
    let sum = w1[i].add(w2[i]);
    let mean = sum.divide(2); //got confused here, how to calculate mean of tensors ??
    mean_weights.push(mean);
}

//apply updates to the model(both client-side and server-side)
model.setWeights(mean_weights);
</code></pre>

<p><strong>So my question is:
How do I calculate the mean of tensor array ?
Also, is this the right approach to perform federated averaging via tensorflowjs ?</strong></p>