Tensorflow中的交互式会话 - 卷积运算符的不同输出

时间:2016-10-24 15:05:11

标签: tensorflow python-3.5

我是tensorflow的新手,我遇到了InteractiveSession的问题。

在以下代码中:

import tensorflow as tf

def weight_variable(shape):
  initial = tf.random_uniform(shape, 0, 10, seed=1, dtype="int32")
  print("weights=\n",initial.eval())
  return tf.Variable(tf.to_float(initial))

def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


# first dimension: Number of examples to train on, 2nd and 3rd: example width and height, 
# last one is: the number of channels 

x = tf.to_float(tf.Variable([[[[1],     [4],    [5],    [6],    [7]],  
                             [[10],   [11],   [22],    [9],    [8]],  
                             [[24],   [25],   [20],   [21],   [19]],  
                             [[14],   [12],   [13],    [3],   [18]],  
                             [[15],   [16],   [19],   [18],   [17]]]])) # 1 example of 5x5 one channel image


sess = tf.InteractiveSession()

# The first two dimensions are the patch size, the next is the number of input channels, 
# and the last is the number of output channels. 
W_conv1 = weight_variable([2, 2, 1, 1]) #[3,3,3,64]

conv = conv2d(x, W_conv1)


sess.run(tf.initialize_all_variables())

print(sess.run(conv))

sess.close()

当我评论这一行时:

print("weights=\n",initial.eval())

我打印卷积print(sess.run(conv))时会得到不同的结果。我理解关键字eval与会话交互,但我理解的方式是,如果我使用它,它不会改变输出。

以下是使用initial.eval()时获得的输出:

  

[[[[7]]

     

[[9]]]

     

[[[3]]

     

[[2]]]] [[[[156.] [209.] [278.] [167.] [79。]]

     

[[389.] [472.] [337。] [319.] [179。]]

     

[[386.] [332。] [314.] [254.] [181。]]

     

[[293.] [317.] [262。] [360.] [171。]]

     

[[143.] [168.] [163.] [154.] [17。]]]]

当我评论该行时,我得到:

  

[[[[95.] [150。] [173.] [148.] [73。]]

     

[[291.] [390。] [337。] [236.] [113。]]

     

[[459.] [417.] [374.] [363.] [187。]]

     

[[283.] [287.] [211。] [271.] [177。]]

     

[[249.] [283.] [295。] [279.] [119。]]]]

请注意,156更改为95以及卷积的其余输出。

1 个答案:

答案 0 :(得分:2)

这是因为种子如何为RNG工作。在tf.random_uniform中设置操作级别种子为伪RNG提供了固定的起点,但意味着对op的重复评估将产生相同的随机数。如果您查看tf.set_random_seed并通过两次调用eval()并打印输出的玩具示例,可以在文档中看到这一点:

In [2]: initial = tf.random_uniform((5,), 0, 10, seed=1, dtype="int32")
   ...: print("weights=\n",initial.eval())
   ...: print("weights=\n",initial.eval())
   ...: 
('weights=\n', array([7, 9, 3, 2, 7], dtype=int32))
('weights=\n', array([3, 5, 5, 4, 9], dtype=int32))

In [3]: initial = tf.random_uniform((5,), 0, 10, seed=1, dtype="int32")
   ...: print("weights=\n",initial.eval())
   ...: print("weights=\n",initial.eval())
   ...: 
('weights=\n', array([7, 9, 3, 2, 7], dtype=int32))
('weights=\n', array([3, 5, 5, 4, 9], dtype=int32))