Class NamedAnyModule

Page Contents

Class Documentation

class torch::nn::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>
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>>
NamedAnyModule(std::string name, M &&module)

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

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

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

NamedAnyModule(std::string name, AnyModule any_module)

Creates a NamedAnyModule from a type-erased AnyModule.

const std::string &name() const noexcept

Returns a reference to the name.

AnyModule &module() noexcept

Returns a reference to the module.

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