Class SequentialImpl¶
Defined in File sequential.h
Page Contents
Inheritance Relationships¶
Base Type¶
public torch::nn::Cloneable< SequentialImpl >
(Template Class Cloneable)
Class Documentation¶
-
class SequentialImpl : public torch::nn::Cloneable<SequentialImpl>¶
A list of
Module
s that acts as aModule
itself.A
Sequential
is fundamentally a list ofModule
s, each with aforward()
method.Sequential
provides aforward()
method of its own, which accepts any input and forwards it to the first module it stores. It then “chains” outputs to inputs sequentially for each subsequent module, finally returning the output of the last module. For example:torch::nn::Sequential seq( torch::nn::Linear(3, 4), torch::nn::BatchNorm1d(4), torch::nn::Dropout(0.5) ); auto output = seq->forward(torch::ones(3));
This can conceptually be thought of as the following loop (using Python as pseudocode):
def forward(sequential, input): for module in sequential: input = module(input) return input
Why should you use
Sequential
instead of a simplestd::vector
? The value aSequential
provides over manually calling a sequence of modules is that it allows treating the whole container as a single module, such that performing a transformation on theSequential
applies to each of the modules it stores (which are each a registered submodule of theSequential
). For example, calling.to(torch::kCUDA)
on aSequential
will move each module in the list to CUDA memory. For example:torch::nn::Sequential seq( torch::nn::Linear(3, 4), torch::nn::BatchNorm1d(4), torch::nn::Dropout(0.5) ); // Convert all modules to CUDA. seq->to(torch::kCUDA);
Finally,
Sequential
provides a lightweight container API, such as allowing iteration over submodules, positional access, adding a new module after construction viapush_back
, as well as joining twoSequential
s viaextend
.Attention
One current limitation of Sequential is that all except the first module must accept a single argument. If your modules need to take multiple arguments, you should define them to take and return tuples.
Public Types
Public Functions
-
SequentialImpl() = default¶
-
template<typename ...Modules>
inline explicit SequentialImpl(Modules&&... modules)¶ Constructs the
Sequential
from a variadic list of modules.
-
inline explicit SequentialImpl(torch::OrderedDict<std::string, AnyModule> &&ordered_dict)¶
Constructs the
Sequential
from anOrderedDict
of namedAnyModule
s.
-
inline explicit SequentialImpl(std::initializer_list<NamedAnyModule> named_modules)¶
Constructs the
Sequential
from a braced-init-list of namedAnyModule
s.It enables the following use case:
Sequential sequential({{"m1", M(1)}, {"m2", M(2)}})
-
inline virtual std::shared_ptr<Module> clone(const std::optional<Device> &device = std::nullopt) const override¶
Special cloning function for
Sequential
because it does not usereset()
.
-
inline virtual void reset() override¶
reset()
is empty forSequential
, since it does not have parameters of its own.
-
inline virtual void pretty_print(std::ostream &stream) const override¶
Pretty prints the
Sequential
module into the givenstream
.
-
template<typename ReturnType = Tensor, typename ...InputTypes>
inline ReturnType forward(InputTypes&&... inputs)¶ Feeds
inputs
to the first module and then chains outputs to inputs, returning the last output.Conceptually the following loop in Python:
def forward(sequential, input): for module in sequential: input = module(input) return input
The return type is taken as the first template parameter. It defaults to
Tensor
. If the last module in theSequential
returns another typeT
, you should callforward<T>(inputs)
instead of justforward(inputs)
:torch::Tensor tensor = sequential1->forward(inputs); int integer = sequential2->forward<int>(inputs); float value = sequential3->forward<float>(inputs);
Adds a new (boxed)
Module
to theSequential
container.
Adds a new named (boxed)
Module
to theSequential
container.
-
template<typename M, typename = torch::detail::enable_if_module_t<M>>
inline void push_back(M &&module)¶ Adds a new
Module
to theSequential
container, moving or copying it into ashared_ptr
internally.This method allows passing value types, and letting the container deal with the boxing. This means you can write
Sequential(Module(3, 4))
instead ofSequential(std::make_shared<Module>(3, 4))
.
-
template<typename M, typename = torch::detail::enable_if_module_t<M>>
inline void push_back(std::string name, M &&module)¶ Adds a new named
Module
to theSequential
container, moving or copying it into ashared_ptr
internally.This method allows passing value types, and letting the container deal with the boxing.
-
template<typename M>
inline void push_back(const ModuleHolder<M> &module_holder)¶ Unwraps the contained module of a
ModuleHolder
and adds it to theSequential
.
-
template<typename M>
inline void push_back(std::string name, const ModuleHolder<M> &module_holder)¶ Unwraps the contained named module of a
ModuleHolder
and adds it to theSequential
.
-
template<typename Container>
inline void extend(const Container &container)¶ Iterates over the container and calls
push_back()
on each value.
-
inline void push_back(AnyModule any_module)¶
Adds a type-erased
AnyModule
to theSequential
.
-
inline Iterator begin()¶
Returns an iterator to the start of the
Sequential
.
-
inline ConstIterator begin() const¶
Returns a const iterator to the start of the
Sequential
.
-
inline Iterator end()¶
Returns an iterator to the end of the
Sequential
.
-
inline ConstIterator end() const¶
Returns a const iterator to the end of the
Sequential
.
-
template<typename T>
inline T &at(size_t index)¶ Attempts to return the module at the given index as the requested type.
Throws an exception if the index is out of bounds or the types do not match.
-
template<typename T>
inline const T &at(size_t index) const¶ Attempts to return the module at the given index as the requested type.
Throws an exception if the index is out of bounds or the types do not match.
-
inline std::shared_ptr<Module> ptr(size_t index) const¶
Attempts to return a
std::shared_ptr
whose dynamic type is that of the underlying module at the given index.Throws an exception if the index is out of bounds.
Attempts to return a
std::shared_ptr
whose type is the one provided.Throws an exception if the index is out of bounds or the types do not match.
-
inline size_t size() const noexcept¶
The current size of the
Sequential
container.
-
inline bool is_empty() const noexcept¶
True if there are no modules in the
Sequential
.
-
SequentialImpl() = default¶