OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::Dense Class Referencefinal

Fully-connected layer with configurable activation, optional batch normalization and dropout. More...

#include <dense_layer.h>

Inheritance diagram for opennn::Dense:
[legend]

Public Member Functions

 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.
 
Shape get_output_shape () const override
 Returns the layer output shape, derived from input shape and configured output features.
 
Index get_input_features () const
 
Index get_sequence_length () const
 
const ActivationOp::Functionget_activation_function () const
 
ActivationOp::Function get_output_activation () const override
 Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).
 
bool get_batch_normalization () const
 
float get_momentum () const
 
vector< TensorSpecget_forward_specs (Index batch_size) const override
 Returns the tensor specs of intermediate forward buffers for a given batch size.
 
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.
 
void set_input_shape (const Shape &) override
 Sets the input shape; subclasses override to derive dependent dimensions.
 
void set_output_shape (const Shape &) override
 Sets the output shape; subclasses override when the output is user-configurable.
 
void on_compute_dtype_changed () override
 Subclass hook invoked when the compute dtype changes; default is no-op.
 
void set_activation_function (const string &)
 Sets the activation function from its string name (e.g. "Tanh", "Logistic", "Linear").
 
void set_batch_normalization (bool enable)
 Enables or disables batch normalization on the layer combination output.
 
void set_dropout_rate (float new_dropout_rate)
 
void set_momentum (float new_momentum)
 Sets the running-statistics momentum used by batch normalization.
 
void read_JSON_body (const Json *) override
 Subclass hook reading the body section of the layer's JSON node.
 
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).
 
- Public Member Functions inherited from opennn::Layer
virtual ~Layer ()=default
 
const string & get_label () const
 
const string & get_name () const
 
LayerType get_type () const
 
void set_label (string new_label)
 
Index get_parameters_number () const
 Returns the total number of trainable parameters owned by this layer.
 
const vector< Operator * > & get_operators () const
 
virtual vector< TensorSpecget_parameter_specs () const
 Returns the tensor specs of trainable parameters; subclasses override.
 
virtual vector< TensorSpecget_state_specs () const
 Returns the tensor specs of persistent state (e.g. running mean/variance).
 
virtual vector< TensorSpecget_backward_specs (Index batch_size) const
 Returns the tensor specs of the backward workspace; empty for non-trainable layers.
 
virtual Shape get_input_shape () const
 Returns the input shape stored by the layer.
 
Index get_inputs_number () const
 
Index get_outputs_number () const
 
virtual void forward_propagate (ForwardPropagation &fp, size_t layer, bool is_training) noexcept
 Runs the forward pass by chaining the layer's operators in order.
 
virtual void back_propagate (ForwardPropagation &fp, BackPropagation &bp, size_t i) const noexcept
 Runs the backward pass by chaining the layer's operators in reverse order.
 
virtual void from_JSON (const JsonDocument &document)
 Restores layer configuration and parameters from a JSON document.
 
virtual void load_state_from_JSON (const JsonDocument &document)
 Restores persistent state (e.g. running statistics) from a JSON document.
 
virtual void to_JSON (JsonWriter &writer) const
 Serializes layer configuration and parameters to a JSON writer.
 
virtual void write_JSON_body (JsonWriter &) const
 Subclass hook writing the body section of the layer's JSON node.
 
virtual void print () const
 Prints a human-readable summary of the layer to standard output.
 
bool get_is_trainable () const
 
Type get_compute_dtype () const
 
void set_compute_dtype (Type new_compute_dtype)
 Sets the compute dtype and notifies subclasses via on_compute_dtype_changed().
 
virtual float * link_states (float *pointer)
 Binds the persistent-state region of the shared buffer to operator views.
 
float * link_gradients (float *pointer, vector< TensorView > &gradient_views)
 Binds the gradient slice of the shared buffer to operator gradient views.
 
vector< TensorView > & get_parameter_views ()
 
const vector< TensorView > & get_parameter_views () const
 
