gpt4 book ai didi

c++ - 在 C++ : Getting NaN's as an output of tf. nn.softmax 中测试 tensorflow 模型

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:43 24 4
gpt4 key购买 nike

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

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;

这是我获得的输出:

Tensor<type: float shape: [1,12288] values: [93 69 40]...>
Tensor<type: float shape: [1,2] values: [0.49990705 0.500093]>
Tensor<type: float shape: [1,2] values: [0.49977857 0.50022149]>
y_gender_predictions[0]: Female
Tensor<type: float shape: [1,12288] values: [112 84 54]...>
Tensor<type: float shape: [1,2] values: [nan nan]>
Tensor<type: float shape: [1,2] values: [nan nan]>
y_gender_predictions[0]: Male
Tensor<type: float shape: [1,12288] values: [126 106 73]...>
Tensor<type: float shape: [1,2] values: [nan nan]>
Tensor<type: float shape: [1,2] values: [nan nan]>
y_gender_predictions[0]: Male
Tensor<type: float shape: [1,12288] values: [126 108 81]...>
Tensor<type: float shape: [1,2] values: [nan nan]>
Tensor<type: float shape: [1,2] values: [nan nan]>
y_gender_predictions[0]: Male
Tensor<type: float shape: [1,12288] values: [132 112 85]...>
Tensor<type: float shape: [1,2] values: [nan nan]>
Tensor<type: float shape: [1,2] values: [nan nan]>
y_gender_predictions[0]: Male

为什么我只在第一次迭代和之后的迭代中得到 float 结果?我该如何解决这个问题?

此外,我有兴趣了解为什么在对同一图像进行两次评估时会得到不同的数值结果。 (值得一提的是,我在 Python 中加载模型并从 softmax 层获得正确的值。评估相同的图像我总是得到相同的结果。)

提前谢谢你。

最佳答案

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

这是python代码:

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)

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

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

    int number_dim = stream_in[0].dim;
int number_test = stream_in[0].num; // 4072
int number_classes = _n_classes;

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({number_test, number_dim}));
auto dst = input_tensor.flat<float>().data();
for (int i = 0; i < stream_in[0].num; i++) {
std::copy_n(dataptr + i * number_dim, number_dim, dst);
dst += number_dim;
}


std::vector<std::pair<std::string, tensorflow::Tensor>> inputs = {{tokens_io[0], input_tensor}};
std::vector<tensorflow::Tensor> outputs;
status = session->Run(inputs, {tokens_io[1]}, {}, &outputs);
if (!status.ok()) {
ssi_wrn("status: %s \n", status.ToString().c_str());
return;
}

std::vector<int> number_hits(number_classes, 0);
for (std::vector<tensorflow::Tensor>::iterator it = outputs.begin(); it != outputs.end(); ++it) {
auto items = it->shaped<float, 2>({number_test, number_classes});
for (int i = 0; i < number_test; i++) {
int arg_max = 0;
float val_max = items(i, 0);
for (int j = 0; j < number_classes; j++) {
if (items(i, j) > val_max) {
arg_max = j;
val_max = items(i, j);
}
}
for (int i = 0; i < number_classes; i++) {
if (arg_max == i) {
number_hits[i]++;
}
}
}
}

for (int i = 0; i < _n_classes; i++) {
float accuracy = (float) number_hits[i] / number_test;
ssi_wrn("accuracy for class %s : %f \n", _class_names[i], accuracy);
_probs[i] = accuracy;
}
session->Close();

关于c++ - 在 C++ : Getting NaN's as an output of tf. nn.softmax 中测试 tensorflow 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46184486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com