xor_girl
xor_girl

Reputation: 51

How to use Complex Data Type in c++ API of Tensorflow?

I have been using Tensorflow C++ API for some time now and I have a shared build working so I can load a graph in C++ api and perform simple examples like that shown here. I need to make use of Complex Data type of tensorflow in C++ but I can't find any example on that. Can you recommend a very simple usage of complex data type?

Upvotes: 4

Views: 599

Answers (1)

ArafatK
ArafatK

Reputation: 760

If someone down-voted you, then I am sorry newbie. Stack overflow can be bit weird for new users, but it is a great place for programmers, and you should definitely use it to the best of your abilities.

As for you question, You can simply make use of complex numbers library.

If you have the shared build working then you can try this example(after you adjust the include paths for the necessary files needed)

#include <bits/stdc++.h>
#include "tensorflow/core/public/tensor_c_api.h"
#include "tf_session_helper.h"
#include "tf_session_helper.cc"
#include "tf_tensor_helper.cc"
using namespace std;

main()
{ 

    long long adims[] ={3};
    std::complex<double> aData[3]={{1, 2},{3, 4}, {231,452}};     
    auto c = tensorflow::TF_NewTensor_wrapper(TF_DataType::TF_COMPLEX128,adims,(sizeof(adims)/sizeof(*adims)),aData,16*(sizeof(aData)/sizeof(*aData)));
    std::complex<double>* tensor_data = static_cast<std::complex<double>*>(TF_TensorData(c));
    auto dims = TF_NumDims(c);
    auto total_elements = 1;
    for (int i = 0; i < dims; ++i) {
        total_elements *= TF_Dim(c, i);
    }
    for (int i = 0; i < total_elements; ++i) {
        cout << std::real(tensor_data[i]) << " " << std::imag(tensor_data[i]) << endl; 
    }

} 

Let me know if you have any questions.

Upvotes: 2

Related Questions