Class ModuleListImpl¶
Defined in File modulelist.h
Page Contents
Inheritance Relationships¶
Base Type¶
public torch::nn::Cloneable< ModuleListImpl >(Template Class Cloneable)
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
ModuleListinstead of a simplestd::vector? The value aModuleListprovides 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 theModuleListapplies to each of the modules it stores (which are each a registered submodule of theModuleList). For example, calling.to(torch::kCUDA)on aModuleListwill 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,
ModuleListprovides a lightweight container API, such as allowing iteration over submodules, positional access, adding a new module after construction viapush_back, as well as joining twoModuleLists viaextend.Public Types
Public Functions
-
ModuleListImpl() = default¶
-
template<typename ...Modules>
inline explicit ModuleListImpl(Modules&&... modules)¶ Constructs the
ModuleListfrom a variadic list of modules.
-
inline virtual std::shared_ptr<Module> clone(const std::optional<Device> &device = nullopt) const override¶
Special cloning function for
ModuleListbecause it does not usereset().
-
inline virtual void reset() override¶
reset()is empty forModuleList, since it does not have parameters of its own.
-
inline virtual void pretty_print(std::ostream &stream) const override¶
Pretty prints the
ModuleListmodule into the givenstream.
-
template<typename M, typename = torch::detail::enable_if_module_t<M>>
inline void push_back(M &&module)¶ Adds a new
Moduleto theModuleListcontainer, moving or copying it into ashared_ptrinternally.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
ModuleHolderand adds it to theModuleList.
-
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_ptrwhose 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_ptrwhose 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
ModuleListcontainer.
-
inline bool is_empty() const noexcept¶
True if there are no modules in the
ModuleList.
-
template<typename M>
inline void insert(size_t index, const ModuleHolder<M> &module_holder)¶ Unwraps the contained module of a
ModuleHolderand inserts it in theModuleList.
-
template<typename M, typename = torch::detail::enable_if_module_t<M>>
inline void insert(size_t index, M &&module)¶ inserts a new
Moduleto theModuleListcontainer, moving or copying it into ashared_ptrinternally.This method allows passing value types, and letting the container deal with the boxing.
-
ModuleListImpl() = default¶