Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Last active March 4, 2019 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobiasd/8f41ef8bf4198ab535060a78b53f2008 to your computer and use it in GitHub Desktop.
Save Dobiasd/8f41ef8bf4198ab535060a78b53f2008 to your computer and use it in GitHub Desktop.

Of course one can use fdeep::tensor5 as the primary data structure and fill it with values like so:

#include <fdeep/fdeep.hpp>
int main()
{
    fdeep::tensor5 t(fdeep::shape5(1, 1, 3, 1, 1), 0);
    t.set(0, 0, 0, 0, 0, 1);
    t.set(0, 0, 1, 0, 0, 2);
    t.set(0, 0, 2, 0, 0, 3);
}

But in case one already has an std::vector<float> with values, one might want to re-use it.

So the std::vector<float> needs to be converted to fplus::shared_ref<std::vector<float>> for fdeep::tensor to accept it in its constructor.

T can be converted to fplus::shared_ref<T> by using fplus::make_shared_ref<T>:

#include <fdeep/fdeep.hpp>
int main()
{
    const std::vector<float> v = {1, 2, 3};
    const fdeep::shared_float_vec sv(fplus::make_shared_ref<fdeep::float_vec>(v));
    fdeep::tensor5 t(fdeep::shape5(1, 1, 3, 1, 1), sv);
}

In case the original vector is no longer needed, the copy of the value can be avoided by making it an r-value with std::move:

#include <fdeep/fdeep.hpp>
int main()
{
    const std::vector<float> v = {1, 2, 3};
    const fdeep::shared_float_vec sv(fplus::make_shared_ref<fdeep::float_vec>(std::move(v)));
    fdeep::tensor5 t(fdeep::shape5(1, 1, 3, 1, 1), sv);
}
@Pcres91
Copy link

Pcres91 commented Oct 18, 2018

Thanks for the example. Can this be updated to reflect that fdeep::shape3 has changed its name to fdeep::shape_hwc? I had to go code hunting to find the change

@Dobiasd
Copy link
Author

Dobiasd commented Dec 12, 2018

@Pcres91: Oh, yes. Thanks for the remark. It's updated now (shape5 meanwhile).

@bforland
Copy link

bforland commented Feb 14, 2019

I can't seem to make it work, so can this work with 2d std::vector 's ?

@Dobiasd
Copy link
Author

Dobiasd commented Mar 4, 2019

@bforland: You'd have to write a loop to copy over the values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment