【发布时间】:2017-11-10 08:37:53
【问题描述】:
我正在尝试学习 TensorFlow,所以我想写一个斐波那契数列(其中的一部分,ofc)。
本练习的灵感来自 IBM 认知课程。
这是我的代码:
#import stuff
import tensorflow as tf
# define the first 2 terms of the sequence
a = tf.Variable(0)
b = tf.Variable(1)
# define the next term. By definition it is the sum of the previous ones
newterm = tf.add(a,b) # or newterm = a+b
# define the update operations. I want a to become b, and b to become the new term
update1 = tf.assign(a, b)
update2 = tf.assign(b, newterm)
# initialize variables
init = tf.global_variables_initializer()
# run
with tf.Session() as sess:
sess.run(init)
fibonacci = [a.eval(), b.eval()]
for i in range(10):
new, up1, up2 = sess.run([newterm, update1, update2])
fibonacci.append(new)
print(fibonacci)
但是这会打印出[0, 1, 2, 4, 8, 12, 24, 48, 96, 144, 240, 480]。我真的不明白我做错了什么。我只是在创建下一个术语,然后使a 与b 相同,b 与newterm 相同。
【问题讨论】:
-
我拒绝了与从标题中删除“TensorFlow”相对应的编辑,因为我可以直接用 python 编写斐波那契序列。但是我仍然不明白 TF 是如何正常工作的,因此我的问题在这里。
-
tensorflow 不是机器学习工具吗?我不知道这就是它“学习”fib序列的方式。我也不太了解 tensorflow 是如何工作的,但也许如果你用序列的前 10 个数字训练它,它就能弄清楚接下来的数字是什么?
-
@alex 是的,我觉得tensorflow非常适合机器学习,特别是深度学习(这也是我努力学习它的原因!以后想做一个master)。然而,在深入研究核心深度学习之前,我想了解语法和机制是如何工作的,所以这是我的尝试。当然,尽管您可能可以使用机器学习方法来学习序列
标签: python tensorflow fibonacci