Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include "tensorflow/cc/client/client_session.h"
  2. #include "tensorflow/cc/ops/standard_ops.h"
  3. #include "tensorflow/core/framework/tensor.h"
  4.  
  5. namespace tf = tensorflow;
  6. namespace to = tf::ops;
  7.  
  8. int main() {
  9. tf::Scope r = tf::Scope::NewRootScope();
  10. tf::Scope s = r.ExitOnError();
  11. tf::ClientSession Session(s);
  12.  
  13.  
  14. tf::Output RandW = to::RandomUniform(s, {10}, tf::DT_DOUBLE);
  15. tf::Output W = to::Variable(s.WithOpName("W"), {10}, tf::DT_DOUBLE);
  16. tf::Output AssignToW = to::Assign(s, W, RandW);
  17.  
  18. tf::Output RandB = to::RandomUniform(s, {10}, tf::DT_DOUBLE);
  19. tf::Output b = to::Variable(s.WithOpName("b"), {10}, tf::DT_DOUBLE);
  20. tf::Output AssignToB = to::Assign(s, b, RandB);
  21.  
  22. {
  23. std::vector<tf::Tensor> OutputsOfAssigning;
  24. TF_CHECK_OK(Session.Run({AssignToW}, &OutputsOfAssigning));
  25. LOG(INFO) << OutputsOfAssigning[0].vec<double>();
  26. }
  27.  
  28. {
  29. std::vector<tf::Tensor> OutputsOfAssigning;
  30. TF_CHECK_OK(Session.Run({AssignToB}, &OutputsOfAssigning));
  31. LOG(INFO) << OutputsOfAssigning[0].vec<double>();
  32. }
  33.  
  34.  
  35. tf::Output x = to::Placeholder(s.WithOpName("xInput"), tf::DT_DOUBLE);
  36. tf::Output Wx = to::MatMul(s.WithOpName("Wx"), W, x, to::MatMul::TransposeB(true));
  37. tf::Output Model = to::AddN(s.WithOpName("Wxb"), {b, Wx});
  38.  
  39. std::vector<tf::Tensor> Outputs;
  40. TF_CHECK_OK(Session.Run({{x, {1., 2., 3., 4., 5., 6., 7., 8., 9., 0.}}}, {Model}, &Outputs));
  41. LOG(INFO) << Outputs[0].matrix<double>();
  42.  
  43. return 0;
  44.  
  45.  
  46. // tf::Scope root = tf::Scope::NewRootScope();
  47. // // Matrix A = [3 2; -1 0]
  48. // auto A = to::Const(root, {{3.f, 2.f}, {-1.f, 0.f}});
  49. // // Vector b = [3 5]
  50. // auto b = to::Const(root, {{3.f, 5.f}});
  51. // // v = Ab^T
  52. // auto v = to::MatMul(root.WithOpName("v"), A, b, to::MatMul::TransposeB(true));
  53.  
  54. // std::vector<tf::Tensor> outputs;
  55. // tf::ClientSession session(root);
  56. // // Run and fetch v
  57. // TF_CHECK_OK(session.Run({v}, &outputs));
  58. // // Expect outputs[0] == [19; -3]
  59. // LOG(INFO) << outputs[0].matrix<float>();
  60. // return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement