tensorflow - MNIST 样本中所有权重均为零(不变)

标签 tensorflow tensorboard mnist softmax

我已经使用 softmax 函数在 MNIST 上训练和评估了 TF 的第一个示例。结果与预期一致,约为 92%。但是,我希望看到每次迭代的权重和偏差。

查看代码,我发现每次迭代都初始化为零,这被认为不是一种有效的初始化方式:

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

另一个问题被建议用一个小的 stddev 截断它们:

W = tf.Variable(tf.truncated_normal([784, 10],stddev=0.001))
b = tf.Variable(tf.truncated_normal([10],stddev=0.001))

我也以这种方式进行了测试,但在这两种情况下,权重都没有变化(第一种情况全为零,第二种情况非零),而偏差只是正在变化的偏差。

MWE:

print "Iteration:", '%04d' % (iteration + 1), "cost=", "{:.9f}".format(avg_cost)
print "Bias: ", b.eval()
print "Weights: ", W.eval()

这是前几张打印的结果:

Iteration: 0001 cost= 29.819621965
Bias:  [-0.38608965  0.36391538  0.1257894  -0.25784218  0.0603136   1.46654773
 -0.11613362  0.62797612 -1.63218892 -0.25228417]
Weights:  [[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
Iteration: 0003 cost= 20.975814890
Bias:  [-0.71424055  0.5187394   0.24631855 -0.44207239 -0.07629333  2.24541211
 -0.20360497  1.08866096 -2.26480484 -0.39810511]
Weights:  [[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]

有趣的是,我在张量板查看器中看到一些非零权重:

有人可以解释为什么我会看到这种行为和不匹配吗?我想查看 TensorFlow 中每一层的权重(在本例中我们只有一层)并检查它们的值。

最佳答案

当我们打印 numpy 数组时,只会打印初始值和最后一个值,并且在 MNIST 的情况下,这些权重索引不会更新,因为图像中的相应像素保持不变,因为所有数字都写入数组或图像的中心部分沿着边界区域。从一个输入样本到另一输入样本变化的实际像素是中心像素,因此只有那些相应的权重元素才会得到更新。要比较训练前后的权重,您可以使用 numpy.array_equal(w1, w2) 或者,您可以通过执行以下操作来打印整个 numpy 数组: import numpy numpy.set_printoptions(threshold='nan') 或者,您可以逐个元素进行比较,并仅打印数组中相差特定阈值的那些值

关于tensorflow - MNIST 样本中所有权重均为零(不变),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873438/

相关文章:

tensorboard - 如何在张量板中制作表格

python - TensorFlow:形状错误

javascript - 使用 javascript/node.js 读取 MNIST 数据集

python - 导入错误 : cannot import name 'keras'

python - 为什么tensorflow LSTM只采用隐藏大小作为输入?

python - Tensorboard 获取空白页

tensorflow - 我什么时候应该定义一个新的 TensorFlow op?

python - 模块未找到错误 : No module named 'mnist'

python - 如何安装tensorflow-gpu==1.11.0

matplotlib - 尝试分割图像颜色时出错 : numpy. ndarray' 对象没有属性 'mask'