Tensorflow 中的交互式会话 - 卷积运算符的不同输出
Interactive Session in Tensorflow - different output for convolution operator
我是 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和剩下的卷积输出。
这是因为 RNG 的种子运作方式。在 tf.random_uniform
中设置 op-level seed 为伪 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))
我是 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和剩下的卷积输出。
这是因为 RNG 的种子运作方式。在 tf.random_uniform
中设置 op-level seed 为伪 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))