Class NamedAnyModule

Page Contents

Class Documentation

class NamedAnyModule

Stores a type erased Module with name.

The NamedAnyModule class enables the following API for constructing nn::Sequential with named submodules:

struct M : torch::nn::Module {
  explicit M(int value_) : value(value_) {}
  int value;
  int forward() {
    return value;

Sequential sequential({
  {"m1", std::make_shared<M>(1)},  // shared pointer to `Module` is
  supported {std::string("m2"), M(2)},  // `Module` is supported
  {"linear1", Linear(10, 3)}  // `ModuleHolder` is supported

Public Functions

template<typename ModuleType>
inline NamedAnyModule(std::string name, std::shared_ptr<ModuleType> module_ptr)

Creates a NamedAnyModule from a (boxed) Module.

template<typename M, typename = torch::detail::enable_if_module_t<M>>
inline NamedAnyModule(std::string name, M &&module)

Creates a NamedAnyModule from a Module, moving or copying it into a shared_ptr internally.

template<typename M>
inline NamedAnyModule(std::string name, const ModuleHolder<M> &module_holder)

Creates a NamedAnyModule from a Module that is unwrapped from a ModuleHolder.

inline NamedAnyModule(std::string name, AnyModule any_module)

Creates a NamedAnyModule from a type-erased AnyModule.

inline const std::string &name() const noexcept

Returns a reference to the name.

inline AnyModule &module() noexcept

Returns a reference to the module.

inline const AnyModule &module() const noexcept

Returns a const reference to the module.


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources