【问题标题】:TensorFlow: What is wrong with my fibonacci sequence?TensorFlow:我的斐波那契数列有什么问题?
【发布时间】: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]。我真的不明白我做错了什么。我只是在创建下一个术语,然后使ab 相同,bnewterm 相同。

【问题讨论】:

  • 我拒绝了与从标题中删除“TensorFlow”相对应的编辑,因为我可以直接用 python 编写斐波那契序列。但是我仍然不明白 TF 是如何正常工作的,因此我的问题在这里。
  • tensorflow 不是机器学习工具吗?我不知道这就是它“学习”fib序列的方式。我也不太了解 tensorflow 是如何工作的,但也许如果你用序列的前 10 个数字训练它,它就能弄清楚接下来的数字是什么?
  • @alex 是的,我觉得tensorflow非常适合机器学习,特别是深度学习(这也是我努力学习它的原因!以后想做一个master)。然而,在深入研究核心深度学习之前,我想了解语法和机制是如何工作的,所以这是我的尝试。当然,尽管您可能可以使用机器学习方法来学习序列

标签: python tensorflow fibonacci


【解决方案1】:

Tensorflow 使用静态图描述计算。您首先必须定义图形,然后执行它。

图表执行从您放入sess.run([var1,var2, ..., vaN])调用的节点开始:变量的顺序是无意义。 Tensorflow图评估从一个随机节点开始,从叶子到根跟随每个节点。

由于您要强制执行特定顺序,因此您必须使用tf.control_dependencies 将排序约束引入图中,从而以特定顺序执行操作。

看看我是如何修改你的代码让它工作的,应该很清楚了。

import tensorflow as tf

# define the first 2 terms of the sequence
a = tf.Variable(0)
b = tf.Variable(1)

# you have to force the order of assigments:
# first execute newterm, then execute update1 and than update2

# define the next term. By definition it is the sum of the previous ones
newterm = tf.add(a,b)     # or newterm = a+b

with tf.control_dependencies([newterm]):
    update1 = tf.assign(a, b)

    # thus, execute update2 after update1 and newterm
    # have been executed

    with tf.control_dependencies([update1]):
        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):
         next, up1, up2 = sess.run([newterm, update1, update2])
         fibonacci.append(next)
    print(fibonacci)

【讨论】:

  • 哇,我不知道!谢谢!另外,我也是意大利人。博洛尼亚在深度学习方面的研究环境如何?
  • 另外,我还有一个问题。在 IBM 认知课程中,此练习的解决方案是:a=tf.Variable(0) b=tf.Variable(1) temp=tf.Variable(0) c=a+b update1=tf.assign(temp,c) update2=tf.assign(a,b) update3=tf.assign(b,temp) init_op = tf.initialize_all_variables() with tf.Session() as s: s.run(init_op) for _ in range(15): print(s.run(a)) s.run(update1) s.run(update2) s.run(update3) 所以基本上他们引入了一个新变量,它是总和的一种占位符。你有解释为什么这种方法有效吗?
  • 不客气。由于我的回答解决了您的问题,请记得将其标记为已接受! (另外,如果您想了解有关 @ Bologna 研究环境的一些信息,请访问我的网站 [您在我的个人资料中找到它] 并以某种方式在堆栈溢出之外与我联系])。
  • 他们还使用 3 个不同的 sess.run 来强制将评估顺序从 python 转换为 tensorflow(但这不是一个好习惯,最好在图中正确定义顺序)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 2011-09-30
  • 2012-03-02
  • 1970-01-01
  • 2012-12-29
  • 2021-09-15
  • 2010-11-24
相关资源
最近更新 更多