Saving previous values of a Tensorflow.js tensor

Viewed 145

I have an function which stores all of the values of the tensor and then concatonates it into one matrix. The function looks like this:

    if (i == 1) {
        var y_pred1 = y_pred
    } else if (i == 2) {
        var y_pred2 = y_pred
    } else if (i == 3) {
        var y_pred3 = y_pred
    } else if (i == 4) {
        var y_pred4 = y_pred
    }
tf.concat([y_pred1, y_pred2, y_pred3, y_pred4])

This function stores every value of y_pred across every iteration of for a loop. Is there a way to make this more efficient so that I do not need to have an obscure amount of if statements inside of the model. For your information y_pred is a tensor.

FYI: i stands for the iteration number so if iteration 1 is reached y_pred1 = y_pred

1 Answers

If every tensors return a result, you probably should use an instance of Array.

If the number of tensor if static and you know it in advance:

var tf = new Array(number_of_tensors);

for (var i = 0; i < number_of_tensors; i++) {
    var y_pred = /** get the result of your tensor **/
    tf[i] = y_pred;
}

And that's all. No need to concat(), tf is already a matrix.

If you don't know the number of tensors:

// before your for-loop
var tf = new Array();

// into your for-loop
var y_pred = /** get the result of your tensor **/
tf.push(y_pred);
Related