Ejemplo n.º 1
0
    freeze_embeddings=args.freeze_emb, gpu=args.gpu
)

if args.gpu:
    model.load_state_dict(torch.load('models/{}.bin'
                                     .format(args.model
                                             + utils.getModelName(args))))
else:
    model.load_state_dict(torch.load('models/{}.bin'
                                     .format(args.model
                                             + utils.getModelName(args)),
                                     map_location=lambda storage, loc: storage)
                          )

# Samples latent and conditional codes randomly from prior
z = model.sample_z_prior(1)
c = model.sample_c_prior(1)

# Generate positive sample given z
c[0, 0], c[0, 1] = 1, 0

_, c_idx = torch.max(c, dim=1)
sample_idxs = model.sample_sentence(z, c, temp=0.1)

print('\nSentiment: {}'.format(dataset.idx2label(int(c_idx))))
print('Generated: {}'.format(dataset.idxs2sentence(sample_idxs)))

# Generate negative sample from the same z
c[0, 0], c[0, 1] = 0, 1

_, c_idx = torch.max(c, dim=1)
Ejemplo n.º 2
0
if args.gpu:
    model.load_state_dict(
        torch.load('models/{}.bin'.format(args.model +
                                          utils.getModelName(args))))
else:
    model.load_state_dict(
        torch.load('models/{}.bin'.format(args.model +
                                          utils.getModelName(args)),
                   map_location=lambda storage, loc: storage))

outputFile = open("models/gen" + utils.getModelName(args) + ".out.txt", "w+")

for i in range(n_iter):
    # Samples latent and conditional codes randomly from prior
    z = model.sample_z_prior(1)
    c = model.sample_c_prior(1)

    # Generate positive sample given z
    for j in range(args.num_classes):
        for k in range(args.num_classes):
            c[0, k] = 0

        c[0, j] = 1

        _, c_idx = torch.max(c, dim=1)
        sample_idxs = model.sample_sentence(z, c, temp=0.1)

        outputFile.write('{}\t{}\n'.format(dataset.idxs2sentence(sample_idxs),
                                           str(dataset.idx2label(int(c_idx)))))