OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
dense_layer.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// D E N S E L A Y E R C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include "layer.h"
12#include "operators.h"
13
14namespace opennn
15{
16
18class Dense final : public Layer
19{
20public:
21
23 Dense(const Shape& = {},
24 const Shape& = {},
25 const string& = "Tanh",
26 bool = false,
27 const string& = "dense_layer");
28
30 Shape get_output_shape() const override;
31
32 Index get_input_features() const { return input_shape.empty() ? 0 : input_shape.back(); }
33 Index get_sequence_length() const { return (input_shape.rank == 2) ? input_shape[0] : Index(1); }
34
35 const ActivationOp::Function& get_activation_function() const { return activation.function; }
36 ActivationOp::Function get_output_activation() const override { return activation.function; }
37
38 bool get_batch_normalization() const { return batch_norm.active(); }
39 float get_momentum() const { return batch_norm.momentum; }
40
42 vector<TensorSpec> get_forward_specs(Index batch_size) const override;
43
45 void set(const Shape& = {},
46 const Shape& = {},
47 const string& = "Tanh",
48 bool = false,
49 const string& = "dense_layer");
50
52 void set_input_shape(const Shape&) override;
54 void set_output_shape(const Shape&) override;
55 void on_compute_dtype_changed() override { configure_operators(); }
56
58 void set_activation_function(const string&);
60 void set_batch_normalization(bool enable);
61 void set_dropout_rate(float new_dropout_rate) { dropout.set_rate(new_dropout_rate); }
63 void set_momentum(float new_momentum);
64
66 void read_JSON_body(const Json*) override;
67
69 string write_expression(const vector<string>& input_names,
70 const vector<string>& output_names) const override;
71
72private:
73
74 Index output_features = 0;
75
76 CombinationOp combination;
77 ActivationOp activation;
78 BatchNormOp batch_norm;
79 DropoutOp dropout;
80
81 enum Forward {Input, CombinationView, BatchNormMean, BatchNormInverseVariance, ActivationView, Output};
82
83 void configure_operators();
84};
85
86}
87
88// OpenNN: Open Neural Networks Library.
89// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
90// Licensed under the GNU Lesser General Public License v2.1 or later.
void set_dropout_rate(float new_dropout_rate)
Definition dense_layer.h:61
void read_JSON_body(const Json *) override
Subclass hook reading the body section of the layer's JSON node.
void set_activation_function(const string &)
Sets the activation function from its string name (e.g. "Tanh", "Logistic", "Linear").
string write_expression(const vector< string > &input_names, const vector< string > &output_names) const override
Returns a human-readable mathematical expression for this layer (empty by default).
void set_batch_normalization(bool enable)
Enables or disables batch normalization on the layer combination output.
void set_input_shape(const Shape &) override
Sets the input shape; subclasses override to derive dependent dimensions.
void set_momentum(float new_momentum)
Sets the running-statistics momentum used by batch normalization.
float get_momentum() const
Definition dense_layer.h:39
Dense(const Shape &={}, const Shape &={}, const string &="Tanh", bool=false, const string &="dense_layer")
Constructs a dense layer with the given input/output shapes, activation, and batch-norm flag.
void on_compute_dtype_changed() override
Subclass hook invoked when the compute dtype changes; default is no-op.
Definition dense_layer.h:55
Shape get_output_shape() const override
Returns the layer output shape, derived from input shape and configured output features.
bool get_batch_normalization() const
Definition dense_layer.h:38
vector< TensorSpec > get_forward_specs(Index batch_size) const override
Returns the tensor specs of intermediate forward buffers for a given batch size.
void set_output_shape(const Shape &) override
Sets the output shape; subclasses override when the output is user-configurable.
Index get_sequence_length() const
Definition dense_layer.h:33
const ActivationOp::Function & get_activation_function() const
Definition dense_layer.h:35
void set(const Shape &={}, const Shape &={}, const string &="Tanh", bool=false, const string &="dense_layer")
Reconfigures the layer with new shapes, activation, batch-normalization flag and label.
ActivationOp::Function get_output_activation() const override
Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).
Definition dense_layer.h:36
Index get_input_features() const
Definition dense_layer.h:32
Definition json.h:23
Layer()=default
Shape input_shape
Definition layer.h:256
Definition adaptive_moment_estimation.h:14
Element-wise non-linear activation (Identity, Sigmoid, Tanh, ReLU, Softmax).
Definition operators.h:169
Function
Supported activation functions.
Definition operators.h:171
Batch normalization with learnable scale/shift and running statistics for inference.
Definition operators.h:308
Affine combination output = input * weights + bias (the dense matmul building block).
Definition operators.h:232
Inverted dropout: at training time zeros activations with probability rate and rescales survivors.
Definition operators.h:122
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42