How to optimize my Tensorflow Code (iteration on tensor)?

Viewed 55

I wrote some code that iterates over the shape of my tensor and adds a value to tensor in each step. The problem is it is slow for a large number of iterations (and I need a lot of them):

for i in range(0, num_samples):
    for j in range(0, int(numTimeSteps.numpy())):
        random_num = tf.squeeze(tf.random.normal([1], mean, std_dev, dtype=dtype))
        tensor[i,j].assign(tf.math.add(random_num, estr[i,j]).numpy())

In each iteration I add a random number (drawn from a normal distribution) to the tensor

  • num_samples is specified by the user and can be in the range of 1000-100.000
  • numTimeSpes is 80

Is there a way to optimize this?

1 Answers

TF should rarely rely on loops or numpy. If you see either, try to vectorize it and operate with native tf ops.

Perhaps something like

estr = estr + tf.random.normal(shape=estr.shape)

Note that tf.random is soon to be deprecated for not being random enough - see the note in the docs. The preferred approach is tf.random.generator.

Related