Skip to content

[PyTorch] How to clone an instance of Module? #1230

Answered by HGuillemet
guangster asked this question in Q&A
Discussion options

You must be logged in to vote

If you'd like to clone an instance of a Java class subclassing Module, you need a Java implementation of clone(), since the C++ library knows nothing about the Java subclass. We could define some CloneableModule mapping torch::nn::Cloneable<torch::nn::Module> and Java modules could inherit from it instead of Module. So the Java implementation of clone() could call the C++ clone() from Cloneable, but it won't help since you'll get an object that is not an instance of the Java class.

For saving/loading models, I currently use OutputArchive and Module.save/Module.load.
Maybe could you use it for cloning too ?

Replies: 4 comments 12 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@saudet
Comment options

Comment options

You must be logged in to vote
11 replies
@guangster
Comment options

@saudet
Comment options

@saudet
Comment options

@HGuillemet
Comment options

@guangster
Comment options

Answer selected by guangster
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants