Shortcuts

Class ModuleListImpl

Inheritance Relationships

Base Type

Class Documentation

class torch::nn::ModuleListImpl : public torch::nn::Cloneable<ModuleListImpl>

A list of Modules that registers its elements.

torch::nn::ModuleList mlist(
  torch::nn::Linear(3, 4),
  torch::nn::BatchNorm1d(4),
  torch::nn::Dropout(0.5)
);

for (const auto &module : *mlist) {
  module->pretty_print(std::cout);
}

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

torch::nn::ModuleList mlist(
  torch::nn::Linear(3, 4),
  torch::nn::BatchNorm1d(4),
  torch::nn::Dropout(0.5)
);

// Convert all modules to CUDA.
mlist->to(torch::kCUDA);

Finally, ModuleList 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 ModuleLists via extend.

Public Types

using Iterator = std::vector<std::shared_ptr<Module>>::iterator
using ConstIterator = std::vector<std::shared_ptr<Module>>::const_iterator

Public Functions

ModuleListImpl() = default
template<typename ...Modules>
ModuleListImpl(Modules&&... modules)

Constructs the ModuleList from a variadic list of modules.

std::shared_ptr<Module> clone(const optional<Device> &device = nullopt) const override

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

void reset() override

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

void pretty_print(std::ostream &stream) const override

Pretty prints the ModuleList module into the given stream.

void push_back(std::shared_ptr<Module> module)
template<typename M, typename = torch::detail::enable_if_module_t<M>>
void push_back(M &&module)

Adds a new Module to the ModuleList container, moving or copying it into a shared_ptr internally.

This method allows passing value types, and letting the container deal with the boxing.

template<typename M>
void push_back(const ModuleHolder<M> &module_holder)

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

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

ConstIterator begin() const

Returns a const iterator to the start of the ModuleList.

Iterator end()

Returns an iterator to the end of the ModuleList.

ConstIterator end() const

Returns a const iterator to the end of the ModuleList.

template<typename T>
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>
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.

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.

template<typename T>
std::shared_ptr<T> ptr(size_t index) const

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.

std::shared_ptr<Module> operator[](size_t index) const

Like ptr(index).

size_t size() const noexcept

The current size of the ModuleList container.

bool is_empty() const noexcept

True if there are no modules in the ModuleList.

void insert(size_t index, std::shared_ptr<Module> module)
template<typename M>
void insert(size_t index, const ModuleHolder<M> &module_holder)

Unwraps the contained module of a ModuleHolder and inserts it in the ModuleList.

template<typename M, typename = torch::detail::enable_if_module_t<M>>
void insert(size_t index, M &&module)

inserts a new Module to the ModuleList container, moving or copying it into a shared_ptr internally.

This method allows passing value types, and letting the container deal with the boxing.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources