Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Types | Public Member Functions
torch::nn::SequentialImpl Class Reference

A list of Modules that acts as a Module itself. More...

#include <sequential.h>

Inheritance diagram for torch::nn::SequentialImpl:
torch::nn::Cloneable< SequentialImpl > torch::nn::Module

Public Types

using Iterator = std::vector< AnyModule >::iterator
 
using ConstIterator = std::vector< AnyModule >::const_iterator
 
- Public Types inherited from torch::nn::Module
using ModuleApplyFunction = std::function< void(Module &)>
 
using ConstModuleApplyFunction = std::function< void(const Module &)>
 
using NamedModuleApplyFunction = std::function< void(const std::string &, Module &)>
 
using ConstNamedModuleApplyFunction = std::function< void(const std::string &, const Module &)>
 
using ModulePointerApplyFunction = std::function< void(const std::shared_ptr< Module > &)>
 
using NamedModulePointerApplyFunction = std::function< void(const std::string &, const std::shared_ptr< Module > &)>
 

Public Member Functions

template<typename... Modules>
 SequentialImpl (Modules &&...modules)
 Constructs the Sequential from a variadic list of modules.
 
std::shared_ptr< Moduleclone (const optional< Device > &device=nullopt) const override
 Special cloning function for Sequential because it does not use reset(). More...
 
void reset () override
 reset() is empty for Sequential, since it does not have parameters of its own. More...
 
void pretty_print (std::ostream &stream) const override
 Pretty prints the Sequential module into the given stream.
 
template<typename ReturnType = Tensor, typename... InputTypes>
ReturnType forward (InputTypes &&...inputs)
 Feeds inputs to the first module and then chains outputs to inputs, returning the last output. More...
 
template<typename ModuleType >
void push_back (std::shared_ptr< ModuleType > module_ptr)
 Adds a new (boxed) Module to the Sequential container.
 
template<typename M , typename = torch::detail::enable_if_module_t<M>>
void push_back (M &&module)
 Adds a new Module to the Sequential container, moving or copying it into a shared_ptr internally. More...
 
template<typename M >
void push_back (const ModuleHolder< M > &module_holder)
 Unwraps the contained module of a ModuleHolder and adds it to the Sequential. More...
 
template<typename Container >
void extend (const Container &container)
 Iterates over the container and calls push_back() on each value.
 
Iterator begin ()
 Returns an iterator to the start of the Sequential.
 
ConstIterator begin () const
 Returns a const iterator to the start of the Sequential.
 
Iterator end ()
 Returns an iterator to the end of the Sequential.
 
ConstIterator end () const
 Returns a const iterator to the end of the Sequential.
 
template<typename T >
Tat (size_t index)
 Attempts to return the module at the given index as the requested type. More...
 
template<typename T >
const Tat (size_t index) const
 Attempts to return the module at the given index as the requested type. More...
 
std::shared_ptr< Moduleptr (size_t index) const
 Attempts to return a std::shared_ptr whose dynamic type is that of the underlying module at the given index. More...
 
template<typename T >
std::shared_ptr< Tptr (size_t index) const
 Attempts to return a std::shared_ptr whose type is the one provided. More...
 
std::shared_ptr< Moduleoperator[] (size_t index) const
 Like ptr(index).
 
size_t size () const noexcept
 The current size of the Sequential container.
 
bool is_empty () const noexcept
 True if there are no modules in the Sequential.
 
- Public Member Functions inherited from torch::nn::Cloneable< SequentialImpl >
std::shared_ptr< Moduleclone (const optional< Device > &device=nullopt) const override
 Performs a recursive "deep copy" of the Module, such that all parameters and submodules in the cloned module are different from those in the original module. More...
 
- Public Member Functions inherited from torch::nn::Module
 Module (std::string name)
 Tells the base Module about the name of the submodule.
 
 Module ()
 Constructs the module without immediate knowledge of the submodule's name. More...
 
const std::string & name () const noexcept
 Returns the name of the Module. More...
 
