0

编辑:对现有问题的修改。我有以下代码:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <sys/time.h>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"

using namespace std;
using namespace tensorflow;
using namespace tensorflow::ops;

Session* session; // tensorflow session
Status status;  // tensorflow status

long get_prediction(Tensor a);

long get_prediction(Tensor a) {

  long prediction;
  vector<Tensor> outputs;

  // preparing input
  std::vector<std::pair<string, tensorflow::Tensor>> inputs = {{"InputDataLayer/InputXPlaceHolder", a}, {"InputDataLayer/LabelYPlaceHolder", a}}; //TODO: Update this line

  // getting prediction for test data
  status = session->Run(inputs, {"OutputLayer/outputLogits"}, {}, &outputs); //TODO: Update this line

  if (!status.ok()) {
    cout<<"Error@get_prediction: "<<status.ToString()<<"\n";
    return 1l;
  }

  prediction = outputs[0].scalar<long>()(0);

  return prediction;
}

int main(int argc, char *argv[]) {

  ifstream f;
  string line = "";
  string token = "";
  double temp = 0.0;
  double matches = 0.0, accuracy = 0.0;
  int row_no=0, col_no=0;
  long prediction = 0l, actual = 0l;

  status = NewSession(SessionOptions(), &session);
  if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
  }

  GraphDef graph_def;

  status = ReadTextProto(Env::Default(), "/home/userj/Desktop/tensorflow/tensorflow/loader/models.pbtxt", &graph_def);

  if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
  }

  status = session->Create(graph_def);
  if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
  }

  session->Run({}, {}, {"init_all_vars_op"}, nullptr); //Initializes all the variables in C++

  auto a = Tensor(DT_DOUBLE, TensorShape({1, 9})); //TODO: Update this line?

  f.open("/home/userj/Desktop/tensorflow/tensorflow/loader/signals.csv");

  while(getline(f, line)) {

    if (row_no == 0) {
        row_no += 1;
        continue;
    }

    istringstream iss(line);

    while(getline(iss, token, ',')) {

      const char * item = token.c_str();

      if (strlen(item) == 0 && col_no != 2) {
        a.matrix<double>()(0, col_no) = 0;
        col_no += 1;
        continue;
      }

      temp = stod(token.c_str()); //breaking on the missing values

      if (col_no == 1) { //skip the time column
        col_no += 1;
        continue;
      }

      // filling feature tensors
      if(col_no != 2)
        a.matrix<double>()(0, col_no) = temp;

      // filling label tensor
      if(col_no == 2)
        actual = (long) temp;

      col_no += 1;

   }

    col_no = 0;
    row_no += 1;

    // getting prediction
    prediction = get_prediction(a);

    // if actual and prediction matches, increment matches
    if(actual == prediction)
      matches += 1;
  }

  accuracy = matches / (row_no);
  cout<<"Total Rows: "<<(row_no)<<endl;
  cout<<"Accuracy: "<<accuracy<<endl;

  session->Close();
  return 0;
}

当我在 bazel 中运行它时出现此错误:

