How to use ready network and generate data?

Hi, I’m new in pytorch as well as in deep learning.
I have used this repository: GitHub - LixiangHan/GANs-for-1D-Signal: implementation of several GANs with pytorch (dcgan is part im interested in) I changed training set for my needs and it performed pretty well. Now I would like to use trained generator to generate data and test it. Some research I have performed didn’t help me to find out how to do that…

Could anyone look at this code and give me some tips? Maybe answer is simple and obvious or
maybe there are some good articles/resources that could help me make progress.
After training a .pkl file is created. I was wondering If this might help?

After training the models, you could create a new Generator and load the trained state_dict via:

netG = Generator()
netG.load_state_dict(torch.load(path_to_generator_state_dict))

Afterwards, you could execute the forward pass using the same inputs as were used to train this model:

with torch.no_grad():
    noise = torch.randn(b_size, nz, 1, device=device)
    fake = netG(noise)
1 Like

Yes, you’re on the right track! After training the models, you can save the state_dict of the trained generator using torch.save(). The code snippet you provided demonstrates how to load the saved state_dict and create a new instance of the generator.

Here’s an example of how you can generate lead data enrichment using the trained generator:
import torch
from generator import Generator # Assuming the generator code is in ‘generator.py’

Create an instance of the generator

netG = Generator()

Load the state_dict of the trained generator

path_to_generator_state_dict = ‘path/to/your/generator_state_dict.pkl’
netG.load_state_dict(torch.load(path_to_generator_state_dict))

Set the generator in evaluation mode

netG.eval()

Generate data using the generator

num_samples = 10 # Specify the number of samples you want to generate
latent_dim = 100 # Specify the size of the generator’s input latent vector

Generate random noise vectors as input to the generator

noise = torch.randn(num_samples, latent_dim)

Pass the noise through the generator to generate samples

generated_samples = netG(noise)

The generated_samples tensor now contains the generated data

print(generated_samples)

Make sure you have the generator.py file available, or modify the import statement accordingly to match the actual file name and location.

The generated_samples tensor will contain the generated data. You can manipulate or analyze this data as per your requirements.

Remember to adjust the num_samples and latent_dim variables to match your specific needs. num_samples determines how many samples you want to generate, and latent_dim specifies the size of the input noise vector for the generator.