ONE - On-device Neural Engine
Loading...
Searching...
No Matches
SGD.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2016 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __NNFW_CKER_TRAIN_OPTIMIZER_SGD_H__
19#define __NNFW_CKER_TRAIN_OPTIMIZER_SGD_H__
20
23
24#include <vector>
25
26namespace nnfw
27{
28namespace cker
29{
30namespace train
31{
32
33inline void GradientDescent(const Shape &output_shape, float *output_data, const Shape &grad_shape,
34 const float *grad_data, float learning_rate)
35{
36 Tensor output_tensor;
37 Tensor grad_tensor;
38 Tensor lr_tensor;
39
40 output_tensor.shape.ReplaceWith(output_shape.DimensionsCount(), output_shape.DimsData());
41 output_tensor.buffer = output_data;
42
43 grad_tensor.shape.ReplaceWith(grad_shape.DimensionsCount(), grad_shape.DimsData());
44 grad_tensor.buffer = const_cast<float *>(grad_data);
45
46 std::vector<float> lr_vec{learning_rate};
47 lr_tensor.buffer = lr_vec.data();
48
49 if (output_shape != grad_shape)
50 throw std::runtime_error(
51 "cker::GradientDescent: output and gradient do not have the same shape");
52
55 device, output_tensor.flat<float>(), lr_tensor.scalar<float>(),
56 static_cast<const Tensor &>(grad_tensor).flat<float>());
57}
58
59} // namespace train
60} // namespace cker
61} // namespace nnfw
62
63#endif // __NNFW_CKER_TRAIN_OPTIMIZER_SGD_H__
int32_t DimensionsCount() const
Definition Shape.h:91
void ReplaceWith(int dimensions_count, const int32_t *dims_data)
Definition Shape.h:130
int32_t * DimsData()
Definition Shape.h:112
const luci_interpreter::RuntimeShape output_shape
const Eigen::ThreadPoolDevice * GetThreadPoolDevice()
void GradientDescent(const Shape &output_shape, float *output_data, const Shape &grad_shape, const float *grad_data, float learning_rate)
Definition SGD.h:33
Eigen::ThreadPoolDevice CPUDevice
Definition topk_v2.h:30
TTypes< T >::ConstScalar scalar() const
Definition Tensor.h:156