为什么我会得到"You must feed a value for placeholder tensor 'output' with dtype int64"?

Why do I get "You must feed a value for placeholder tensor 'output' with dtype int64"?

本文关键字:tensor int64 dtype placeholder output with value You must 为什么 feed      更新时间:2023-10-16

我正在尝试构建一个C++程序,该程序读取预先训练的模型并使用它。我从这里获取了代码并对其进行了一点修改。我现在拥有的是:

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(),即当你运行任何依赖于它的操作时必须馈送的符号张量。

在以下调用 session->Run() 的行中,您告诉 TensorFlow 评估并获取名为 "output" 的节点的结果:

status = session->Run({{"input", inputs}}, {"output"}, {}, &outputs);

这似乎没有意义:为什么要获取必须在同一行上馈送的占位符的值?

我怀疑名为 "output" 的节点实际上并不对应于模型的输出(例如预测),而是用于馈送预期输出的占位符(例如,馈送到"input"的相应值的已知标签)。图中可能还有其他一些节点可供您评估以获取预测,但其名称将取决于您最初构建图形的方式。