Muser
Muser

Reputation: 603

Tensorflow's placeholder initialization differs from tensorflow's constant initialization. Why?

I have written 2 functions that initialize tensorflow's variables in different ways. I don't know why the results are different. Here is the first function using placeholder for initialization:

First function

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()))

And the result is:

result = [[-1.98748544]
 [-2.76826248]
 [-0.78635415]
 [-2.77463846]]

Second function

Second function uses tf.constant to initialize variables:

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:

result = [[-2.15657382]
 [ 2.95891446]
 [-1.08926781]
 [-0.84538042]]

What is the problem? Is it related to np.random.seed(1) ?

Thanks.

Upvotes: 1

Views: 163

Answers (1)

javidcf
javidcf

Reputation: 59731

In the first snippet, the feed_dict is:

{W:np.random.randn(4,3), X:np.random.randn(3,1), b:np.random.randn(4,1)}

So first a random value for W is produced, then for X and then for b. However, in the second snippet the random values are given in the order X, W and b. Since the order in which the random numbers are generated is not the same, the values differ. If for example you change the order adequately in the feed_dict in the first snippet you will get the same result as the second one:

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()))

Output:

result = [[-2.15657382]
 [ 2.95891446]
 [-1.08926781]
 [-0.84538042]]

Upvotes: 1

Related Questions