OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
embedding_layer.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// E M B E D D I N G 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 Embedding final : public Layer
19{
20public:
21
26 Embedding(const Shape& = {0, 0},
27 Index = 0,
28 const string& = "embedding_layer");
29
31 Shape get_input_shape() const override { return {sequence_length}; }
32
34 Shape get_output_shape() const override;
35
36 Index get_vocabulary_size() const { return vocabulary_size; }
37 Index get_sequence_length() const { return sequence_length; }
38 Index get_embedding_dimension() const { return embedding_dimension; }
39
40 vector<TensorSpec> get_backward_specs(Index) const override { return {}; }
41
43 void set(Index = 0,
44 Index = 0,
45 Index = 0,
46 const string& = "embedding_layer");
47
49 void set_scale_embedding(bool enabled) { embedding_lookup.scale_embedding = enabled; }
50
52 void set_add_positional_encoding(bool enabled) { embedding_lookup.add_positional_encoding = enabled; }
53
55 void set_dropout_rate(float rate) { dropout.set_rate(rate); }
56
58 void read_JSON_body(const Json*) override;
59
61 void write_JSON_body(JsonWriter&) const override;
62
63private:
64
65 Index vocabulary_size = 0;
66 Index sequence_length = 0;
67 Index embedding_dimension = 0;
68
69 EmbeddingLookupOp embedding_lookup;
70 DropoutOp dropout;
71
72 enum Backward {OutputDelta};
73};
74
75}
76
77// OpenNN: Open Neural Networks Library.
78// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
79// Licensed under the GNU Lesser General Public License v2.1 or later.
void set_scale_embedding(bool enabled)
Enables or disables embedding scaling by sqrt(embedding_dimension).
Definition embedding_layer.h:49
void set_dropout_rate(float rate)
Sets the dropout rate applied to the embedding output.
Definition embedding_layer.h:55
void read_JSON_body(const Json *) override
Reads the layer configuration from a JSON node.
Embedding(const Shape &={0, 0}, Index=0, const string &="embedding_layer")
Constructs an embedding layer.
Shape get_input_shape() const override
Returns the input tensor shape (sequence_length of token ids).
Definition embedding_layer.h:31
Index get_sequence_length() const
Definition embedding_layer.h:37
void set(Index=0, Index=0, Index=0, const string &="embedding_layer")
Reconfigures the layer with vocabulary size, sequence length, embedding dimension and name.
void set_add_positional_encoding(bool enabled)
Enables or disables sinusoidal positional encoding added to the embeddings.
Definition embedding_layer.h:52
vector< TensorSpec > get_backward_specs(Index) const override
Returns the tensor specs of the backward workspace; empty for non-trainable layers.
Definition embedding_layer.h:40
Index get_vocabulary_size() const
Definition embedding_layer.h:36
Index get_embedding_dimension() const
Definition embedding_layer.h:38
Shape get_output_shape() const override
Returns the output tensor shape (sequence_length, embedding_dimension).
void write_JSON_body(JsonWriter &) const override
Writes the layer configuration to a JSON writer.
Definition json.h:85
Definition json.h:23
Layer()=default
Definition adaptive_moment_estimation.h:14
bool & enabled()
Definition profiler.h:68
Inverted dropout: at training time zeros activations with probability rate and rescales survivors.
Definition operators.h:122
Token embedding lookup with optional scaling and additive positional encoding.
Definition operators.h:947
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42