tensorflow - 为什么 tf.matmul() 给出看似不一致的结果?

标签 tensorflow

sess = tf.InteractiveSession()
num_elements = 10
output = [[0.76158798] * num_elements]
softmax_w = [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]] * num_elements
print(tf.matmul(output, softmax_w).eval())

给出

[[ 0.76158804  0.76158804  0.76158804  0.76158804  0.76158804  0.76158804 0.76158804]]

将 num_elements 更改为 50

sess = tf.InteractiveSession()
num_elements = 50
output = [[0.76158798] * num_elements]
softmax_w = [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]] * num_elements
print(tf.matmul(output, softmax_w).eval())

给出

[[ 3.80794024  3.80794024  3.80794024  3.80794024  3.80793881  3.80793881 3.80793881]]

为什么第二个示例的结果矩阵中的元素不完全相同?

我正在使用 tensorflow 0.11.0rc0

最佳答案

似乎是由数值错误引起的。我使用您的代码得到了相同的结果,但后来我将 outputsoftmax_w 设置为 float64 张量,问题就消失了:

sess = tf.InteractiveSession()
num_elements = 50
output = tf.convert_to_tensor([[0.76158798] * num_elements], dtype = tf.float64)
softmax_w = tf.convert_to_tensor([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]] * num_elements, dtype = tf.float64)
print(tf.matmul(output, softmax_w).eval())

关于tensorflow - 为什么 tf.matmul() 给出看似不一致的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340570/

相关文章:

python - tensorflow :具有单个神经元的输出层,预期浮点输出[0.0,1.0]

python - tensorflow 对多个图像进行分类

java - 如何确保我的 tensorFlow Java 程序正在 Windows 上使用我的 GPU?

python - 导入 Bazel 构建 Tensorflow

tensorflow - tf.initialize_all_variables() 和 tf.initialize_local_variables() 有什么区别?

python - TensorFlow - Tflearning 错误 feed_dict

deep-learning - 如何在 TensorFlow 中设置权重成本强度?

python - 张量板错误 'Can not convert a AdamOptimizer into a Tensor or Operation.'

python - 只能使用 TensorFlow 中处理梯度的代码示例来实现类似优化器的梯度下降吗?

python - Keras MNIST 目标向量自动转换为 one-hot?