Is this a mistake in the tensor flow?

I am trying to generate a Fibonacci number: F (n + 2) = F (n + 1) + F (n) using tensor flow. Every time I run my code, it produces different results, very strange. The code is simple and pasted below.

import tensorflow as tf

a = tf.Variable(1)
b = tf.Variable(1)
c = tf.Variable(2)
sum=tf.add(a,b) 

as0 = tf.assign(a,b)
as1=tf.assign(b, c) 
as2=tf.assign(c, sum) 

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(10):
    print(sess.run([as2, as1,as0]))
+4
source share
3 answers

I think @gdelab will not answer fully . I mean, it's true that solves the problem, but I think that is not the real reason. Here is my hunch.

I strongly think that you are trying to run this code in a Jupyter Notebook. If this is not the case, then probably I'm wrong. Well, suppose it's true:

  • When you run the code for the first time, it gives the correct output. First time

  • The second time you run the code, it gives a different output. enter image description here

  • , . , ​​ , , . " ", , ( ) enter image description here

, , , Jupyter Notebook:)

+3

!

, , , sess.run(), , , , , (, ) . , - :

import tensorflow as tf
a = tf.Variable(1)
b = tf.Variable(1)
c = tf.Variable(2)
sum=tf.add(a,b) 
as0 = tf.assign(a,b)
as1=tf.assign(b, c) 
as2=tf.assign(c, sum) 
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(10):
    sess.run(as0)
    sess.run(as1)
    print(sess.run(as2))

, . probavbl , .

0

- , , , . , , .

, ( tf.control_dependencies) node node ( a node node).

:

a = tf.Variable(1)
b = tf.Variable(1)
c = tf.Variable(2)
sum=tf.add(a,b)

3 . : node. .

as0 = tf.assign(a,b)
as1=tf.assign(b, c) 
as2=tf.assign(c, sum)

3 . . , , - .

, , sum c .

, , sum node assign(c,sum) node.

PS: this is not the recommended way to calculate operations such as a Fibonacci sequence. Instead, you should use tf.scanone that has any parameter accumulatorthat helps you a lot.

0
source

Source: https://habr.com/ru/post/1679809/


All Articles