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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#pragma once
#include <memory>
#include <vector>
#include "ngraph/runtime/backend.hpp"
#include "ngraph/type/element_type.hpp"
namespace ngraph
{
namespace autodiff
{
/// \brief numeric approximation of the derivative
/// \param f A function
/// \param args Values for the arguments (the independent variables)
/// \param delta increment for the variables
/// \param indep_params parameters with respect to which to compute derivatives
/// \returns vector of dy/dvar, where each dy/dvar's shape is concat(y.shape(), var.shape())
template <typename T>
std::vector<std::shared_ptr<runtime::Tensor>>
numeric_derivative(runtime::Backend* backend,
const std::shared_ptr<Function>& f,
const std::vector<std::shared_ptr<runtime::Tensor>>& args,
T delta,
const std::vector<std::shared_ptr<op::Parameter>>& indep_params)
{
Shape y_shape = f->get_output_shape(0);
auto params = f->get_parameters();
// Results for each derivative, shape Y|X_i
std::vector<std::shared_ptr<runtime::Tensor>> results;
for (auto param : indep_params)
{
Shape s = y_shape;
auto param_shape = param->get_shape();
s.insert(s.end(), param_shape.begin(), param_shape.end());
results.push_back(backend->create_tensor<T>(s));
}
// ref_y is the function evaluated at the args
auto ref_y = backend->create_tensor<T>(y_shape);
auto f_handle = backend->compile(f);
f_handle->call_with_validate(
std::vector<std::shared_ptr<ngraph::runtime::Tensor>>{ref_y}, args);
auto ref_vec = read_vector<T>(ref_y);
// inc_y will hold f(x+dx) values
auto inc_y = backend->create_tensor<T>(y_shape);
// Assuming vars, y, and results are row-major
T inv_delta = 1 / delta;
size_t pos = 0;
for (size_t i = 0; i < args.size(); ++i)
{
if (std::find(indep_params.begin(), indep_params.end(), params[i]) !=
indep_params.end())
{
auto arg = args[i];
auto res = read_vector<T>(results[pos]);
auto vec = read_vector<T>(arg);
for (size_t j = 0; j < vec.size(); j++)
{
auto old_val = vec[j];
vec[j] += delta;
write_vector(arg, vec);
f_handle->call_with_validate({inc_y}, args);
auto inc_vec = read_vector<T>(inc_y);
vec[j] = old_val;
write_vector(arg, vec);
size_t res_k = j;
for (size_t k = 0; k < inc_vec.size(); k++)
{
auto y1 = inc_vec[k];
auto y0 = ref_vec[k];
res[res_k] = inv_delta * (y1 - y0);
res_k += vec.size();
}
}
write_vector(results[pos], res);
pos++;
}
}
return results;
}
}
}