Alfan Rizaldy
Alfan Rizaldy

Reputation: 9

How to create custom tensor value with shape (b,n,m)?

How to create custom tensor value with shape (b,n,m) I see the cppflow::fill method but it allows inserting 1 value which fills the whole same value in the shape I see https://github.com/serizba/cppflow/issues/114 but have not found how to fill the value from the custom value or vector for example

I've already created a 2d vector using

    vector<vector<float>> tensordata;
  
    for(int i=0; i<cloud->points.size(); i++)
    {
        vector<float> temp;
        for(int j=0; j<3; j++)
        {
            if(j==0)
            {
                temp.push_back(cloud->points[i].x);
            }
            if(j==1)
            {
                temp.push_back(cloud->points[i].y);
            }
            if(j==2)
            {
                temp.push_back(cloud->points[i].z);
            }            
        }
        tensordata.push_back(temp);
    }

but still, there's an error to convert it into a tensor.

Upvotes: 0

Views: 470

Answers (2)

starball
starball

Reputation: 50430

Quoting from the answer to this question provided by GitHub user @serizba, the author of serizba/cppflow in issue ticket #187- in particular, in comment 1087220079:

I see. You cannot create a tensor directly from a multidimensional std::vector. You must create a flat std:vector<float> tensordata, and then convert it to tensor specifying the shape.

In your case, it wouold be something like:

vector<float> tensordata;
for(int i=0; i<cloud->points.size(); i++)
{
    for(int j=0; j<3; j++)
    {
        if(j==0)
            tensordata.push_back(cloud->points[i].x);
        if(j==1)
            tensordata.push_back(cloud->points[i].y);
        if(j==2)
            tensordata.push_back(cloud->points[i].z);     
    }
}
auto mytensor = cppflow::tensor(tensordata, {1, cloud->points.size(), 3});

Upvotes: 0

YScharf
YScharf

Reputation: 2037

Create a vector of floats (a 1 dimensional vector, not a vector of vectors). vector<float> tensor_data;

Fill the vector using tensor_data.push_back() as if it was 1d data.

Initialize a cppflow tensor with the right dimensions using the vector.

cppflow::tensor tnsr = cppflow::tensor(tensor_data, { b, n, m});

Upvotes: 0

Related Questions