具有张量流的张量切片的迭代更新

时间:2016-09-12 15:51:23

标签: tensorflow

我正在尝试迭代地更新一个tensorflow变量切片。一个例子是计算所有斐波那契数,直到N。我希望最终得到NF[0] = 0, F[1]=1的张量F[n] = F[n-1] + F[n-2]

1 个答案:

答案 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))