0

I have troubles with my C++ code. I am using OpenNN to buid a Neural Network and I want to use my trained Neural Network (see variable neuralnetwork), to predict the output from a set of features (see variable featureVector). I am trying to call a function which needs a initialiser_list - object as argument. This is the code:

float featureVector[1][5] = {{1, 2, 3, 4, 5}};
Tensor<type, 2> inputs(1, featureLength);
inputs.setValues(featureVector);
neuralnetwork.calculate_outputs(inputs);

I get this error message: neuralnetwork.cpp:141:22: error: reference to type 'const typename internal::Initializer<Tensor<float, 2, 0, long long>, NumDimensions>::InitList' (aka 'const initializer_list<initializer_list>') could not bind to an lvalue of type 'float [1][5]' TensorBase.h:861:81: note: passing argument to parameter 'vals' here

When I change my code according to an example from OpenNN, it works, but this is not what I want because the feature vector needs to be a variable like above:

Tensor<type, 2> inputs(1, featureLength);
inputs.setValues({{1, 2, 3, 4, 5}});
neuralnetwork.calculate_outputs(inputs);

I also tried to change featureVector to a list (std::list) but this does not work, to. I know that featureVector needs to be an initialiser_list object, but I could not find any answers online on how to initialise an initialiser_list-object. It would be nice if featureVector is a array or std::list, which will be converted to a initialiser_list.

Edit:

Type of neuralnetwork:

 OpenNN::NeuralNetwork neuralnetwork;

Function calculate_outputs:

Tensor<type, 2> NeuralNetwork::calculate_outputs(const Tensor<type, 2>& inputs)

With the solution in the comment I figured something out that works for me:

auto featureVector1 = {1.f, 2.f, 3.f, 4.f, 5.f};
auto featureVector = {featureVector1};
Tensor<type, 2> inputs(1, featureLength);
inputs.setValues(featureVector);
neuralnetwork.calculate_outputs(inputs);

It does not look nice and maybe it is not perfect, but it this is exactly how I wanted it. Thank you!

ano
  • 17
  • 1
  • 10
  • what is the signature of `calculate_outputs`, what type is `neuralnetwork`? Initializer lists are typically created on the fly, maybe you just need to add brackets – 463035818_is_not_an_ai May 18 '21 at 12:04
  • Use OpenNN's `Vector` and `Matrix` types instead of arrays or the standard library's collections. – molbdnilo May 18 '21 at 12:14
  • For a simple initializer list, `auto initialize_list = { 1.f, 2.f, 3.f, 4.f, 5.f };` That will define the `initializer_list` variable as a `std::initializer_list`. If you want an initializer lists of initializer lists you must be explicit with the type `std::initializer_list>` – Some programmer dude May 18 '21 at 12:15

2 Answers2

0

How to create initialiser_list object in C++

You use a braced-init-list. Like in the example that you quote.

but this is not what I want

Then it seems like std::initialiser_list is not a good choice for you. You should try to use something else. Have you checked whether the class has some other member functions?

the feature vector needs to be a variable like above

The feature vector in your example isn't variable. It's a constant array.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

You can check in openNN source what arg types availible to use in setValues() function. I`m not find strictly setValues() but here this code:

// Constructors
explicit Tensor(const Vector<size_t>&);
explicit Tensor(const Matrix<T>&);
///....
void set(const Vector<size_t>&);
void set(const Tensor<T>&);

So this mean u can create varaible like Vector<5> featureVector = {{1, 2, 3, 4, 5}}; or maybe if T float allowed Matrix<float> featureVector = {{1, 2, 3, 4, 5}};

Or u can create Tensor object and pass in constructor Vector or Matrix object without using set. Note that this Vector is not the same as std::vector, it`s the difend in openNN lib data structure.