Invalid argument: Expects arg[1] to be int32 but double is provided
*** Error in `./ml': malloc(): memory corruption (fast): 0x0000000005602fd0 ***

下面是我的 .pbtxt 文件。InputDataLayer/InputXPlaceHolder 和 InputDataLayer/InputLabelYPlaceHolder 是输入,OutputLayer/outputLogits 是输出。有谁知道我做错了什么?我确信它是多方面的,包括运行功能。

node {
  name: "InputDataLayer/InputXPlaceHolder"
  op: "Placeholder"
  attr {
    key: "dtype"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "shape"
    value {
      shape {
        dim {
          size: -1
        }
        dim {
          size: -1
        }
        dim {
          size: 9
        }
      }
    }
  }
}
node {
  name: "InputDataLayer/LabelYPlaceHolder"
  op: "Placeholder"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "shape"
    value {
      shape {
        dim {
          size: -1
        }
      }
    }
  }
}
node {
  name: "RNNLayers/ones_like/Shape"
  op: "Shape"
  input: "InputDataLayer/InputXPlaceHolder"
  attr {
    key: "T"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "out_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/ones_like/Const"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT16
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT16
        tensor_shape {
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "RNNLayers/ones_like"
  op: "Fill"
  input: "RNNLayers/ones_like/Shape"
  input: "RNNLayers/ones_like/Const"
  attr {
    key: "T"
    value {
      type: DT_INT16
    }
  }
  attr {
    key: "index_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/Max/reduction_indices"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 2
      }
    }
  }
}
node {
  name: "RNNLayers/Max"
  op: "Max"
  input: "RNNLayers/ones_like"
  input: "RNNLayers/Max/reduction_indices"
  attr {
    key: "T"
    value {
      type: DT_INT16
    }
  }
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "keep_dims"
    value {
      b: false
    }
  }
}
node {
  name: "RNNLayers/Sum/reduction_indices"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "RNNLayers/Sum"
  op: "Sum"
  input: "RNNLayers/Max"
  input: "RNNLayers/Sum/reduction_indices"
  attr {
    key: "T"
    value {
      type: DT_INT16
    }
  }
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "keep_dims"
    value {
      b: false
    }
  }
}
node {
  name: "RNNLayers/Shape"
  op: "Shape"
  input: "InputDataLayer/InputXPlaceHolder"
  attr {
    key: "T"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "out_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/strided_slice/stack"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 1
          }
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "RNNLayers/strided_slice/stack_1"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 1
          }
        }
        int_val: 2
      }
    }
  }
}
node {
  name: "RNNLayers/strided_slice/stack_2"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 1
          }
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "RNNLayers/strided_slice"
  op: "StridedSlice"
  input: "RNNLayers/Shape"
  input: "RNNLayers/strided_slice/stack"
  input: "RNNLayers/strided_slice/stack_1"
  input: "RNNLayers/strided_slice/stack_2"
  attr {
    key: "Index"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "begin_mask"
    value {
      i: 0
    }
  }
  attr {
    key: "ellipsis_mask"
    value {
      i: 0
    }
  }
  attr {
    key: "end_mask"
    value {
      i: 0
    }
  }
  attr {
    key: "new_axis_mask"
    value {
      i: 0
    }
  }
  attr {
    key: "shrink_axis_mask"
    value {
      i: 1
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims/dim"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims"
  op: "ExpandDims"
  input: "RNNLayers/strided_slice"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims/dim"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "Tdim"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/Const"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 1
          }
        }
        int_val: 128
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat/axis"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat"
  op: "ConcatV2"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/Const"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat/axis"
  attr {
    key: "N"
    value {
      i: 2
    }
  }
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/zeros/Const"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_DOUBLE
        tensor_shape {
        }
        double_val: 0.0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/zeros"
  op: "Fill"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/zeros/Const"
  attr {
    key: "T"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "index_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_1/dim"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_1"
  op: "ExpandDims"
  input: "RNNLayers/strided_slice"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_1/dim"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "Tdim"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/Const_1"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 1
          }
        }
        int_val: 128
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_2/dim"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_2"
  op: "ExpandDims"
  input: "RNNLayers/strided_slice"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_2/dim"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "Tdim"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/Const_2"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 1
          }
        }
        int_val: 128
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat_1/axis"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat_1"
  op: "ConcatV2"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_2"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/Const_2"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat_1/axis"
  attr {
    key: "N"
    value {
      i: 2
    }
  }
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/zeros_1/Const"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_DOUBLE
        tensor_shape {
        }
        double_val: 0.0
      }
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/zeros_1"
  op: "Fill"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/concat_1"
  input: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/zeros_1/Const"
  attr {
    key: "T"
    value {
      type: DT_DOUBLE
    }
  }
  attr {
    key: "index_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "RNNLayers/MultiRNNCellZeroState/LSTMCellZeroState/ExpandDims_3/dim"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}

输出是这样的。

node {
  name: "OutputLayer/outputLogits"
  op: "Sigmoid"
  input: "OutputLayer/Add"
  attr {
    key: "T"
    value {
      type: DT_DOUBLE
    }
  }
}
node {
  name: "save/Const"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_STRING
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_STRING
        tensor_shape {
        }
        string_val: "model"
      }
    }
  }
}
4

0 回答 0