-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumAlongAxes.h
80 lines (68 loc) · 2.67 KB
/
SumAlongAxes.h
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
80
#ifndef LIBDL_SUMALONGAXES_H
#define LIBDL_SUMALONGAXES_H
#include "../Tensor.h"
#include "../Utils.h"
template <typename D,std::int64_t RA,std::int64_t RB>
class SumAlongAxes : public CNode<D, RA - RB> {
public:
SumAlongAxes(
const std::optional<std::shared_ptr<CNode<D, RA>>> &cx,
const std::shared_ptr<Tensor<D, RA - RB>> &result,
const std::array <std::int64_t, RB> &axes,
const std::array<std::int64_t, RA> &oldDimensions)
: CNode<D, RA - RB>(Utils::removeOption<std::shared_ptr<CNodeBase>>({cx}), result),
cx(cx),
axes(axes),
oldDimensions(oldDimensions) {}
/*
* \brief computes the sum along the given axes
*
* \param x a tensor of any shape
* \param axes the axes along which the sum should be computed
*
* \return a new tensor containing the sum
* */
static std::shared_ptr<Tensor<D, RA - RB>> sum(
const std::shared_ptr<Tensor<D, RA>> &x,
std::array <std::int64_t, RB> axes) {
for (auto a : axes)
if (a < 0 || a >= RA)
throw std::invalid_argument("axis index out of range");
std::array<std::int64_t, RA - RB> newShape {};
for (std::int64_t i = 0, j = 0; i < (RA - RB); j++)
if (notIn(j, axes))
newShape[i++] = x->data->dimension(j);
auto result = std::make_shared<Tensor<D, RA - RB>>(x->data->sum(axes), newShape);
if (x->needsGradient() && !CNodeBase::noGrad)
result->setGradFn(std::make_shared<SumAlongAxes<D, RA, RB>>(x->gradFn, result, axes, x->data->dimensions()));
return result;
}
void computeGradients() override {
if (cx.has_value()) {
std::array <std::int64_t, RA> reshape;
std::array <std::int64_t, RA> broadcast;
for (std::int64_t i = 0; i < RA; i++) {
if (notIn(i, axes)) {
reshape[i] = oldDimensions[i];
broadcast[i] = 1;
} else {
reshape[i] = 1;
broadcast[i] = oldDimensions[i];
}
}
cx.value()->addGrad(CNode<D, RA - RB>::grad->reshape(reshape).broadcast(broadcast));
}
CNode<D, RA - RB>::finishComputeGradient();
}
private:
std::optional<std::shared_ptr<CNode<D, RA>>> cx;
std::array <std::int64_t, RB> axes;
std::array<std::int64_t, RA> oldDimensions;
static bool notIn(std::int64_t a, const std::array <std::int64_t, RB> &axes) {
for (auto i : axes)
if (a == i)
return false;
return true;
}
};
#endif //LIBDL_SUMALONGAXES_H