LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
bernoulli.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_LAYER_BERNOULLI_HPP_INCLUDED
28 #define LBANN_LAYER_BERNOULLI_HPP_INCLUDED
29 
31 #include "lbann/models/model.hpp"
32 #include "lbann/utils/random.hpp"
33 
34 namespace lbann {
35 
41 template <typename TensorDataType,
43  El::Device Dev = El::Device::CPU>
44 class bernoulli_layer : public data_type_layer<TensorDataType>
45 {
46 public:
47  using ProbabilityType = double;
48 
49 public:
51  std::vector<int> dims,
52  ProbabilityType prob = 0.5)
53  : data_type_layer<TensorDataType>(comm), m_prob(prob)
54  {
55  this->set_output_dims(dims);
57  }
58  bernoulli_layer* copy() const override { return new bernoulli_layer(*this); }
59 
61 
63  template <typename ArchiveT>
64  void serialize(ArchiveT& ar);
65 
67 
68  std::string get_type() const override { return "Bernoulli"; }
69  data_layout get_data_layout() const override { return T_layout; }
70  El::Device get_device_allocation() const override { return Dev; }
71  bool can_run_inplace() const override { return false; }
72  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
73 
74  description get_description() const override
75  {
77  desc.add("Probability", m_prob);
78  return desc;
79  }
80 
81 protected:
83  void write_specific_proto(lbann_data::Layer& proto) const final;
84 
85  friend class cereal::access;
86  bernoulli_layer() : bernoulli_layer(nullptr, {1}, 0.5) {}
87 
88  void fp_compute() override;
89 
90 private:
93 };
94 
95 #ifndef LBANN_BERNOULLI_LAYER_INSTANTIATE
96 
97 #define PROTO_DEVICE(T, Device) \
98  extern template class bernoulli_layer<T, \
99  data_layout::DATA_PARALLEL, \
100  Device>; \
101  extern template class bernoulli_layer<T, data_layout::MODEL_PARALLEL, Device>
102 
104 #undef PROTO_DEVICE
105 
106 #endif // LBANN_BERNOULLI_LAYER_INSTANTIATE
107 
108 } // namespace lbann
109 
110 #endif // LBANN_LAYER_BERNOULLI_HPP_INCLUDED
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: bernoulli.hpp:72
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
Definition: bernoulli.hpp:69
description get_description() const override
Human-readable description.
Definition: bernoulli.hpp:74
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual description get_description() const
Human-readable description.
constexpr El::Device Device
void write_specific_proto(lbann_data::Layer &proto) const final
bernoulli_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: bernoulli.hpp:58
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
Random tensor with Bernoulli distribution.
Definition: bernoulli.hpp:44
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
Definition: bernoulli.hpp:70
friend class cereal::access
Definition: bernoulli.hpp:85
void serialize(ArchiveT &ar)
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: bernoulli.hpp:71
bernoulli_layer(lbann_comm *comm, std::vector< int > dims, ProbabilityType prob=0.5)
Definition: bernoulli.hpp:50
ProbabilityType m_prob
Definition: bernoulli.hpp:92
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
void fp_compute() override
Apply layer operation. Called by the &#39;forward_prop&#39; function. Given the input tensors, the output tensors are populated with computed values.
int m_expected_num_parent_layers
Definition: layer.hpp:838
std::string get_type() const override
Get the layer type&#39;s name.
Definition: bernoulli.hpp:68