OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
recurrent_layer.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// R E C U R R E N T 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
13namespace opennn
14{
15
17class Recurrent final : public Layer
18{
19public:
20
24 Recurrent(const Shape& = {0, 0}, const Shape& = {0});
25
27 Shape get_input_shape() const override { return {time_steps, input_features}; }
28
30 Shape get_output_shape() const override;
31
32 const TensorView& get_biases() const { return biases; }
33 const TensorView& get_input_weights() const { return input_weights; }
34 const TensorView& get_recurrent_weights() const { return recurrent_weights; }
35 const string& get_activation_function() const { return activation_function; }
36
38 vector<TensorSpec> get_parameter_specs() const override;
39
41 vector<TensorSpec> get_forward_specs(Index batch_size) const override;
42
44 vector<TensorSpec> get_backward_specs(Index batch_size) const override;
45
47 void set(const Shape& = {}, const Shape& = {});
48
50 void set_input_shape(const Shape&) override;
51
53 void set_output_shape(const Shape&) override;
54
56 void set_activation_function(const string&);
57
59 void forward_propagate(ForwardPropagation&, size_t, bool) noexcept override;
60
62 void back_propagate(ForwardPropagation&, BackPropagation&, size_t) const noexcept override;
63
65 void read_JSON_body(const Json*) override;
66
68 void write_JSON_body(JsonWriter&) const override;
69
71 string write_expression(const vector<string>& input_names,
72 const vector<string>& output_names) const override;
73
74private:
75
76 Index time_steps = 0;
77 Index input_features = 0;
78
79 TensorView biases;
80 TensorView input_weights;
81 TensorView recurrent_weights;
82
83 Tensor2 empty_2; // sentinel passed to calculate_activations<2>(...) when no derivative output is wanted
84
85 string activation_function = "Tanh";
86};
87
88}
89
90// OpenNN: Open Neural Networks Library.
91// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
92// Licensed under the GNU Lesser General Public License v2.1 or later.
Definition json.h:85
Definition json.h:23
Layer()=default
vector< TensorSpec > get_backward_specs(Index batch_size) const override
Returns the tensor specifications used during back propagation.
vector< TensorSpec > get_parameter_specs() const override
Returns the tensor specifications for the trainable parameters.
void set(const Shape &={}, const Shape &={})
Reconfigures the layer with new input and output shapes.
const string & get_activation_function() const
Definition recurrent_layer.h:35
vector< TensorSpec > get_forward_specs(Index batch_size) const override
Returns the tensor specifications used during forward propagation.
Recurrent(const Shape &={0, 0}, const Shape &={0})
Constructs a recurrent layer with input and output shapes.
void read_JSON_body(const Json *) override
Reads the layer configuration from a JSON node.
void forward_propagate(ForwardPropagation &, size_t, bool) noexcept override
Runs the forward pass, unrolling the recurrence across time steps.
const TensorView & get_input_weights() const
Definition recurrent_layer.h:33
Shape get_output_shape() const override
Returns the output tensor shape (hidden units).
const TensorView & get_recurrent_weights() const
Definition recurrent_layer.h:34
void back_propagate(ForwardPropagation &, BackPropagation &, size_t) const noexcept override
Runs the backward pass (backpropagation through time).
void set_output_shape(const Shape &) override
Updates the layer for a new output shape.
void set_activation_function(const string &)
Sets the activation function by name (e.g. "Tanh", "ReLU").
string write_expression(const vector< string > &input_names, const vector< string > &output_names) const override
Returns a symbolic expression of the layer for export.
void write_JSON_body(JsonWriter &) const override
Writes the layer configuration to a JSON writer.
const TensorView & get_biases() const
Definition recurrent_layer.h:32
Shape get_input_shape() const override
Returns the input tensor shape (time_steps, input_features).
Definition recurrent_layer.h:27
void set_input_shape(const Shape &) override
Updates the layer for a new input shape.
Definition adaptive_moment_estimation.h:14
Tensor< float, 2, Layout|AlignedMax > Tensor2
Definition pch.h:189
Workspace holding parameter gradients and per-layer deltas during a backward pass.
Definition back_propagation.h:21
Workspace holding the activations of every layer during a forward pass.
Definition forward_propagation.h:20
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42
Non-owning view over a tensor: pointer, shape, and data type with rich reshape helpers.
Definition tensor_utilities.h:293