ONE - On-device Neural Engine
Loading...
Searching...
No Matches
example_array.cpp File Reference
#include "ndarray/Array.h"
#include <iostream>
#include <iterator>

Go to the source code of this file.

Functions

void gather_array (const Array< float > &input, Array< float > &output, const Array< int > &indices)
 
int main ()
 

Function Documentation

◆ gather_array()

void gather_array ( const Array< float > &  input,
Array< float > &  output,
const Array< int > &  indices 
)

Definition at line 24 of file example_array.cpp.

25{
26 assert(indices.shape().rank() == 3);
27 assert(input.shape().rank() == 3);
28 assert(indices.shape().dim(1) == input.shape().rank());
29
30 for (size_t i = 0; i < indices.shape().dim(0); ++i)
31 {
32 for (size_t j = 0; j < indices.shape().dim(1); ++j)
33 {
34 auto index = indices.slice(i, j);
35 output.slice(i, j).assign(input.slice(index[0], index[1]));
36 }
37 }
38}
const Shape & shape() const noexcept
Definition Array.h:130
ContiguousSpan< T, std::is_const< T >::value > slice(Ts... x) noexcept
returns last dimension as ContigniousSpan
Definition Array.h:106
size_t dim(int i) const noexcept
Definition Shape.h:44
size_t rank() const noexcept
Definition Shape.h:57
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References ndarray::Shape::dim(), ndarray::Shape::rank(), ndarray::Array< T >::shape(), and ndarray::Array< T >::slice().

Referenced by main().

◆ main()

int main ( void  )

Definition at line 40 of file example_array.cpp.

41{
42 // fill tensor of shape[3,3,4] with sequential numbers from [0..36)
43 Shape in_shape{3, 3, 4};
44 std::vector<float> input_data(in_shape.element_count());
45 for (size_t i = 0; i < in_shape.element_count(); ++i)
46 input_data[i] = i;
47
48 Array<float> input(input_data.data(), in_shape);
49
50 // select column-vectors on main diagonal
51 Shape indices_shape{1, 3, 2};
52 std::vector<int> indices_data(indices_shape.element_count());
53 Array<int> indices(indices_data.data(), indices_shape);
54
55 indices.slice(0, 0) = {0, 0};
56 indices.slice(0, 1) = {1, 1};
57 indices.slice(0, 2) = {2, 2};
58
59 Shape output_shape{1, 3, 4};
60 std::vector<float> output_data(output_shape.element_count());
61
63
64 gather_array(input, output, indices);
65
66 for (size_t i = 0; i < indices_shape.dim(0); ++i)
67 {
68 for (size_t j = 0; j < indices_shape.dim(1); ++j)
69 {
70 auto output_piece = output.slice(i, j);
71 std::ostream_iterator<int> cout_it(std::cout, ", ");
72 std::copy(output_piece.begin(), output_piece.end(), cout_it);
73 std::cout << std::endl;
74 }
75 }
76}
void gather_array(const Array< float > &input, Array< float > &output, const Array< int > &indices)
const luci_interpreter::RuntimeShape output_shape
list input_data
Definition infer.py:29
Definition Shape.h:28

References gather_array(), output_shape, and ndarray::Array< T >::slice().