-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorFlow.cpp
More file actions
79 lines (62 loc) · 2.48 KB
/
TensorFlow.cpp
File metadata and controls
79 lines (62 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "TensorFlow.h"
TensorFlow::TensorFlow()
{
qDebug() << "TensorFlow::TensorFlow - Hello from TensorFlow C library version" << TF_Version();
}
void TensorFlow::DeallocateTensor(void *data, std::size_t s, void * arg) {
std::free(data);
qDebug() << "TensorFlow::DeallocateTensor - size" << s ;
qDebug() << "TensorFlow::DeallocateTensor - arg" << *static_cast<QString*>(arg);
}
int TensorFlow::createTensor()
{
const std::vector<std::int64_t> dims = {1, 5, 12};
const auto data_size = std::accumulate(dims.begin(), dims.end(), sizeof(float), std::multiplies<std::int64_t>{});
auto data = static_cast<float*>(std::malloc(data_size));
std::copy(vals.begin(), vals.end(), data); // init input_vals.
QString dealArg = "This is an argument for the deallocation function";
auto tensor = TF_NewTensor(TF_FLOAT,
dims.data(), static_cast<int>(dims.size()),
data, data_size,
DeallocateTensor, &dealArg);
// The lambda will be executed right before your function returns
auto cleanup = qScopeGuard([=]{
qDebug() << "TensorFlow::createTensor - Calling TF_DeleteTensor";
TF_DeleteTensor(tensor);
});
if (tensor == nullptr) {
qDebug() << "TensorFlow::createTensor - Wrong creat tensor";
return 1;
}
if (TF_TensorType(tensor) != TF_FLOAT) {
qDebug() << "TensorFlow::createTensor - Wrong tensor type";
return 2;
}
if (TF_NumDims(tensor) != static_cast<int>(dims.size())) {
qDebug() << "TensorFlow::createTensor - Wrong number of dimensions";
return 3;
}
for (std::size_t i = 0; i < dims.size(); ++i) {
if (TF_Dim(tensor, static_cast<int>(i)) != dims[i]) {
qDebug() << "TensorFlow::createTensor - Wrong dimension size for dim: " << i;
return 4;
}
}
if (TF_TensorByteSize(tensor) != data_size) {
qDebug() << "TensorFlow::createTensor - Wrong tensor byte size";
return 5;
}
auto tensor_data = static_cast<float*>(TF_TensorData(tensor));
if (tensor_data == nullptr) {
qDebug() << "TensorFlow::createTensor - Wrong data tensor";
return 6;
}
for (std::size_t i = 0; i < vals.size(); ++i) {
if (tensor_data[i] != vals[i]) {
qDebug() << "TensorFlow::createTensor - Element: " << i << " does not match";
return 7;
}
}
qDebug() << "TensorFlow::createTensor - Success creating tensor";
return 0;
}