LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
l2.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_OBJECTIVE_FUNCTIONS_WEIGHT_REGULARIZATION_L2_WEIGHT_REGULARIZATION_HPP_INCLUDED
28 #define LBANN_OBJECTIVE_FUNCTIONS_WEIGHT_REGULARIZATION_L2_WEIGHT_REGULARIZATION_HPP_INCLUDED
29 
32 #ifdef LBANN_HAS_GPU
34 #endif // LBANN_HAS_GPU
35 
36 namespace lbann {
37 
38 template <typename>
40 template <typename>
42 
51 {
52 public:
53  using AccumulateDataType = DataType;
54 
56 
58 
59  template <El::Device D>
60  using DMatType = El::Matrix<AccumulateDataType, D>;
61 
63 
64 public:
68  l2_weight_regularization(EvalType scale_factor = 1);
69  l2_weight_regularization* copy() const override
70  {
71  return new l2_weight_regularization(*this);
72  }
73 
75  template <typename ArchiveT>
76  void serialize(ArchiveT& ar);
77 
78  std::string name() const override { return "L2 weight regularization"; }
79  void setup(model& m) override;
80  void start_evaluation() override;
81  EvalType finish_evaluation() override;
82 
90  void differentiate() override{};
91 
96  void compute_weight_regularization() override;
97 
98 private:
100  void write_specific_proto(lbann_data::ObjectiveFunction& proto) const final;
101 
103  std::map<El::Device, CPUMatType> m_contributions;
104 
107 #ifdef LBANN_HAS_GPU
108 
109  gpu_lib::event_wrapper m_copy_event;
110 #endif // LBANN_HAS_GPU
111 
118  template <El::Device Device>
119  static void accumulate_contribution(const DMatType<Device>& vals,
120  DMatType<Device>& contribution);
121 };
122 
123 } // namespace lbann
124 
125 #endif // LBANN_OBJECTIVE_FUNCTIONS_WEIGHT_REGULARIZATION_L2_WEIGHT_REGULARIZATION_HPP_INCLUDED
DMatType< El::Device::CPU > CPUMatType
Definition: l2.hpp:62
void serialize(ArchiveT &ar)
El::Matrix< AccumulateDataType, D > DMatType
Definition: l2.hpp:60
l2_weight_regularization(EvalType scale_factor=1)
void compute_weight_regularization() override
void write_specific_proto(lbann_data::ObjectiveFunction &proto) const final
Add objective function data to prototext.
void differentiate() override
Definition: l2.hpp:90
l2_weight_regularization * copy() const override
Definition: l2.hpp:69
Apply L2 regularization to a set of weights.
Definition: l2.hpp:50
EvalType finish_evaluation() override
Abstract base class for neural network models.
Definition: model.hpp:83
Al::request m_allreduce_req
Definition: l2.hpp:106
std::map< El::Device, CPUMatType > m_contributions
Definition: l2.hpp:103
std::string name() const override
Definition: l2.hpp:78
static void accumulate_contribution(const DMatType< Device > &vals, DMatType< Device > &contribution)
void setup(model &m) override
double EvalType
Definition: base.hpp:189