在c ++中测试tensorflow模型:将NaN' s作为tf.nn.softmax的输出

时间:2017-09-12 20:01:24

标签: c++ tensorflow

当我在C ++中获得softmax图层的输出时,我很挣扎。有时候它会返回正确的价值,有时它会给我NaN's。 这是我用来重现错误的代码片段:

O(n)

这是我获得的输出:

cout << x.DebugString() << endl;

std::vector<std::pair<string, Tensor>> inputs = {
    {"x", x},
};

std::vector<tensorflow::Tensor> outputs;

// Run the session, evaluating our "softmax" operation from the graph
// status = session->Run(inputs, {"softmax_tensor"}, {}, &outputs);
status = session->Run(inputs, {"softmax_tf"}, {}, &outputs);
if (!status.ok()) {
    throw runtime_error(status.ToString());
}

std::cout << outputs[0].DebugString() << std::endl;

outputs.clear();

// Run the session, evaluating our "softmax" operation from the graph
// status = session->Run(inputs, {"softmax_tensor"}, {}, &outputs);
status = session->Run(inputs, {"softmax_tf"}, {}, &outputs);
if (!status.ok()) {
    throw runtime_error(status.ToString());
}

std::cout << outputs[0].DebugString() << std::endl;

为什么我只在第一次迭代中获得浮动结果,然后是NaN&#39; s?我该如何解决这个问题?

同样,我有兴趣了解为什么当我评估两次相同的图像时,我得到不同的数值结果。 (重要的是要提到我在Python中加载模型并从softmax层获得适当的值。评估相同的图像我得到的结果总是相同。)

提前谢谢。

1 个答案:

答案 0 :(得分:0)

你好,你在jackytung博客上问过我。我设法解决了我的问题,但我不确定它是否对你有帮助我只是给你我的代码也许你看到了一些有用的东西。

这是python代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view?.endEditing(true)

    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let node = self.atPoint(location)
        let randomNumber = Int(arc4random_uniform(13))

        if node.name == "deckOfCards" {
            yourCard.texture = SKTexture(imageNamed: "\(cardname[randomNumber])")
        }
    }
}

也许问题可能是您定义为输出图层的图层。对于c ++图,我使用了“logits”作为输出层,但是对于python中的训练,我使用了成本变量。

这是我加载图表的cpp代码:

saver = tf.train.Saver()

# output Node for prediction in c++ !! Still use softmax_cross_entropy method because it is more stable for training
# prediction = tf.nn.softmax(neural_net_layer)
# use logits (and prediction only for c++)
logits = tf.matmul(neural_net_layer, output_layer['weight'], name="output_TT") + output_layer['bias']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

#tupel with (EpochNr, EpochLoss, PredictAccuracy)
train_info = [] 

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    accuracy_tmp = 0  # start to save models if accuracy is over x per cent
    epoch_nr_best_model = 0

    for epoch in range(1,hm_epochs+1):
        epoch_loss = 0
        i = 0
        while i < len(train_x):
            start = i
            end = i + batch_size
            batch_x = np.array(train_x[start:end])
            batch_y = np.array(train_y[start:end])

            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y_: batch_y})
            epoch_loss += c
            i += batch_size

        print('Epoch', epoch, '/', hm_epochs, 'loss:', epoch_loss)

        correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        accuracy_val = accuracy.eval({x: test_x, y_: test_y})
        print('Accuracy:', accuracy_val)