0

I would like to do something like:

x = tf.Variable(tf.ones([100], dtype=tf.float32))
x0 = tf.Variable(tf.ones([1], dtype=tf.float32))
def f(x):
    return tf.sin(x)
x[0] = x0
for i in range(1,100):
    x[i+1] = f(x[i])

to construct a tensor 'x'. Is such a construct possible? I looked at 'tf.while_loop' but does not seem to help.

2 Answers 2

0
def f(x):
    return tf.sin(x)
x = []
x.append(tf.constant(1.0))
for i in range(1,100):
    x.append(f(i))

Is this what you want? This is not recursive by the way. It's iterative.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but no, I would like to get out a tensor 'x'. I edited my question to clarify.
See this, maybe it can help stackoverflow.com/questions/37697747/…
The method in the link gives me what I want, thank you!
0

The solution in TypeError: 'Tensor' object does not support item assignment in TensorFlow pretty much solves the problem as Atirag pointed out, but 'tf.pack()' seems to have been deprecated. Just for completeness, here is an up-to-date solution to the posted problem:

x0 = tf.Variable(tf.ones(1, dtype=tf.float32))
def f(x):
    return tf.sin(x)
x = [x0]
for i in range(1, 100):
    x.append(f(x[i - 1]))

tf.stack(x)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.