void apply (const ModuleApplyFunction &function)
 Applies the function to the Module and recursively to every submodule. More...
 
void apply (const ConstModuleApplyFunction &function) const
 Applies the function to the Module and recursively to every submodule. More...
 
void apply (const NamedModuleApplyFunction &function, const std::string &name_prefix=std::string())
 Applies the function to the Module and recursively to every submodule. More...
 
void apply (const ConstNamedModuleApplyFunction &function, const std::string &name_prefix=std::string()) const
 Applies the function to the Module and recursively to every submodule. More...
 
void apply (const ModulePointerApplyFunction &function) const
 Applies the function to the Module and recursively to every submodule. More...
 
void apply (const NamedModulePointerApplyFunction &function, const std::string &name_prefix=std::string()) const
 Applies the function to the Module and recursively to every submodule. More...
 
std::vector< Tensorparameters (bool recurse=true) const
 Returns the parameters of this Module and if recurse is true, also recursively of every submodule. More...
 
OrderedDict< std::string, Tensornamed_parameters (bool recurse=true) const
 Returns an OrderedDict with the parameters of this Module along with their keys, and if recurse is true also recursively of every submodule. More...
 
std::vector< Tensorbuffers (bool recurse=true) const
 Returns the buffers of this Module and if recurse is true, also recursively of every submodule. More...
 
OrderedDict< std::string, Tensornamed_buffers (bool recurse=true) const
 Returns an OrderedDict with the buffers of this Module along with their keys, and if recurse is true also recursively of every submodule. More...
 
std::vector< std::shared_ptr< Module > > modules (bool include_self=true) const
 Returns the submodules of this Module (the entire submodule hierarchy) and if include_self is true, also inserts a shared_ptr to this module in the first position. More...
 
OrderedDict< std::string, std::shared_ptr< Module > > named_modules (const std::string &name_prefix=std::string(), bool include_self=true) const
 Returns an OrderedDict of he submodules of this Module (the entire submodule hierarchy) and thei keys, and if include_self is true, also inserts a shared_ptr to this module in the first position. More...
 
std::vector< std::shared_ptr< Module > > children () const
 Returns the direct submodules of this Module.
 
OrderedDict< std::string, std::shared_ptr< Module > > named_children () const
 Returns an OrderedDict of the direct submodules of this Module and their keys. More...
 
virtual void train (bool on=true)
 Enables "training" mode.
 
void eval ()
 Calls train(false) to enable "eval" mode. More...
 
virtual bool is_training () const noexcept
 True if the module is in training mode. More...
 
virtual void to (torch::Device device, torch::Dtype dtype, bool non_blocking=false)
 Recursively casts all parameters to the given dtype and device. More...
 
virtual void to (torch::Dtype dtype, bool non_blocking=false)
 Recursively casts all parameters to the given dtype. More...
 
virtual void to (torch::Device device, bool non_blocking=false)
 Recursively moves all parameters to the given device. More...
 
virtual void zero_grad ()
 Recursively zeros out the grad value of each registered parameter.
 
template<typename ModuleType >
ModuleType::ContainedType * as () noexcept
 Attempts to cast this Module to the given ModuleType. More...
 
template<typename ModuleType >
const ModuleType::ContainedType * as () const noexcept
 Attempts to cast this Module to the given ModuleType. More...
 
template<typename ModuleType , typename = torch::detail::disable_if_module_holder_t<ModuleType>>
ModuleType * as () noexcept
 Attempts to cast this Module to the given ModuleType. More...
 
template<typename ModuleType , typename = torch::detail::disable_if_module_holder_t<ModuleType>>
const ModuleType * as () const noexcept
 Attempts to cast this Module to the given ModuleType. More...
 
virtual void save (serialize::OutputArchive &archive) const
 Serializes the Module into the given OutputArchive.
 
virtual void load (serialize::InputArchive &archive)
 Deserializes the Module from the given InputArchive.
 

Additional Inherited Members

