Class NamedAnyModule¶
Defined in File named_any.h
Page Contents
Class Documentation¶
-
class NamedAnyModule¶
Stores a type erased
Module
with name.The
NamedAnyModule
class enables the following API for constructingnn::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
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 aModule
, moving or copying it into ashared_ptr
internally.
-
template<typename M>
inline NamedAnyModule(std::string name, const ModuleHolder<M> &module_holder)¶ Creates a
NamedAnyModule
from aModule
that is unwrapped from aModuleHolder
.
-
inline NamedAnyModule(std::string name, AnyModule any_module)¶
Creates a
NamedAnyModule
from a type-erasedAnyModule
.
-
inline const std::string &name() const noexcept¶
Returns a reference to the name.