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
最佳答案
似乎是由数值错误引起的。我使用您的代码得到了相同的结果,但后来我将 output
和 softmax_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/