Logo ROOT  
Reference Guide
TensorDataLoader.cxx
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Vladimir Ilievski
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : TTensorDataLoader *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Specialization of the Tensor Data Loader Class *
12 * *
13 * Authors (alphabetical): *
14 * Vladimir Ilievski <ilievski.vladimir@live.com> - CERN, Switzerland *
15 * *
16 * Copyright (c) 2005-2015: *
17 * CERN, Switzerland *
18 * U. of Victoria, Canada *
19 * MPI-K Heidelberg, Germany *
20 * U. of Bonn, Germany *
21 * *
22 * Redistribution and use in source and binary forms, with or without *
23 * modification, are permitted according to the terms listed in LICENSE *
24 * (http://tmva.sourceforge.net/LICENSE) *
25 **********************************************************************************/
26
27///////////////////////////////////////////////////////////////////
28// Specializations of Copy functions for the TensorDataLoader //
29// specialized for the reference architecture. //
30///////////////////////////////////////////////////////////////////
31
33#include "TMVA/DataSetInfo.h"
34
35namespace TMVA {
36namespace DNN {
37
38//______________________________________________________________________________
39template <>
40void TTensorDataLoader<TensorInput, TReference<Real_t>>::CopyTensorInput(std::vector<TMatrixT<Real_t>> &tensor,
41 IndexIterator_t sampleIterator)
42{
43 const std::vector<TMatrixT<Double_t>> &linputTensor = std::get<0>(fData);
44
45 if (fBatchDepth == 1) {
46 for (size_t i = 0; i < fBatchHeight; i++) {
47 size_t sampleIndex = *sampleIterator;
48 for (size_t j = 0; j < fBatchWidth; j++) {
49 tensor[0](i, j) = static_cast<Real_t>(linputTensor[0](sampleIndex, j));
50 }
51 sampleIterator++;
52 }
53 } else {
54 for (size_t i = 0; i < fBatchDepth; i++) {
55 size_t sampleIndex = *sampleIterator;
56 for (size_t j = 0; j < fBatchHeight; j++) {
57 for (size_t k = 0; k < fBatchWidth; k++) {
58 tensor[i](j, k) = static_cast<Real_t>(linputTensor[sampleIndex](j, k));
59 }
60 }
61 sampleIterator++;
62 }
63 }
64}
65
66//______________________________________________________________________________
67template <>
69 IndexIterator_t sampleIterator)
70{
71 const TMatrixT<Double_t> &loutputMatrix = std::get<1>(fData);
72 size_t n = loutputMatrix.GetNcols();
73
74 for (size_t i = 0; i < fInputShape[0]; i++) {
75 size_t sampleIndex = *sampleIterator;
76 for (size_t j = 0; j < n; j++) {
77 matrix(i, j) = static_cast<Real_t>(loutputMatrix(sampleIndex, j));
78 }
79
80 sampleIterator++;
81 }
82}
83
84//______________________________________________________________________________
85template <>
87 IndexIterator_t sampleIterator)
88{
89 const TMatrixT<Double_t> &lweightMatrix = std::get<2>(fData);
90
91 for (size_t i = 0; i < fInputShape[0]; i++) {
92 size_t sampleIndex = *sampleIterator;
93 matrix(i, 0) = static_cast<Real_t>(lweightMatrix(sampleIndex, 0));
94 sampleIterator++;
95 }
96}
97
98//______________________________________________________________________________
99template <>
101 IndexIterator_t sampleIterator)
102{
103 const std::vector<TMatrixT<Double_t>> &linputTensor = std::get<0>(fData);
104
105 if (fBatchDepth == 1) {
106 for (size_t i = 0; i < fBatchHeight; i++) {
107 size_t sampleIndex = *sampleIterator;
108 for (size_t j = 0; j < fBatchWidth; j++) {
109 tensor[0](i, j) = linputTensor[0](sampleIndex, j);
110 }
111 sampleIterator++;
112 }
113 } else {
114 for (size_t i = 0; i < fBatchDepth; i++) {
115 size_t sampleIndex = *sampleIterator;
116 for (size_t j = 0; j < fBatchHeight; j++) {
117 for (size_t k = 0; k < fBatchWidth; k++) {
118 tensor[i](j, k) = linputTensor[sampleIndex](j, k);
119 }
120 }
121 sampleIterator++;
122 }
123 }
124}
125
126//______________________________________________________________________________
127template <>
129 IndexIterator_t sampleIterator)
130{
131 const TMatrixT<Double_t> &loutputMatrix = std::get<1>(fData);
132 size_t n = loutputMatrix.GetNcols();
133
134 for (size_t i = 0; i < fInputShape[0]; i++) {
135 size_t sampleIndex = *sampleIterator;
136 for (size_t j = 0; j < n; j++) {
137 matrix(i, j) = loutputMatrix(sampleIndex, j);
138 }
139
140 sampleIterator++;
141 }
142}
143
144//______________________________________________________________________________
145template <>
147 IndexIterator_t sampleIterator)
148{
149 const TMatrixT<Double_t> &lweightMatrix = std::get<2>(fData);
150
151 for (size_t i = 0; i < fInputShape[0]; i++) {
152 size_t sampleIndex = *sampleIterator;
153 matrix(i, 0) = lweightMatrix(sampleIndex, 0);
154 sampleIterator++;
155 }
156}
157
158//______________________________________________________________________________
159template <>
160void TTensorDataLoader<TMVAInput_t, TReference<Real_t>>::CopyTensorInput(std::vector<TMatrixT<Real_t>> &tensor,
161 IndexIterator_t sampleIterator)
162{
163 // one event, one example in the batch
164 Event *event = std::get<0>(fData)[0];
165
166 if (fBatchDepth == 1) {
167 for (size_t i = 0; i < fBatchHeight; i++) {
168 size_t sampleIndex = *sampleIterator;
169 for (size_t j = 0; j < fBatchWidth; j++) {
170 event = std::get<0>(fData)[sampleIndex];
171 tensor[0](i, j) = static_cast<Real_t>(event->GetValue(j));
172 }
173 sampleIterator++;
174 }
175 } else {
176 for (size_t i = 0; i < fBatchDepth; i++) {
177 size_t sampleIndex = *sampleIterator;
178 for (size_t j = 0; j < fBatchHeight; j++) {
179 for (size_t k = 0; k < fBatchWidth; k++) {
180 event = std::get<0>(fData)[sampleIndex];
181 tensor[i](j, k) = static_cast<Real_t>(event->GetValue(j * fBatchWidth + k));
182 }
183 }
184 sampleIterator++;
185 }
186 }
187}
188
189//______________________________________________________________________________
190template <>
192 IndexIterator_t sampleIterator)
193{
194 const DataSetInfo &info = std::get<1>(fData);
195 Int_t n = matrix.GetNcols();
196
197 for (size_t i = 0; i < fInputShape[0]; i++) {
198 size_t sampleIndex = *sampleIterator++;
199 Event *event = std::get<0>(fData)[sampleIndex];
200
201 for (Int_t j = 0; j < n; j++) {
202 if (event->GetNTargets() == 0) {
203 if (n == 1) {
204 matrix(i, j) = (info.IsSignal(event)) ? 1.0 : 0.0;
205 } else {
206 matrix(i, j) = 0.0;
207 if (j == (Int_t)event->GetClass()) {
208 matrix(i, j) = 1.0;
209 }
210 }
211 } else {
212 matrix(i, j) = static_cast<Real_t>(event->GetTarget(j));
213 }
214 }
215 }
216}
217
218//______________________________________________________________________________
219template <>
221 IndexIterator_t sampleIterator)
222{
223 for (size_t i = 0; i < fInputShape[0]; i++) {
224 size_t sampleIndex = *sampleIterator++;
225 Event *event = std::get<0>(fData)[sampleIndex];
226 matrix(i, 0) = static_cast<Real_t>(event->GetWeight());
227 }
228}
229
230//______________________________________________________________________________
231template <>
233 IndexIterator_t sampleIterator)
234{
235 // one event, one example in the batch
236 Event *event = std::get<0>(fData)[0];
237
238 if (fBatchDepth == 1) {
239 for (size_t i = 0; i < fBatchHeight; i++) {
240 size_t sampleIndex = *sampleIterator;
241 for (size_t j = 0; j < fBatchWidth; j++) {
242 event = std::get<0>(fData)[sampleIndex];
243 tensor[0](i, j) = event->GetValue(j);
244 }
245 sampleIterator++;
246 }
247 } else {
248 for (size_t i = 0; i < fBatchDepth; i++) {
249 size_t sampleIndex = *sampleIterator;
250 for (size_t j = 0; j < fBatchHeight; j++) {
251 for (size_t k = 0; k < fBatchWidth; k++) {
252 event = std::get<0>(fData)[sampleIndex];
253 tensor[i](j, k) = event->GetValue(j * fBatchWidth + k);
254 }
255 }
256 sampleIterator++;
257 }
258 }
259}
260
261//______________________________________________________________________________
262template <>
264 IndexIterator_t sampleIterator)
265{
266 const DataSetInfo &info = std::get<1>(fData);
267 Int_t n = matrix.GetNcols();
268
269 for (size_t i = 0; i < fInputShape[0]; i++) {
270 size_t sampleIndex = *sampleIterator++;
271 Event *event = std::get<0>(fData)[sampleIndex];
272
273 for (Int_t j = 0; j < n; j++) {
274 if (event->GetNTargets() == 0) {
275 if (n == 1) {
276 matrix(i, j) = (info.IsSignal(event)) ? 1.0 : 0.0;
277 } else {
278 matrix(i, j) = 0.0;
279 if (j == (Int_t)event->GetClass()) {
280 matrix(i, j) = 1.0;
281 }
282 }
283 } else {
284 matrix(i, j) = event->GetTarget(j);
285 }
286 }
287 }
288}
289
290//______________________________________________________________________________
291template <>
293 IndexIterator_t sampleIterator)
294{
295 for (size_t i = 0; i < fInputShape[0]; i++) {
296 size_t sampleIndex = *sampleIterator++;
297 Event *event = std::get<0>(fData)[sampleIndex];
298 matrix(i, 0) = event->GetWeight();
299 }
300}
301
302} // namespace DNN
303} // namespace TMVA
int Int_t
Definition: RtypesCore.h:41
float Real_t
Definition: RtypesCore.h:64
Class that contains all the data information.
Definition: DataSetInfo.h:60
Bool_t IsSignal(const Event *ev) const
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:237
Int_t GetNcols() const
Definition: TMatrixTBase.h:127
TMatrixT.
Definition: TMatrixT.h:39
const Int_t n
Definition: legend1.C:16
typename std::vector< size_t >::iterator IndexIterator_t
Definition: DataLoader.h:42
create variable transformations