【问题标题】:tf.reduce_sum returning larger than expected valuetf.reduce_sum 返回大于预期值
【发布时间】:2017-05-26 17:05:38
【问题描述】:

我正在尝试编写一个接收输入特征并返回输出的神经网络。但是,我想通过将输出与实际输出进行比较来检查 NN 的“正确性”。同时,我想让这个指标考虑输出中的不确定性。假设如果预测输出与实际输出相差 1 个单位以内,则将该预测输出视为正确。

代码意图:检查是否 |x-y|小于或等于 1 ,如果是这样,计算所有出现这种情况的情况。基本上这样我就可以知道有多少案例是真实的。

下面是代码,

correct = tf.reduce_sum(tf.cast(tf.less_equal(tf.abs(x - y), 1), tf.int32))
correct.eval({x: predicted_output, y = real_output})

当我将一个小列表传递给字典(下面的代码)时,我可以得到正确的结果:

{x: [1,2,3,4,5], y: [1,2,3,1,1,]}

但是,当我传递 长度为 10 000预测输出实际输出 时,有时返回值是 更多超过 10 000。

我是否正确假设返回值必须小于 10 000?如果是,那么我犯了什么错误会导致返回值超过 10 000?

已编辑以包含完整的代码:

def neural_network_model(data):
 hidden_1_layer = {"weights": tf.Variable(tf.random_normal([n_input, n_nodes_hl1])),
                  "biases": tf.Variable(tf.random_normal([n_nodes_hl1]))}
 hidden_2_layer = {"weights": tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                  "biases": tf.Variable(tf.random_normal([n_nodes_hl2]))}
 hidden_3_layer = {"weights": tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                  "biases": tf.Variable(tf.random_normal([n_nodes_hl3]))}
 output_layer = {"weights": tf.Variable(tf.random_normal([n_nodes_hl3, n_output])),
                  "biases": tf.Variable(tf.random_normal([n_output]))}

 l1 = tf.add(tf.matmul(data, hidden_1_layer["weights"]), hidden_1_layer["biases"])
 l1 = tf.nn.relu(l1)

 l2 = tf.add(tf.matmul(l1, hidden_2_layer["weights"]), hidden_2_layer["biases"])
 l2 = tf.nn.relu(l2)

 l3 = tf.add(tf.matmul(l2, hidden_3_layer["weights"]), hidden_3_layer["biases"])
 l3 = tf.nn.relu(l3)

 output = tf.matmul(l3, output_layer["weights"]) + output_layer["biases"]

 return output

prediction = neural_network_model(x)
correct = tf.reduce_sum(tf.cast(tf.less_equal(tf.abs(prediction - y), 1), tf.int32))
correct.eval({x: val_features, y: val_label})

【问题讨论】:

  • 随机整数对我来说很好。你能告诉我你有多少课吗?是 10000 吗?你检查过 x 和 y 的最大值和最小值吗?
  • @hars 类数为 1。它是一个单一的连续输出NN。我已编辑问题以包含有关代码的更多信息。未包含在编辑中,正在运行培训课程。在检查正确之前。
  • 你检查过 x,y 的大小和它的帽子值了吗?
  • 只有上面的信息,我最好打赌 val_featuresval_labels(或两者)之一具有超过 1 个等级。
  • @hars 在 tensorflow 会话中检查张量形状的最佳方法是什么?我不习惯 tf 使用的这种计算图方法。 val_features 是一个 10000 x 12 数组。 val_label 是一个 10000 长的向量。哪个应该是正确的形状是的?

标签: python tensorflow


【解决方案1】:

找到错误的根源。

在这种情况下,val_label 取自一个名为 data 的更大的 numpy 数组。标签位于 data 数组的最后一列。

val_label = data[:, -1]

这显然返回了一个维度为 (10 000, ) 的数组,它是一个向量

当它与维度为 (10 000, 1) 的 val_labels 的张量进行比较时,就会发生错误。

修复方法是确保 val_label 数组的维度为 (10 000, 1),如下所示:

val_label = data[:, -1:]

或:

val_label = data[:, -1]
val_label = val_label.reshape((-1,1))

重新评估张量流图将返回正确的预期输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2013-09-04
    • 2016-02-13
    • 2012-02-04
    • 2012-02-04
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多