Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com>
4 : *
5 : * @file loss_layer.cpp
6 : * @date 12 June 2020
7 : * @brief This is Loss Layer Class for Neural Network
8 : * @see https://github.com/nnstreamer/nntrainer
9 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : *
12 : */
13 :
14 : #include <layer_context.h>
15 : #include <loss_layer.h>
16 :
17 : namespace nntrainer {
18 648 : void LossLayer::finalize(InitLayerContext &context) {
19 648 : std::vector<TensorDim> input_dim = context.getInputDimensions();
20 648 : std::vector<TensorDim> output_dim = input_dim;
21 :
22 1296 : for (auto &d : output_dim)
23 1296 : d.setDataType(
24 : str_converter<enum_class_prop_tag,
25 : nntrainer::TensorDataTypeInfo>::from_string("FP32"));
26 :
27 648 : context.setOutputDimensions(output_dim);
28 :
29 648 : is_inplace = true;
30 648 : if (context.getActivationDataType() != ml::train::TensorDim::DataType::FP32)
31 0 : is_inplace = false;
32 648 : }
33 :
34 6141 : void LossLayer::updateLoss(RunLayerContext &context, const Tensor &l) {
35 : float loss_sum = 0.0f;
36 : const float *data = l.getData();
37 :
38 15856 : for (unsigned int i = 0; i < l.batch(); i++) {
39 9715 : loss_sum += data[i];
40 : }
41 6141 : context.setLoss(loss_sum / (float)l.batch());
42 6141 : }
43 :
44 599 : void LossLayer::applyLossScale(RunLayerContext &context, Tensor &ret_deriv) {
45 :
46 599 : float loss_scale = context.getLossScale();
47 599 : if (loss_scale != 1.0)
48 0 : ret_deriv.multiply_i(loss_scale);
49 599 : }
50 :
51 : /**
52 : * @copydoc Layer::setProperty(const std::vector<std::string> &values)
53 : */
54 3409 : void LossLayer::setProperty(const std::vector<std::string> &values) {
55 3427 : NNTR_THROW_IF(!values.empty(), std::invalid_argument)
56 : << "[Layer] Unknown Layer Properties count = " << values.size();
57 3391 : }
58 :
59 : } /* namespace nntrainer */
|