TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
keras_model.cpp
1// TRUST_NO_INDENT
2// Reproduced from https://github.com/moof2k/kerasify
3/*
4 * Copyright (c) 2016 Robert W. Rose
5 *
6 * MIT License, see LICENSE file.
7 */
8
9#include <keras_model.h>
10
11#include <cmath>
12#include <iostream>
13#include <fstream>
14#include <limits>
15#include <stdio.h>
16#include <utility>
17#include <Process.h>
18
19bool ReadUnsignedInt(std::ifstream* file, unsigned int* i) {
20 KASSERT(file, "Invalid file stream");
21 KASSERT(i, "Invalid pointer");
22
23 file->read((char*)i, sizeof(unsigned int));
24 KASSERT(file->gcount() == sizeof(unsigned int), "Expected unsigned int");
25
26 return true;
27}
28
29bool ReadFloat(std::ifstream* file, float* f) {
30 KASSERT(file, "Invalid file stream");
31 KASSERT(f, "Invalid pointer");
32
33 file->read((char*)f, sizeof(float));
34 KASSERT(file->gcount() == sizeof(float), "Expected float");
35
36 return true;
37}
38
39bool ReadFloats(std::ifstream* file, float* f, size_t n) {
40 KASSERT(file, "Invalid file stream");
41 KASSERT(f, "Invalid pointer");
42
43 file->read((char*)f, sizeof(float) * n);
44 KASSERT(((unsigned int)file->gcount()) == sizeof(float) * n,
45 "Expected floats");
46
47 return true;
48}
49
50bool KerasLayerActivation::LoadLayer(std::ifstream* file) {
51 KASSERT(file, "Invalid file stream");
52
53 unsigned int activation = 0;
54 KASSERT(ReadUnsignedInt(file, &activation),
55 "Failed to read activation type");
56
57 switch (activation) {
58 case kLinear:
59 activation_type_ = kLinear;
60 break;
61 case kRelu:
62 activation_type_ = kRelu;
63 break;
64 case kSoftPlus:
65 activation_type_ = kSoftPlus;
66 break;
67 case kHardSigmoid:
68 activation_type_ = kHardSigmoid;
69 break;
70 case kSigmoid:
71 activation_type_ = kSigmoid;
72 break;
73 case kTanh:
74 activation_type_ = kTanh;
75 break;
76 default:
77#if INT_is_64_ == 1
78 KASSERT(false, "Unsupported activation type %ld", activation);
79#else
80 KASSERT(false, "Unsupported activation type %d", activation);
81#endif
82 }
83
84 return true;
85}
86
88 KASSERT(in, "Invalid input");
89 KASSERT(out, "Invalid output");
90
91 *out = *in;
92
93 switch (activation_type_) {
94 case kLinear:
95 break;
96 case kRelu:
97 for (size_t i = 0; i < out->data_.size(); i++) {
98 if (out->data_[i] < 0.0) {
99 out->data_[i] = 0.0;
100 }
101 }
102 break;
103 case kSoftPlus:
104 for (size_t i = 0; i < out->data_.size(); i++) {
105 out->data_[i] = (float)std::log(1.0 + std::exp(out->data_[i]));
106 }
107 break;
108 case kHardSigmoid:
109 for (size_t i = 0; i < out->data_.size(); i++) {
110 float x = (out->data_[i] * 0.2f) + 0.5f;
111
112 if (x <= 0) {
113 out->data_[i] = 0.0f;
114 } else if (x >= 1) {
115 out->data_[i] = 1.0f;
116 } else {
117 out->data_[i] = x;
118 }
119 }
120 break;
121 case kSigmoid:
122 for (size_t i = 0; i < out->data_.size(); i++) {
123 float& x = out->data_[i];
124
125 if (x >= 0) {
126 out->data_[i] = 1.0f / (1.0f + std::exp(-x));
127 } else {
128 float z = std::exp(x);
129 out->data_[i] = z / (1.0f + z);
130 }
131 }
132 break;
133 case kTanh:
134 for (size_t i = 0; i < out->data_.size(); i++) {
135 out->data_[i] = std::tanh(out->data_[i]);
136 }
137 break;
138 default:
139 break;
140 }
141
142 return true;
143}
144
145bool KerasLayerDense::LoadLayer(std::ifstream* file) {
146 KASSERT(file, "Invalid file stream");
147
148 unsigned int weights_rows = 0;
149 KASSERT(ReadUnsignedInt(file, &weights_rows), "Expected weight rows");
150 KASSERT(weights_rows > 0, "Invalid weights # rows");
151
152 unsigned int weights_cols = 0;
153 KASSERT(ReadUnsignedInt(file, &weights_cols), "Expected weight cols");
154 KASSERT(weights_cols > 0, "Invalid weights shape");
155
156 unsigned int biases_shape = 0;
157 KASSERT(ReadUnsignedInt(file, &biases_shape), "Expected biases shape");
158 KASSERT(biases_shape > 0, "Invalid biases shape");
159
160 weights_.Resize(weights_rows, weights_cols);
161 KASSERT(
162 ReadFloats(file, weights_.data_.data(), weights_rows * weights_cols),
163 "Expected weights");
164
165 biases_.Resize(biases_shape);
166 KASSERT(ReadFloats(file, biases_.data_.data(), biases_shape),
167 "Expected biases");
168
169 KASSERT(activation_.LoadLayer(file), "Failed to load activation");
170
171 return true;
172}
173
175 KASSERT(in, "Invalid input");
176 KASSERT(out, "Invalid output");
177 KASSERT(in->dims_.size() <= 2, "Invalid input dimensions");
178
179 if (in->dims_.size() == 2) {
180#if INT_is_64_ == 1
181 KASSERT(in->dims_[1] == weights_.dims_[0], "Dimension mismatch %ld %ld",
182 in->dims_[1], weights_.dims_[0]);
183#else
184 KASSERT(in->dims_[1] == weights_.dims_[0], "Dimension mismatch %d %d",
185 in->dims_[1], weights_.dims_[0]);
186#endif
187 }
188
189 Tensor tmp(weights_.dims_[1]);
190
191 for (int i = 0; i < weights_.dims_[0]; i++) {
192 for (int j = 0; j < weights_.dims_[1]; j++) {
193 tmp(j) += (*in)(i)*weights_(i, j);
194 }
195 }
196
197 for (int i = 0; i < biases_.dims_[0]; i++) {
198 tmp(i) += biases_(i);
199 }
200
201 KASSERT(activation_.Apply(&tmp, out), "Failed to apply activation");
202
203 return true;
204}
205
206bool KerasLayerConvolution2d::LoadLayer(std::ifstream* file) {
207 KASSERT(file, "Invalid file stream");
208
209 unsigned int weights_i = 0;
210 KASSERT(ReadUnsignedInt(file, &weights_i), "Expected weights_i");
211 KASSERT(weights_i > 0, "Invalid weights # i");
212
213 unsigned int weights_j = 0;
214 KASSERT(ReadUnsignedInt(file, &weights_j), "Expected weights_j");
215 KASSERT(weights_j > 0, "Invalid weights # j");
216
217 unsigned int weights_k = 0;
218 KASSERT(ReadUnsignedInt(file, &weights_k), "Expected weights_k");
219 KASSERT(weights_k > 0, "Invalid weights # k");
220
221 unsigned int weights_l = 0;
222 KASSERT(ReadUnsignedInt(file, &weights_l), "Expected weights_l");
223 KASSERT(weights_l > 0, "Invalid weights # l");
224
225 unsigned int biases_shape = 0;
226 KASSERT(ReadUnsignedInt(file, &biases_shape), "Expected biases shape");
227 KASSERT(biases_shape > 0, "Invalid biases shape");
228
229 weights_.Resize(weights_i, weights_j, weights_k, weights_l);
230 KASSERT(ReadFloats(file, weights_.data_.data(),
231 weights_i * weights_j * weights_k * weights_l),
232 "Expected weights");
233
234 biases_.Resize(biases_shape);
235 KASSERT(ReadFloats(file, biases_.data_.data(), biases_shape),
236 "Expected biases");
237
238 KASSERT(activation_.LoadLayer(file), "Failed to load activation");
239
240 return true;
241}
242
244 KASSERT(in, "Invalid input");
245 KASSERT(out, "Invalid output");
246
247 KASSERT(in->dims_[0] == weights_.dims_[1],
248 "Input 'depth' doesn't match kernel 'depth'");
249
250 int st_nj = (weights_.dims_[2] - 1) / 2;
251 int st_pj = (weights_.dims_[2]) / 2;
252 int st_nk = (weights_.dims_[3] - 1) / 2;
253 int st_pk = (weights_.dims_[3]) / 2;
254
255 Tensor tmp(weights_.dims_[0], in->dims_[1] - st_nj - st_pj,
256 in->dims_[2] - st_nk - st_pk);
257
258 // Iterate over each kernel.
259 for (int i = 0; i < weights_.dims_[0]; i++) {
260 // Iterate over each 'depth'.
261 for (int j = 0; j < weights_.dims_[1]; j++) {
262 // 2D convolution in x and y (k and l in Tensor dimensions).
263 for (int tj = st_nj; tj < in->dims_[1] - st_pj; tj++) {
264 for (int tk = st_nk; tk < in->dims_[2] - st_pk; tk++) {
265 // Iterate over kernel.
266 for (int k = 0; k < weights_.dims_[2]; k++) {
267 for (int l = 0; l < weights_.dims_[3]; l++) {
268 const float weight = weights_(i, j, k, l);
269 const float value =
270 (*in)(j, tj - st_nj + k, tk - st_nk + l);
271
272 tmp(i, tj - st_nj, tk - st_nk) += weight * value;
273 }
274 }
275 }
276 }
277 }
278
279 // Apply kernel bias to all points in output.
280 for (int j = 0; j < tmp.dims_[1]; j++) {
281 for (int k = 0; k < tmp.dims_[2]; k++) {
282 tmp(i, j, k) += biases_(i);
283 }
284 }
285 }
286
287 KASSERT(activation_.Apply(&tmp, out), "Failed to apply activation");
288
289 return true;
290}
291
292bool KerasLayerFlatten::LoadLayer(std::ifstream* file) {
293 KASSERT(file, "Invalid file stream");
294 return true;
295}
296
298 KASSERT(in, "Invalid input");
299 KASSERT(out, "Invalid output");
300
301 *out = *in;
302 out->Flatten();
303
304 return true;
305}
306
307bool KerasLayerElu::LoadLayer(std::ifstream* file) {
308 KASSERT(file, "Invalid file stream");
309
310 KASSERT(ReadFloat(file, &alpha_), "Failed to read alpha");
311
312 return true;
313}
314
316 KASSERT(in, "Invalid input");
317 KASSERT(out, "Invalid output");
318
319 *out = *in;
320
321 for (size_t i = 0; i < out->data_.size(); i++) {
322 if (out->data_[i] < 0.0) {
323 out->data_[i] = alpha_ * ((float)exp(out->data_[i]) - 1.0f);
324 }
325 }
326
327 return true;
328}
329
330bool KerasLayerMaxPooling2d::LoadLayer(std::ifstream* file) {
331 KASSERT(file, "Invalid file stream");
332
333 KASSERT(ReadUnsignedInt(file, &pool_size_j_), "Expected pool size j");
334 KASSERT(ReadUnsignedInt(file, &pool_size_k_), "Expected pool size k");
335
336 return true;
337}
338
340 KASSERT(in, "Invalid input");
341 KASSERT(out, "Invalid output");
342
343 KASSERT(in->dims_.size() == 3, "Input must have 3 dimensions");
344
345 Tensor tmp(in->dims_[0], in->dims_[1] / pool_size_j_,
346 in->dims_[2] / pool_size_k_);
347
348 for (int i = 0; i < tmp.dims_[0]; i++) {
349 for (int j = 0; j < tmp.dims_[1]; j++) {
350 const int tj = j * pool_size_j_;
351
352 for (int k = 0; k < tmp.dims_[2]; k++) {
353 const int tk = k * pool_size_k_;
354
355 // Find maximum value over patch starting at tj, tk.
356 float max_val = -std::numeric_limits<float>::infinity();
357
358 for (unsigned int pj = 0; pj < pool_size_j_; pj++) {
359 for (unsigned int pk = 0; pk < pool_size_k_; pk++) {
360 const float pool_val = (*in)(i, tj + pj, tk + pk);
361 if (pool_val > max_val) {
362 max_val = pool_val;
363 }
364 }
365 }
366
367 tmp(i, j, k) = max_val;
368 }
369 }
370 }
371
372 *out = tmp;
373
374 return true;
375}
376
377bool KerasLayerLSTM::LoadLayer(std::ifstream* file) {
378 KASSERT(file, "Invalid file stream");
379
380 unsigned int wi_rows = 0;
381 KASSERT(ReadUnsignedInt(file, &wi_rows), "Expected Wi rows");
382 KASSERT(wi_rows > 0, "Invalid Wi # rows");
383
384 unsigned int wi_cols = 0;
385 KASSERT(ReadUnsignedInt(file, &wi_cols), "Expected Wi cols");
386 KASSERT(wi_cols > 0, "Invalid Wi shape");
387
388 unsigned int ui_rows = 0;
389 KASSERT(ReadUnsignedInt(file, &ui_rows), "Expected Ui rows");
390 KASSERT(ui_rows > 0, "Invalid Ui # rows");
391
392 unsigned int ui_cols = 0;
393 KASSERT(ReadUnsignedInt(file, &ui_cols), "Expected Ui cols");
394 KASSERT(ui_cols > 0, "Invalid Ui shape");
395
396 unsigned int bi_shape = 0;
397 KASSERT(ReadUnsignedInt(file, &bi_shape), "Expected bi shape");
398 KASSERT(bi_shape > 0, "Invalid bi shape");
399
400 unsigned int wf_rows = 0;
401 KASSERT(ReadUnsignedInt(file, &wf_rows), "Expected Wf rows");
402 KASSERT(wf_rows > 0, "Invalid Wf # rows");
403
404 unsigned int wf_cols = 0;
405 KASSERT(ReadUnsignedInt(file, &wf_cols), "Expected Wf cols");
406 KASSERT(wf_cols > 0, "Invalid Wf shape");
407
408 unsigned int uf_rows = 0;
409 KASSERT(ReadUnsignedInt(file, &uf_rows), "Expected Uf rows");
410 KASSERT(uf_rows > 0, "Invalid Uf # rows");
411
412 unsigned int uf_cols = 0;
413 KASSERT(ReadUnsignedInt(file, &uf_cols), "Expected Uf cols");
414 KASSERT(uf_cols > 0, "Invalid Uf shape");
415
416 unsigned int bf_shape = 0;
417 KASSERT(ReadUnsignedInt(file, &bf_shape), "Expected bf shape");
418 KASSERT(bf_shape > 0, "Invalid bf shape");
419
420 unsigned int wc_rows = 0;
421 KASSERT(ReadUnsignedInt(file, &wc_rows), "Expected Wc rows");
422 KASSERT(wc_rows > 0, "Invalid Wc # rows");
423
424 unsigned int wc_cols = 0;
425 KASSERT(ReadUnsignedInt(file, &wc_cols), "Expected Wc cols");
426 KASSERT(wc_cols > 0, "Invalid Wc shape");
427
428 unsigned int uc_rows = 0;
429 KASSERT(ReadUnsignedInt(file, &uc_rows), "Expected Uc rows");
430 KASSERT(uc_rows > 0, "Invalid Uc # rows");
431
432 unsigned int uc_cols = 0;
433 KASSERT(ReadUnsignedInt(file, &uc_cols), "Expected Uc cols");
434 KASSERT(uc_cols > 0, "Invalid Uc shape");
435
436 unsigned int bc_shape = 0;
437 KASSERT(ReadUnsignedInt(file, &bc_shape), "Expected bc shape");
438 KASSERT(bc_shape > 0, "Invalid bc shape");
439
440 unsigned int wo_rows = 0;
441 KASSERT(ReadUnsignedInt(file, &wo_rows), "Expected Wo rows");
442 KASSERT(wo_rows > 0, "Invalid Wo # rows");
443
444 unsigned int wo_cols = 0;
445 KASSERT(ReadUnsignedInt(file, &wo_cols), "Expected Wo cols");
446 KASSERT(wo_cols > 0, "Invalid Wo shape");
447
448 unsigned int uo_rows = 0;
449 KASSERT(ReadUnsignedInt(file, &uo_rows), "Expected Uo rows");
450 KASSERT(uo_rows > 0, "Invalid Uo # rows");
451
452 unsigned int uo_cols = 0;
453 KASSERT(ReadUnsignedInt(file, &uo_cols), "Expected Uo cols");
454 KASSERT(uo_cols > 0, "Invalid Uo shape");
455
456 unsigned int bo_shape = 0;
457 KASSERT(ReadUnsignedInt(file, &bo_shape), "Expected bo shape");
458 KASSERT(bo_shape > 0, "Invalid bo shape");
459
460 // Load Input Weights and Biases
461 Wi_.Resize(wi_rows, wi_cols);
462 KASSERT(ReadFloats(file, Wi_.data_.data(), wi_rows * wi_cols),
463 "Expected Wi weights");
464
465 Ui_.Resize(ui_rows, ui_cols);
466 KASSERT(ReadFloats(file, Ui_.data_.data(), ui_rows * ui_cols),
467 "Expected Ui weights");
468
469 bi_.Resize(1, bi_shape);
470 KASSERT(ReadFloats(file, bi_.data_.data(), bi_shape), "Expected bi biases");
471
472 // Load Forget Weights and Biases
473 Wf_.Resize(wf_rows, wf_cols);
474 KASSERT(ReadFloats(file, Wf_.data_.data(), wf_rows * wf_cols),
475 "Expected Wf weights");
476
477 Uf_.Resize(uf_rows, uf_cols);
478 KASSERT(ReadFloats(file, Uf_.data_.data(), uf_rows * uf_cols),
479 "Expected Uf weights");
480
481 bf_.Resize(1, bf_shape);
482 KASSERT(ReadFloats(file, bf_.data_.data(), bf_shape), "Expected bf biases");
483
484 // Load State Weights and Biases
485 Wc_.Resize(wc_rows, wc_cols);
486 KASSERT(ReadFloats(file, Wc_.data_.data(), wc_rows * wc_cols),
487 "Expected Wc weights");
488
489 Uc_.Resize(uc_rows, uc_cols);
490 KASSERT(ReadFloats(file, Uc_.data_.data(), uc_rows * uc_cols),
491 "Expected Uc weights");
492
493 bc_.Resize(1, bc_shape);
494 KASSERT(ReadFloats(file, bc_.data_.data(), bc_shape), "Expected bc biases");
495
496 // Load Output Weights and Biases
497 Wo_.Resize(wo_rows, wo_cols);
498 KASSERT(ReadFloats(file, Wo_.data_.data(), wo_rows * wo_cols),
499 "Expected Wo weights");
500
501 Uo_.Resize(uo_rows, uo_cols);
502 KASSERT(ReadFloats(file, Uo_.data_.data(), uo_rows * uo_cols),
503 "Expected Uo weights");
504
505 bo_.Resize(1, bo_shape);
506 KASSERT(ReadFloats(file, bo_.data_.data(), bo_shape), "Expected bo biases");
507
508 KASSERT(innerActivation_.LoadLayer(file),
509 "Failed to load inner activation");
510 KASSERT(activation_.LoadLayer(file), "Failed to load activation");
511
512 unsigned int return_sequences = 0;
513 KASSERT(ReadUnsignedInt(file, &return_sequences),
514 "Expected return_sequences param");
515 return_sequences_ = (bool)return_sequences;
516
517 return true;
518}
519
521 // Assume bo always keeps the output shape and we will always receive one
522 // single sample.
523 int outputDim = bo_.dims_[1];
524 Tensor ht_1 = Tensor(1, outputDim);
525 Tensor ct_1 = Tensor(1, outputDim);
526
527 ht_1.Fill(0.0f);
528 ct_1.Fill(0.0f);
529
530 int steps = in->dims_[0];
531
532 Tensor outputs, lastOutput;
533
534 if (return_sequences_) {
535 outputs.dims_ = {steps, outputDim};
536 outputs.data_.reserve(steps * outputDim);
537 }
538
539 for (int s = 0; s < steps; s++) {
540 Tensor x = in->Select(s);
541
542 KASSERT(Step(&x, &lastOutput, &ht_1, &ct_1), "Failed to execute step");
543
544 if (return_sequences_) {
545 outputs.data_.insert(outputs.data_.end(), lastOutput.data_.begin(),
546 lastOutput.data_.end());
547 }
548 }
549
550 if (return_sequences_) {
551 *out = outputs;
552 } else {
553 *out = lastOutput;
554 }
555
556 return true;
557}
558
559bool KerasLayerEmbedding::LoadLayer(std::ifstream* file) {
560 KASSERT(file, "Invalid file stream");
561
562 unsigned int weights_rows = 0;
563 KASSERT(ReadUnsignedInt(file, &weights_rows), "Expected weight rows");
564 KASSERT(weights_rows > 0, "Invalid weights # rows");
565
566 unsigned int weights_cols = 0;
567 KASSERT(ReadUnsignedInt(file, &weights_cols), "Expected weight cols");
568 KASSERT(weights_cols > 0, "Invalid weights shape");
569
570 weights_.Resize(weights_rows, weights_cols);
571 KASSERT(
572 ReadFloats(file, weights_.data_.data(), weights_rows * weights_cols),
573 "Expected weights");
574
575 return true;
576}
577
579 int output_rows = in->dims_[1];
580 int output_cols = weights_.dims_[1];
581 out->dims_ = {output_rows, output_cols};
582 out->data_.reserve(output_rows * output_cols);
583
584 for (const auto& i: in->data_) {
585 std::vector<float>::const_iterator first = this->weights_.data_.begin() + (std::lrint(i) * output_cols);
586 std::vector<float>::const_iterator last = this->weights_.data_.begin() + (std::lrint(i) + 1) * output_cols;
587
588 out->data_.insert(out->data_.end(), first, last);
589 }
590
591 return true;
592}
593
594bool KerasLayerLSTM::Step(Tensor* x, Tensor* out, Tensor* ht_1, Tensor* ct_1) {
595 Tensor xi = x->Dot(Wi_) + bi_;
596 Tensor xf = x->Dot(Wf_) + bf_;
597 Tensor xc = x->Dot(Wc_) + bc_;
598 Tensor xo = x->Dot(Wo_) + bo_;
599
600 Tensor i_ = xi + ht_1->Dot(Ui_);
601 Tensor f_ = xf + ht_1->Dot(Uf_);
602 Tensor c_ = xc + ht_1->Dot(Uc_);
603 Tensor o_ = xo + ht_1->Dot(Uo_);
604
605 Tensor i, f, cc, o;
606
607 KASSERT(innerActivation_.Apply(&i_, &i),
608 "Failed to apply inner activation on i");
609 KASSERT(innerActivation_.Apply(&f_, &f),
610 "Failed to apply inner activation on f");
611 KASSERT(activation_.Apply(&c_, &cc), "Failed to apply activation on c_");
612 KASSERT(innerActivation_.Apply(&o_, &o),
613 "Failed to apply inner activation on o");
614
615 *ct_1 = f.Multiply(*ct_1) + i.Multiply(cc);
616
617 KASSERT(activation_.Apply(ct_1, &cc), "Failed to apply activation on c");
618 *out = *ht_1 = o.Multiply(cc);
619
620 return true;
621}
622
623bool KerasModel::LoadModel(const std::string& filename) {
624 std::ifstream file(filename.c_str(), std::ios::binary);
625 if(!file.is_open()){
626 std::cerr << "fichier: " << filename << " inexistant!" << std::endl;
628 }
629 //KASSERT(file.is_open(), "Unable to open file %s", filename.c_str());
630
631 unsigned int num_layers = 0;
632 KASSERT(ReadUnsignedInt(&file, &num_layers), "Expected number of layers");
633
634 for (unsigned int i = 0; i < num_layers; i++) {
635 unsigned int layer_type = 0;
636 KASSERT(ReadUnsignedInt(&file, &layer_type), "Expected layer type");
637
638 KerasLayer* layer = nullptr;
639
640 switch (layer_type) {
641 case kDense:
642 layer = new KerasLayerDense();
643 break;
644 case kConvolution2d:
645 layer = new KerasLayerConvolution2d();
646 break;
647 case kFlatten:
648 layer = new KerasLayerFlatten();
649 break;
650 case kElu:
651 layer = new KerasLayerElu();
652 break;
653 case kActivation:
654 layer = new KerasLayerActivation();
655 break;
656 case kMaxPooling2D:
657 layer = new KerasLayerMaxPooling2d();
658 break;
659 case kLSTM:
660 layer = new KerasLayerLSTM();
661 break;
662 case kEmbedding:
663 layer = new KerasLayerEmbedding();
664 break;
665 default:
666 break;
667 }
668#if INT_is_64_ == 1
669 KASSERT(layer, "Unknown layer type %ld", layer_type);
670#else
671 KASSERT(layer, "Unknown layer type %d", layer_type);
672#endif
673 bool result = layer->LoadLayer(&file);
674 if (!result) {
675#if INT_is_64_ == 1
676 printf("Failed to load layer %ld", i);
677#else
678 printf("Failed to load layer %d", i);
679#endif
680 delete layer;
681 return false;
682 }
683
684 layers_.push_back(layer);
685 }
686
687 return true;
688}
689
691 Tensor temp_in, temp_out;
692
693 for (unsigned int i = 0; i < layers_.size(); i++) {
694 if (i == 0) {
695 temp_in = *in;
696 }
697
698#if INT_is_64_ == 1
699 KASSERT(layers_[i]->Apply(&temp_in, &temp_out),
700 "Failed to apply layer %ld", i);
701#else
702 KASSERT(layers_[i]->Apply(&temp_in, &temp_out),
703 "Failed to apply layer %d", i);
704#endif
705 temp_in = temp_out;
706 }
707
708 *out = temp_out;
709
710 return true;
711}
bool LoadLayer(std::ifstream *file) override
bool Apply(Tensor *in, Tensor *out) override
bool Apply(Tensor *in, Tensor *out) override
bool LoadLayer(std::ifstream *file) override
bool LoadLayer(std::ifstream *file) override
bool Apply(Tensor *in, Tensor *out) override
bool Apply(Tensor *in, Tensor *out) override
bool LoadLayer(std::ifstream *file) override
bool Apply(Tensor *in, Tensor *out) override
bool LoadLayer(std::ifstream *file) override
bool Apply(Tensor *in, Tensor *out) override
bool LoadLayer(std::ifstream *file) override
bool Apply(Tensor *in, Tensor *out) override
bool LoadLayer(std::ifstream *file) override
bool LoadLayer(std::ifstream *file) override
bool Apply(Tensor *in, Tensor *out) override
virtual bool LoadLayer(std::ifstream *file)=0
virtual bool LoadModel(const std::string &filename)
virtual bool Apply(Tensor *in, Tensor *out)
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
std::vector< float > data_
void Flatten()
Definition keras_model.h:78
std::vector< int > dims_
Tensor Select(int row) const
Tensor Multiply(const Tensor &other)
Tensor Dot(const Tensor &other)
void Fill(float value)