- Protected Member Functions inherited from torch::nn::Module
Tensorregister_parameter (std::string name, Tensor tensor, bool requires_grad=true)
 Registers a parameter with this Module. More...
 
Tensorregister_buffer (std::string name, Tensor tensor)
 Registers a buffer with this Module. More...
 
template<typename ModuleType >
std::shared_ptr< ModuleType > register_module (std::string name, std::shared_ptr< ModuleType > module)
 Registers a submodule with this Module. More...
 
template<typename ModuleType >
std::shared_ptr< ModuleType > register_module (std::string name, ModuleHolder< ModuleType > module_holder)
 Registers a submodule with this Module. More...
 

Detailed Description

A list of Modules that acts as a Module itself.

A Sequential is fundamentally a list of Modules, each with a forward() method. Sequential provides a forward() 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:

.. code-block:: cpp

torch::nn::Sequential seq( torch::nn::Linear(3, 4), torch::nn::BatchNorm(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):

.. code-block:: python

def forward(sequential, input): for module in sequential: input = module(input) return input

Why should you use Sequential instead of a simple std::vector? The value a Sequential 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 the Sequential applies to each of the modules it stores (which are each a registered submodule of the Sequential). For example, calling .to(torch::kCUDA) on a Sequential will move each module in the list to CUDA memory. For example:

.. code-block:: cpp

torch::nn::Sequential seq( torch::nn::Linear(3, 4), torch::nn::BatchNorm(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 via push_back, as well as joining two Sequentials via extend.

.. 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.

Definition at line 91 of file sequential.h.

Member Function Documentation

template<typename T >
T& torch::nn::SequentialImpl::at ( size_t  index)
inline

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.

Definition at line 244 of file sequential.h.

template<typename T >
const T& torch::nn::SequentialImpl::at ( size_t  index) const
inline

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.

Definition at line 256 of file sequential.h.

std::shared_ptr<Module> torch::nn::SequentialImpl::clone ( const optional< Device > &  device = nullopt) const
inlineoverridevirtual

Special cloning function for Sequential because it does not use reset().

Reimplemented from torch::nn::Module.

Definition at line 107 of file sequential.h.

template<typename ReturnType = Tensor, typename... InputTypes>
ReturnType torch::nn::SequentialImpl::forward ( InputTypes &&...  inputs)
inline

Feeds inputs to the first module and then chains outputs to inputs, returning the last output.

Conceptually the following loop in Python:

.. code-block:: 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 the Sequential returns another type T, you should call forward<T>(inputs) instead of just forward(inputs):

.. code-block:: cpp

torch::Tensor tensor = sequential1->forward(inputs); int integer = sequential2->forward<int>(inputs); float value = sequential3->forward<float>(inputs);

Definition at line 153 of file sequential.h.

std::shared_ptr<Module> torch::nn::SequentialImpl::ptr ( size_t  index) const
inline

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.

Definition at line 267 of file sequential.h.

template<typename T >
std::shared_ptr<T> torch::nn::SequentialImpl::ptr ( size_t  index) const
inline

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.

Definition at line 276 of file sequential.h.

template<typename M , typename = torch::detail::enable_if_module_t<M>>
void torch::nn::SequentialImpl::push_back ( M &&  module)
inline

Adds a new Module to the Sequential container, moving or copying it into a shared_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 of Sequential(std::make_shared<Module>(3, 4)).

Definition at line 198 of file sequential.h.

template<typename M >
void torch::nn::SequentialImpl::push_back ( const ModuleHolder< M > &  module_holder)
inline

Unwraps the contained module of a ModuleHolder and adds it to the Sequential.

Definition at line 208 of file sequential.h.

void torch::nn::SequentialImpl::reset ( )
inlineoverridevirtual

reset() is empty for Sequential, since it does not have parameters of its own.

Implements torch::nn::Cloneable< SequentialImpl >.

Definition at line 118 of file sequential.h.


The documentation for this class was generated from the following file: