For some custom code, I need to run a for-loop to dynamically create a variable in Tensorflow 2 (with eager execution mode enabled). (In my custom code, the values I write to the variable will require gradients, so I want to track the computations in the for loop, so I can get gradients from autodiff). My code works, but it is incredibly slow. In fact, it is several orders of magnitude slower than performing the same operation in numpy.
I have isolated the problem and I am providing a toy code snippet which highlights the problem. Fixing it will allow me to fix my custom code.
import numpy as np
import tensorflow as tf
import timeit
N = int(1e5)
data = np.random.randn(N)
def numpy_func(data):
new_data = np.zeros_like(data)
for i in range(len(data)):
new_data[i] = data[i]
return new_data
def tf_func(data):
new_data = tf.Variable(tf.zeros_like(data))
for i in range(len(data)):
new_data[i].assign(data[i])
return new_data
%timeit numpy_func(data)
%timeit tf_func(data)
The key takeaways of this code snippet are that in the for-loop I need to only update a slice of the variable. The slice to be updated is different at each iteration. The data used for the update is different at each iteration (In my custom code it is the result of a few simple computations that depend on slices of the variable, here I am just using the fixed array for problem isolation.)
I am using Tensorflow 2 and the tensorflow code ideally needs to run with eager execution enabled, since parts of the custom operations depend on eager execution.
I am new to Tensorflow and I would really appreciate any help with fixing this problem.
Many thanks, Max