Ejemplo n.º 1
0
# custom weights initialization called on netG and netD
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)


netG = netG(nc, nz, ngf, gfs, ngpu)

netG.apply(weights_init)
if opt.netG != '':
    netG.load_state_dict(torch.load(opt.netG))
print(netG)

netD = netD(nc, ndf, dfs, ngpu=1)
netD.apply(weights_init)
if opt.netD != '':
    netD.load_state_dict(torch.load(opt.netD))
print(netD)

criterion = nn.BCELoss()

# Optimizers
optimizerD = optim.Adam(netD.parameters(),
                        lr=opt.lr,
                        betas=(opt.beta1, 0.999),
                        weight_decay=opt.l2_fac)
Ejemplo n.º 2
0
nrow = 129
ncol = 129

nr = 100  # the number of realizations

# Dimension of latent vector
zx = 5
zy = 5
prior_weight = 2
LearningRate = 0.1

# Prepare GAN generator
device = torch.device("cpu")
netG = netG(1, 1, 64, 5, 1)
netG.load_state_dict(torch.load('netG_epoch_10.pth'))
netG.to(device)
netG.eval()
torch.set_grad_enabled(False)

netD = netD(1, 64, 5, 1)
netD.load_state_dict(torch.load('netD_epoch_10.pth'))
netD.to(device)
netD.eval()
torch.set_grad_enabled(False)

Sample_Point = 10
Reference_k = np.loadtxt('Ref_ln_K.txt')
mask_k = np.zeros((nrow, ncol))
Sample = np.zeros((Sample_Point, 2))
for i in range(Sample_Point):
ngpu = 1  # 'number of GPUs to use'

# Input batch parameters
batch_size = 1
zx = 97
zy = 97
npx = zx
npy = zy
# File directory
outf = './train_data'
epoch = 32

# Load model
# netG = netG(nc, nz, ngf, gfs, ngpu)
netG = netG_transformer()
netG.load_state_dict(torch.load('%s/netG_epoch_%d.pth' % (outf, epoch)))

# netG.load_state_dict(torch.load('./train_data_5_21_2020/netG_epoch_22.pth'))
# density: 4

netG.to(device)
netG.eval()
print(netG)
print()


def generate_condition(ref_k_array):
    # ref_k_array = np.loadtxt("k_array_ref_gan.txt")
    ref_k_array = torch.as_tensor(ref_k_array, dtype=torch.float32)
    random_matrix = torch.randint_like(ref_k_array, 2)
    for x in range(6):