我正在尝试迭代地更新一个tensorflow变量切片。一个例子是计算所有斐波那契数,直到N
。我希望最终得到N
和F[0] = 0, F[1]=1
的张量F[n] = F[n-1] + F[n-2]
。
答案 0 :(得分:1)
以下是在TensorFlow中实现Fibonacci序列的一种方法。以下代码将序列打印为2x1 2D张量变量。 希望这有帮助!!
import tensorflow as tf
with tf.Graph().as_default() as g:
fib_matrix = tf.constant([[0.0, 1.0],
[1.0, 1.0]])
fib_sequence = tf.Variable([[0.0], [1.0]])
# Multiply fib_matrix and fib_sequence.
next_fib = tf.matmul(fib_matrix, fib_sequence)
# Assign result back to fig_sequence.
assign_op = tf.assign(fib_sequence, next_fib)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for step in range(10):
sess.run(assign_op)
print(sess.run(fib_sequence))