I am using the Python API for Tensorflow. I am trying to implement the Rosenbrock function given below without the use of a Python loop:
My current implementation is as follows:
def rosenbrock(data_tensor):
columns = tf.unstack(data_tensor)
summation = 0
for i in range(1, len(columns) - 1):
first_term = tf.square(tf.subtract(columns[i + 1], tf.square(columns[i])))
second_term = tf.square(tf.subtract(columns[i], 1.0))
summation += tf.add(tf.multiply(100.0, first_term), second_term)
return summation
I have tried implementing the summation in a tf.while_loop()
; however, I found the API somewhat unintuitive when it comes to using an index integer that is meant to remain separate from the data. The example given in the documentation uses the data as the index (or vice-versa):
i = tf.constant(0)
c = lambda i: tf.less(i, 10)
b = lambda i: tf.add(i, 1)
r = tf.while_loop(c, b, [i])