5 #include <boost/numeric/conversion/cast.hpp> 7 #include "tensorflow/core/platform/env.h" 14 return boost::numeric_cast<
int>(
x);
19 const std::vector<InputConfigKeys> &scalarInputKeys,
20 const std::vector<InputConfigKeys> &vectorInputKeys,
21 const std::vector<std::string> &outputKeys
22 ) :
config(savedir, scalarInputKeys, vectorInputKeys, outputKeys),
28 const std::vector<std::string> &vars,
float fillValue
32 DT_FLOAT, TensorShape( {1, 1,
asInt(vars.size())} )
37 for (
int varIdx = 0; varIdx <
asInt(vars.size()); varIdx++) {
38 resultData(0, 0, varIdx) = fillValue;
45 const std::unordered_map<std::string, double> &
varMap,
46 const std::vector<std::string> &vars
50 DT_FLOAT, TensorShape( {1,
asInt(vars.size())} )
54 for (
int varIdx = 0; varIdx <
asInt(vars.size()); varIdx++) {
55 resultData(0, varIdx) = varMap.at(vars[varIdx]);
63 const std::vector<std::string> &vars
70 const size_t vectorSize = (
varMap.empty()) ? 0 :
varMap.at(vars[0]).size();
72 if (vectorSize == 0) {
81 DT_FLOAT, TensorShape({ 1,
asInt(vectorSize),
asInt(vars.size()) })
85 for (
int varIdx = 0; varIdx <
asInt(vars.size()); varIdx++)
89 if (
values.size() != vectorSize) {
90 throw std::runtime_error(
"Vectors have different lengths");
93 for (
int i = 0; i <
asInt(vectorSize); i++) {
94 resultData(0, i, varIdx) =
values[i];
107 auto status = NewSession(SessionOptions(), &session);
112 throw std::runtime_error(
113 "Failed to initialize TF Session: " +
status.ToString()
122 throw std::runtime_error(
123 "Failed to load TF Graph: " +
status.ToString()
129 throw std::runtime_error(
130 "Failed to create TF Session: " + status.ToString()
151 std::vector<std::pair<std::string, Tensor>>
inputs;
160 inputConfig.nodeName,
167 inputConfig.nodeName,
177 throw std::runtime_error(
178 "Failed to run TF Session: " +
status.ToString()
static tensorflow::Tensor constructScalarInput(const std::unordered_map< std::string, double > &varMap, const std::vector< std::string > &vars)
def graph(desc, maker=maker)
std::vector< tensorflow::Tensor > predict(const VarDict &vars) const
void initTFSession() const
std::string getModelPath() const
void ensure_initialized() const
std::unordered_map< std::string, std::vector< double > > vector
TFModel(const std::string &savedir, const std::vector< InputConfigKeys > &scalarInputKeys, const std::vector< InputConfigKeys > &vectorInputKeys, const std::vector< std::string > &outputKeys)
def session(dburl="sqlite:///:memory:")
const std::vector< InputConfig > & getVectorInputs() const
static tensorflow::Tensor constructDummyVectorInput(const std::vector< std::string > &vars, float fillValue=0.0)
const std::vector< std::string > & getOutputNodes() const
static tensorflow::Tensor constructVectorInput(const std::unordered_map< std::string, std::vector< double >> &varMap, const std::vector< std::string > &vars)
const std::vector< InputConfig > & getScalarInputs() const
static QMap< QCString, MemberDef * > varMap
std::shared_ptr< tensorflow::Session > tfSession
std::unordered_map< std::string, double > scalar