我正在尝试构建一个 C++ 程序来读取预训练模型并使用它。我拿了代码 from here并对其进行了一些修改。 我现在拥有的是:
int main(int argc, char* argv[]) {
// Initialize a tensorflow session
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
// Read in the protobuf graph we exported
GraphDef graph_def;
status = ReadTextProto(Env::Default(), "models/train.pbtxt", &graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
// Add the graph to the session
status = session->Create(graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
tensorflow::Tensor inputs(DT_FLOAT, TensorShape({46}));
auto inputs_flat = inputs.flat<float>();
inputs_flat.setRandom();
// The session will initialize the outputs
std::vector<tensorflow::Tensor> outputs;
status = session->Run({{"input", inputs}}, {"output"}, {}, &outputs);
if (!status.ok()) {
std::cout << status.ToString() << "\n"; // <--- error shows here
return 1;
}
// Grab the first output
// and convert the node to a scalar representation.
auto output_c = outputs[0].scalar<int>();
// Print the results
std::cout << outputs[0].DebugString() << "\n";
std::cout << output_c() << "\n";
// Free any resources used by the session
session->Close();
return 0;
}
但是当我运行它的时候,我得到了
Invalid argument: You must feed a value for placeholder tensor 'output' with dtype int64
[[Node: output = Placeholder[_output_shapes=[[-1]], dtype=DT_INT64, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
我在 models/train.pbtxt
中阅读的图表有 14K 行,所以我不会在这里复制它。我会把相关部分:
...................
node {
name: "input"
op: "Placeholder"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: -1
}
dim {
size: 46
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
}
}
}
}
node {
name: "output"
op: "Placeholder"
attr {
key: "_output_shapes"
value {
list {
shape {
dim {
size: -1
}
}
}
}
}
attr {
key: "dtype"
value {
type: DT_INT64
}
}
attr {
key: "shape"
value {
shape {
}
}
}
}
...................
问题是这样的:这条错误消息告诉我什么?
最佳答案
在原始图中,有一个名为"output"
的节点是tf.placeholder()
,即必须为 fed 的符号张量当您运行任何依赖它的操作时。
在下一行中,调用了session->Run()
,您要告诉 TensorFlow 评估并获取名为 "output"
的节点的结果:
status = session->Run({{"input", inputs}}, {"output"}, {}, &outputs);
这似乎没有意义:为什么要获取必须在同一行上提供的占位符的值?
我怀疑名为 "output"
的节点实际上并不对应于您的模型的输出(例如预测),而是用于输入预期输出的占位符(例如已知的为 "input"
提供的相应值的标签)。图中可能还有一些其他节点,您可以评估这些节点以获得预测,但其名称将取决于您最初构建该图的方式。
关于c++ - 为什么我得到 "You must feed a value for placeholder tensor ' 输出 ' 与 dtype int64"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41429665/