didriknielsen / survae_flows

Code for paper "SurVAE Flows: Surjections to Bridge the Gap between VAEs and Flows"
MIT License
283 stars 34 forks source link

sample generation with fixed noise #8

Open hmdolatabadi opened 3 years ago

hmdolatabadi commented 3 years ago

Hi,

I want to generate a bunch of base distribution samples z and keep them fixed, so that as the training goes on I can compute their original space representation and see their evolution. However, it seems to me that the current code does not support that, right? The only option right now is to just sample from the learned distribution, but I cannot keep the latent representation fixed and it changes everytime I sample I guess.

Many thanks.

Nintorac commented 3 years ago

You could use pytorchs fork rng and set seed functions to achieve this though it would be good to have this functionality built in for some use cases

import torch

model = Flow(........)

with torch.random.fork_rng():
    torch.random.set_rng_state(42)
    x = model.sample(10)
pabloppp commented 3 years ago

If I'm not mistaken, you should be able to do something like:

z_sample = fized_noise.clone()
for transform in reversed(flow.transforms):
    z_sample = transform.inverse(z_sample)

Using this you can also experiment with different Zs, maybe reduce the std by multiplying it by a value < 1 so you get less different but more coherent results, etc... Would be great to add a couple of methods to the models todo maybe: z = flow.to_z(image) and image = flow.from_z(z) Or idk the naming...

didriknielsen commented 3 years ago

Hey!

As mentioned before, if you want to map between x and z, you can do this simply using:

# x to z:
for t in flow.transforms:
    x, _ = t(x)

# z to x:
for t in reversed(flow.transforms):
    z = t.inverse(z)

Note that this simple approach generalizes if you want to map to intermediate representations (by not looping through all transforms), etc.

I purposefully went for keeping the code simple and hackable and thus did not add specific functions for this behavior. You can see how to get your desired behavior by looking at the code.