Shortcuts

Class ModuleListImpl

Inheritance Relationships

Base Type

Class Documentation

class 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>
inline explicit ModuleListImpl(Modules&&... modules)

Constructs the ModuleList from a variadic list of modules.

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

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

inline virtual void reset() override

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

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

Pretty prints the ModuleList module into the given stream.

inline void push_back(std::shared_ptr<Module> module)
template<typename M, typename = torch::detail::enable_if_module_t<M>>
inline 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>
inline void push_back(const ModuleHolder<M> &module_holder)

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

template<typename Container>
inline void extend(const Container &container)

Iterates over the container and calls push_back() on each value.

inline Iterator begin()

Returns an iterator to the start of the ModuleList.

inline ConstIterator begin() const

Returns a const iterator to the start of the ModuleList.

inline Iterator end()

Returns an iterator to the end of the ModuleList.

inline ConstIterator end() const

Returns a const iterator to the end of the ModuleList.

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.

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

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

Like ptr(index).

inline size_t size() const noexcept

The current size of the ModuleList container.

inline bool is_empty() const noexcept

True if there are no modules in the ModuleList.

inline void insert(size_t index, std::shared_ptr<Module> module)
template<typename M>
inline 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>>
inline 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