Tensorflow random seed not working

2019-07-01 10:55发布

问题:

I have following code snippet in my project

import numpy as np
np.random.seed(1337)  # for reproducibility
import tensorflow as tf
tf.set_random_seed(1)

  # Here are the dictionary of weights and biases of each layer
        weights = {
            'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=STDDEV, seed=1)),
            'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=STDDEV, seed=1)),
        }

        biases = {
            'b1': tf.Variable(tf.random_normal([n_hidden_1], seed=1)),
            'b2': tf.Variable(tf.random_normal([n_hidden_2], seed=1))
        }

 init_op = tf.global_variables_initializer()
    # Launch session
    sess = tf.Session()
    sess.run(init_op)

        for i in range(total_batch):
            randidx = np.random.randint(int(TRAIN_SIZE), size=BATCH_SIZE)
            batch_xs = data_train[randidx, :]
            batch_ys = labels_train[randidx, :]
            # Fit using batched data
            sess.run(optimizer, feed_dict={X: batch_xs, y: batch_ys, dropout_keep_prob: 0.9})
            # Calculate average cost
            avg_cost += sess.run(cost, feed_dict={X: batch_xs, y: batch_ys, dropout_keep_prob: 1.}) / total_batch

As you can see I have set random set at the top and each time I run this code it generates different results. Why my random seed not working.