Tensorflow中的交互式会话-卷积运算符的不同输出
我是tensorflow的新手,在互动会话方面遇到了问题 在以下代码中:Tensorflow中的交互式会话-卷积运算符的不同输出,tensorflow,python-3.5,Tensorflow,Python 3.5,我是tensorflow的新手,在互动会话方面遇到了问题 在以下代码中: 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
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,其余为卷积输出 这是因为种子对RNG的作用。在tf.random\u uniform
中设置op级别种子为伪RNG提供了一个固定的起点,但并不意味着op的重复评估将产生相同的随机数。如果您签出并通过两次调用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))