我编写了两个函数,以不同的方式初始化tensorflow的变量。我不知道为什么结果不同。这是使用占位符进行初始化的第一个函数:
第一个函数
import tensorflow as tf
import numpy as np
def linear_function():
np.random.seed(1)
X = tf.placeholder(dtype = tf.float64, name='X')
W = tf.placeholder(dtype = tf.float64, name='W')
b = tf.placeholder(dtype = tf.float64, name='b')
Y = tf.add(tf.matmul(W, X), b)
sess = tf.Session()
result = sess.run(Y, feed_dict={W:np.random.randn(4,3), X:np.random.randn(3,1), b:np.random.randn(4,1)})
sess.close()
return result
print( "result = " + str(linear_function()))
结果是:
result = [[-1.98748544]
[-2.76826248]
[-0.78635415]
[-2.77463846]]
第二个函数
第二个函数使用tf.constant
来初始化变量:
def linear_function():
np.random.seed(1)
X = tf.constant(np.random.randn(3,1), name ="X")
W = tf.constant(np.random.randn(4,3), name ="X")
b = tf.constant(np.random.randn(4,1), name ="X")
Y = tf.add(tf.matmul(W,X), b)
sess = tf.Session()
result = sess.run(Y)
sess.close()
return result
print( "result = " + str(linear_function()))
结果:
result = [[-2.15657382]
[ 2.95891446]
[-1.08926781]
[-0.84538042]]
有什么问题吗?它与np.random.seed(1)
有关吗?
谢谢。
最佳答案
在第一个片段中,feed_dict
是:
{W:np.random.randn(4,3), X:np.random.randn(3,1), b:np.random.randn(4,1)}
因此,首先生成 W
的随机值,然后生成 X
的随机值,最后生成 b
的随机值。但是,在第二个片段中,随机值按 X
、W
和 b
的顺序给出。由于生成随机数的顺序不同,因此值不同。例如,如果您在第一个代码段的 feed_dict
中充分更改顺序,您将获得与第二个代码段相同的结果:
import tensorflow as tf
import numpy as np
def linear_function():
np.random.seed(1)
X = tf.placeholder(dtype = tf.float64, name='X')
W = tf.placeholder(dtype = tf.float64, name='W')
b = tf.placeholder(dtype = tf.float64, name='b')
Y = tf.add(tf.matmul(W, X), b)
sess = tf.Session()
result = sess.run(Y, feed_dict={X:np.random.randn(3,1), W:np.random.randn(4,3), b:np.random.randn(4,1)})
sess.close()
return result
print( "result = " + str(linear_function()))
输出:
result = [[-2.15657382]
[ 2.95891446]
[-1.08926781]
[-0.84538042]]
关于python - Tensorflow的占位符初始化与tensorflow的常量初始化不同。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54865674/