Accessing part of a model (eg. encoder of an Autoencoder) when defined using setup
#2601
Unanswered
nipunbatra
asked this question in
Q&A
Replies: 1 comment
-
Hey @nipunbatra, there are 2 ways to go about this. Define an APIYou can define methods to interact with specific parts of you model and use them via the class AE(nn.Module):
bottleneck: int
out: int
def setup(self):
self.encoder = Encoder(bottleneck=self.bottleneck)
self.decoder = Decoder(out=self.out)
def __call__(self, x):
z = self.encoder(x)
x_hat = self.decoder(z)
return x_hat
def encode(self, x):
return self.encoder(x)
def decode(self, z):
return self.decoder(z)
ae = AE(1, 2)
variables = ae.init(random.PRNGKey(0), X)
params = variables['params']
# call encoder
z = ae.apply({'params': params}, X, method=ae.encode)
# call decoder
x_hat = ae.apply({'params': params}, X, method=ae.decode) Extract submodulesYou can use def extract_encoder(ae):
encoder_module = ae.encoder.clone() # avoid leakage
encoder_params = ae.encoder.variables['params']
return encoder_module, encoder_params
def extract_decoder(ae):
decoder_module = ae.decoder.clone() # avoid leakage
decoder_params = ae.decoder.variables['params']
return decoder_module, decoder_params
encoder, encoder_params = nn.apply(extract_encoder, ae)({'params': params})
decoder, decoder_params = nn.apply(extract_decoder, ae)({'params': params})
# call encoder
z = encoder.apply({'params': encoder_params}, X)
# call decoder
x_hat = decoder.apply({'params': decoder_params}, z) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have an Autoencoder module defined below.
When I try to access the
ae.encoder
, I get the following error.gives the error
As a workaround, I am able to do the following
encoded_1d = Encoder(1).apply({"params":params["params"]["encoder"]}, X)
What could be a simpler/better way to access the
encoder
part of the AutoEncoder module and use a.apply
on it with the encoder parameters?Beta Was this translation helpful? Give feedback.
All reactions