gpt4 book ai didi

c++ - tensorflow C++ : use array for feed_dict

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:45 26 4
gpt4 key购买 nike

我在 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("Error\n");
}
}

return 0;
}

然而,在 C++ 中将值发送到占位符的格式显示在这个 link 中。 . C++ 中使用的 feedtype 给出 here .

我对如何将二维数组修改为 feeddict 格式以便在“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);

关于c++ - tensorflow C++ : use array for feed_dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48770708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com