Tmva 0 0 1_ R Tensor

This tutorial illustrates the basic features of the RTensor class, RTensor is a std::vector-like container with additional shape information. The class serves as an interface in C++ between multi-dimensional data and the algorithm such as in machine learning workflows. The interface is similar to Numpy arrays and provides a subset of the functionality.

Author: Stefan Wunsch
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, June 17, 2021 at 05:49 PM.

In [1]:
using namespace TMVA::Experimental;

Create rtensor from scratch

In [2]:
RTensor<float> x({2, 2});
cout << x << endl;
{ { 0, 0 } { 0, 0 } }

Assign some data

In [3]:
x(0, 0) = 1;
x(0, 1) = 2;
x(1, 0) = 3;
x(1, 1) = 4;

Apply transformations

In [4]:
auto x2 = x.Reshape({1, 4}).Squeeze();
cout << x2 << endl;
{ 1, 2, 3, 4 }

Slice

In [5]:
auto x3 = x.Reshape({2, 2}).Slice({{0, 2}, {0, 1}});
cout << x3 << endl;
{ 1, 3 }

Create tensor as view on data without ownership

In [6]:
float data[] = {5, 6, 7, 8};
RTensor<float> y(data, {2, 2});
cout << y << endl;
{ { 5, 6 } { 7, 8 } }

Create tensor as view on data with ownership

In [7]:
auto data2 = std::make_shared<std::vector<float>>(4);
float c = 9;
for (auto &v : *data2) {
   v = c;
   c++;
}

RTensor<float> z(data2, {2, 2});
cout << z << endl;
{ { 9, 10 } { 11, 12 } }