-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
43 lines (35 loc) · 1.3 KB
/
Copy pathexample.cpp
File metadata and controls
43 lines (35 loc) · 1.3 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
#include <iostream>
#include <tensorlib/tensorlib.hpp>
using namespace std;
int main() {
Device device = Device::GPU;
// 2d vector
variable x = TensorFactory::create(
vector<float>{1.0f, 2.0f, 3.0f, 10.0f, 11.0f, 12.0f, 20.0f, 21.0f, 22.0f,
30.0f, 31.0f, 32.0f},
vector<size_t>{2, 2, 3}, device, true);
variable y =
TensorFactory::create(vector<float>{1.f, -2.f, 5.f, 3.f, 2.f, 1.f},
vector<size_t>{2, 3, 1}, device, true);
variable z = matmul(x, y);
variable w, l;
{
NoGradScope scope;
w = log(z);
l = sum(w, 1, false);
}
cout << "x: " << x->to_string() << endl;
cout << "y: " << y->to_string() << endl;
cout << "z: " << z->to_string() << endl;
cout << "w: " << w->to_string() << endl;
cout << "l: " << l->to_string() << endl;
variable init_grad_l =
TensorFactory::randn(l->shape(), 0.0f, 1.0f, device, false);
l->backward(init_grad_l);
cout << "l grad: " << l->autograd_meta().grad_->to_string() << endl;
cout << "w grad: " << w->autograd_meta().grad_->to_string() << endl;
cout << "z grad: " << z->autograd_meta().grad_->to_string() << endl;
cout << "y grad: " << y->autograd_meta().grad_->to_string() << endl;
cout << "x grad: " << x->autograd_meta().grad_->to_string() << endl;
return 0;
}