TensorFlow C :将数组用于feed_dict

Tensorflow C++: use array for feed_dict

本文关键字:feed dict 用于 数组 TensorFlow      更新时间:2023-10-16

i在Tensorflow中具有C 代码,如下所示,涉及使用占位符的矩阵乘法:

#include <stdio.h>
#include <stdlib.h>
#include <ctime> 
#include <iostream>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main(int argc, char const *argv[]){
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();
  auto alpha = Const(root, 2.0, {1, 1});
  auto beta = Const(root, 3.0, {1, 1});
  auto A = Placeholder(root, DT_FLOAT);
  auto B = Placeholder(root, DT_FLOAT);
  auto C = Placeholder(root, DT_FLOAT);
  auto temp1 = MatMul(root, A, B);
  auto temp2 = Mul(root, alpha, temp1);
  auto temp3 = Mul(root, beta, C);
  auto D = Add(root.WithOpName("D"), temp1, temp3);

  std::vector<Tensor> outputs;
  ClientSession session(root);
  int num_size = 2;
  for(int step = 1; step < num_size; step++){
    /*Allocating arrays*/
    int array_size = pow(10, step);
    float **a, **b, **c;
    a = (float **)malloc(sizeof(float)*array_size);
    b = (float **)malloc(sizeof(float)*array_size);
    c = (float **)malloc(sizeof(float)*array_size);
    for(int i = 0; i < array_size; i++){
      a[i] = (float *)malloc(sizeof(float)*array_size);
      b[i] = (float *)malloc(sizeof(float)*array_size);
      c[i] = (float *)malloc(sizeof(float)*array_size);
    }
    srand((unsigned)time(0)); 
    for(int i = 0; i < array_size; i++){
      for(int j = 0; j < array_size; j++){
        a[i][j] = (rand()%100)+1;
        b[i][j] = (rand()%200)+1;
        c[i][j] = (rand()%300)+1;
      }
    }
    for(int num = 0; num < 10; num++){
      Status s = session.Run({{A, a}, {B, b}, {C, c}}, {D}, &outputs);
      if(s.ok())
         c = outputs[0];
      else
         printf("Errorn");
    }
  }
  return 0;
}

但是,此链接中显示了将值发送给占位符的格式。此处给出了C 中使用的FeedType。

我对如何将我的2D阵列修改为饲料格式,以便在" session.run(("中提供。

谢谢。

编辑1

这个问题的最小表示如下 -

考虑以下代码片段:

Scope root = Scope::NewRootScope();
auto a = Placeholder(root, DT_INT32);
// [3 3; 3 3]
auto b = Const(root, 3, {2, 2});
auto c = Add(root, a, b);
ClientSession session(root);
std::vector<Tensor> outputs;
// Feed a <- [1 2; 3 4]
int feed_a[2][2] = {{1, 2}, {3, 4}}; 
session.Run({ {a, feed_a} }, {c}, &outputs);
// The working code is - session.Run({ {a, { {1, 2}, {3, 4} } } }, {c}, &outputs);
// outputs[0] == [4 5; 6 7]

在显示" feed_a"数组从单独函数接收到的情况下,我如何使此代码工作,并且需要使用它来设置占位符'a'的价值。

您需要创建一个C阵列并将数据放在那里而不是使用锯齿状数组。

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();
  // [3 3; 3 3]
  auto b = Const(root, {{3.f, 3.f}, {3.f, 3.f}});
  ClientSession session(root);
  std::vector<Tensor> outputs;
  // just print b
  TF_CHECK_OK(session.Run({}, {b}, &outputs));
  LOG(INFO) << "b = ";
  LOG(INFO) << outputs[0].matrix<float>();

  // just print c = a + b
  float *a_data = new float[4];
  for (int i = 0; i < 4; ++i)
    a_data[i] = 1.f;
  auto a_shape = TensorShape({2, 2});
  auto a_init = Input::Initializer(*a_data, a_shape);
  auto a_plhdr = Placeholder(root, DT_FLOAT);
  auto c = Add(root, a_plhdr, b);
  TF_CHECK_OK(session.Run({{a_plhdr, a_init}}, {c}, &outputs));

  LOG(INFO) << "a + b";
  LOG(INFO) << outputs[0].matrix<float>();
  return 0;
}

给我

2018-02-14 22:45:47.469766: I tensorflow/cc/example/example.cc:20] b = 
2018-02-14 22:45:47.469800: I tensorflow/cc/example/example.cc:21] 3 3
3 3
2018-02-14 22:45:47.473519: I tensorflow/cc/example/example.cc:36] a + b
2018-02-14 22:45:47.473543: I tensorflow/cc/example/example.cc:37] 4 4
4 4

注意,出于某种原因

int32 *a_data = new int32[4];
for (int i = 0; i < 4; ++i)
  a_data[i] = 1;
auto a_shape = TensorShape({2, 2});
auto a_init = Input::Initializer(*a_data, a_shape);
auto a_plhdr = Placeholder(root, DT_INT32);

产生故障(无输出(:

Check failed: dtype() == expected_dtype (1 vs. 3)

无法通过

解决
auto a_casted = Cast(root, a_plhdr, DT_FLOAT)
auto c = Add(root, a_casted, b);