Class ModuleDictImpl#
Defined in File moduledict.h
Inheritance Relationships#
Base Type#
public torch::nn::Cloneable< ModuleDictImpl >(Template Class Cloneable)
Class Documentation#
-
class ModuleDictImpl : public torch::nn::Cloneable<ModuleDictImpl>#
An OrderedDict of
Modules that registers its elements by theirkeys.torch::OrderedDict<std::string, std::shared_ptr<Module>> ordereddict = { {"linear", Linear(10, 3).ptr()}, {"conv", Conv2d(1, 2, 3).ptr()}, {"dropout", Dropout(0.5).ptr()}, }; torch::nn::ModuleDict dict1(ordereddict); for (const auto &module : *dict1) { module->pretty_print(std::cout); } std::vector<std::pair<std::string, std::shared_ptr<Module>>> list = { {"linear", Linear(10, 3).ptr()}, {"conv", Conv2d(1, 2, 3).ptr()}, {"dropout", Dropout(0.5).ptr()}, }; torch::nn::ModuleDict dict2(list); for (const auto &module : *dict2) { module->pretty_print(std::cout); }
Why should you use
ModuleDictinstead of a simplemaporOrderedDict? The value aModuleDictprovides over manually calling an ordered map of modules is that it allows treating the whole container as a single module, such that performing a transformation on theModuleDictapplies to each of the modules it stores (which are each a registered submodule of theModuleDict). For example, calling.to(torch::kCUDA)on aModuleDictwill move each module in the map to CUDA memory. For example:torch::OrderedDict<std::string, std::shared_ptr<Module>> ordereddict = { {"linear", Linear(10, 3).ptr()}, {"conv", Conv2d(1, 2, 3).ptr()}, {"dropout", Dropout(0.5).ptr()}, }; torch::nn::ModuleDict dict(ordereddict); // Convert all modules to CUDA. dict->to(torch::kCUDA);
Finally,
ModuleDictprovides a lightweight container API, such as allowing iteration over submodules, positional access, adding new modules from a vector of key-module pairs or anOrderedDictor anotherModuleDictafter construction viaupdate.Public Types
-
using Iterator = torch::OrderedDict<std::string, std::shared_ptr<Module>>::Iterator#
-
using ConstIterator = torch::OrderedDict<std::string, std::shared_ptr<Module>>::ConstIterator#
Public Functions
-
ModuleDictImpl() = default#
Constructs the
ModuleDictfrom a list of string-Module pairs.
Constructs the
ModuleDictfrom anOrderedDict.
-
inline std::vector<std::pair<std::string, std::shared_ptr<Module>>> items() const#
Return the items in the
ModuleDict.
-
inline std::vector<std::string> keys() const#
Return the keys in the
ModuleDict.
-
inline std::vector<std::shared_ptr<Module>> values() const#
Return the values in the
ModuleDict.
-
inline Iterator begin()#
Return an iterator to the start of
ModuleDict.
-
inline ConstIterator begin() const#
Return a const iterator to the start of
ModuleDict.
-
inline Iterator end()#
Return an iterator to the end of
ModuleDict.
-
inline ConstIterator end() const#
Return a const iterator to the end of
ModuleDict.
-
inline size_t size() const noexcept#
Return the number of items currently stored in the
ModuleDict.
-
inline bool empty() const noexcept#
Return true if the
ModuleDictis empty, otherwise return false.
-
inline bool contains(const std::string &key) const noexcept#
Check if the certain parameter with the key in the
ModuleDict.
-
inline void clear()#
Remove all items from the
ModuleDict.
-
inline virtual std::shared_ptr<Module> clone(const std::optional<Device> &device = std::nullopt) const override#
Special cloning function for
ModuleDictbecause it does not usereset().
-
inline virtual void reset() override#
reset()is empty forModuleDict, since it does not have parameters of its own.
-
inline virtual void pretty_print(std::ostream &stream) const override#
Pretty prints the
ModuleDictinto the givenstream.
-
inline std::shared_ptr<Module> operator[](const std::string &key) const#
Attempts to returns the
Moduleassociated with the givenkey.Throws an exception if no such
keyis stored in theModuleDict. Check contains(key) before for a non-throwing way of access.
-
template<typename T>
inline T &at(const std::string &key)# Attempts to return the module at the given key as the requested type.
Throws an exception if no such
keyis stored in theModuleDict. Check contains(key) before for a non-throwing way of access.
-
template<typename T>
inline const T &at(const std::string &key) const# Attempts to return the module at the given key as the requested type.
Throws an exception if no such
keyis stored in theModuleDict. Check contains(key) before for a non-throwing way of access.
-
inline std::shared_ptr<Module> pop(const std::string &key)#
Removes and returns the
Moduleassociated with the givenkey.Throws an exception if no such
keyis stored in theModuleDict. Check contains(key) before for a non-throwing way of access.
Updated the
ModuleDictwith a vector of key-module pairs.
-
template<typename Container>
inline void update(const Container &container)# Updated the
ModuleDictwith key-value pairs fromOrderedDictorModuleDict.
-
using Iterator = torch::OrderedDict<std::string, std::shared_ptr<Module>>::Iterator#