void redistribute_parameters_to_operators ()
 Re-binds operator parameter views after the parameter buffer has been resized or moved.
 

Additional Inherited Members

- Protected Types inherited from opennn::Layer
enum  Forward { Input , Output }
 
enum  Backward { OutputDelta , InputDelta }
 
- Protected Member Functions inherited from opennn::Layer
 Layer ()=default
 
 Layer (LayerType t, bool trainable=true)
 
float * link_views_to_operators (vector< TensorView > &views, float *pointer, vector< TensorSpec >(Operator::*specs_fn)() const, void(Operator::*link_fn)(span< const TensorView >))
 
- Protected Attributes inherited from opennn::Layer
string label = "my_layer"
 
LayerType layer_type = LayerType::Dense
 
bool is_trainable = true
 
Shape input_shape
 
Type compute_dtype = Type::FP32
 
vector< TensorViewparameters
 
vector< TensorViewstates
 
vector< Operator * > operators
 

Detailed Description

Fully-connected layer with configurable activation, optional batch normalization and dropout.

Constructor & Destructor Documentation

◆ Dense()

opennn::Dense::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.

Member Function Documentation

◆ get_activation_function()

const ActivationOp::Function & opennn::Dense::get_activation_function ( ) const
inline

◆ get_batch_normalization()

bool opennn::Dense::get_batch_normalization ( ) const
inline

◆ get_forward_specs()

vector< TensorSpec > opennn::Dense::get_forward_specs ( Index batch_size) const
overridevirtual

Returns the tensor specs of intermediate forward buffers for a given batch size.

Reimplemented from opennn::Layer.

◆ get_input_features()

Index opennn::Dense::get_input_features ( ) const
inline

◆ get_momentum()

float opennn::Dense::get_momentum ( ) const
inline

◆ get_output_activation()

ActivationOp::Function opennn::Dense::get_output_activation ( ) const
inlineoverridevirtual

Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).

Reimplemented from opennn::Layer.

◆ get_output_shape()

Shape opennn::Dense::get_output_shape ( ) const
overridevirtual

Returns the layer output shape, derived from input shape and configured output features.

Implements opennn::Layer.

◆ get_sequence_length()

Index opennn::Dense::get_sequence_length ( ) const
inline

◆ on_compute_dtype_changed()

void opennn::Dense::on_compute_dtype_changed ( )
inlineoverridevirtual

Subclass hook invoked when the compute dtype changes; default is no-op.

Reimplemented from opennn::Layer.

◆ read_JSON_body()

void opennn::Dense::read_JSON_body ( const Json * )
overridevirtual

Subclass hook reading the body section of the layer's JSON node.

Reimplemented from opennn::Layer.

◆ set()

void opennn::Dense::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.

◆ set_activation_function()

void opennn::Dense::set_activation_function ( const string & )

Sets the activation function from its string name (e.g. "Tanh", "Logistic", "Linear").

◆ set_batch_normalization()

void opennn::Dense::set_batch_normalization ( bool enable)

Enables or disables batch normalization on the layer combination output.

◆ set_dropout_rate()

void opennn::Dense::set_dropout_rate ( float new_dropout_rate)
inline

◆ set_input_shape()

void opennn::Dense::set_input_shape ( const Shape & )
overridevirtual

Sets the input shape; subclasses override to derive dependent dimensions.

Reimplemented from opennn::Layer.

◆ set_momentum()

void opennn::Dense::set_momentum ( float new_momentum)

Sets the running-statistics momentum used by batch normalization.

◆ set_output_shape()

void opennn::Dense::set_output_shape ( const Shape & )
overridevirtual

Sets the output shape; subclasses override when the output is user-configurable.

Reimplemented from opennn::Layer.

◆ write_expression()

string opennn::Dense::write_expression ( const vector< string > & input_names,
const vector< string > & output_names ) const
overridevirtual

Returns a human-readable mathematical expression for this layer (empty by default).

Parameters
input_namesNames assigned to the inputs.
output_namesNames assigned to the outputs.

Reimplemented from opennn::Layer.