示例#1
0
start_time = time.time()
print >> sys.stderr, 'Initial det(W)', np.linalg.det(
    generator.gen_l_out.W.get_value())

for batch_id in range(1, num_epochs + 1):
    id1 = next(we_batches1)
    id2 = next(we_batches2)
    X[:HALF_BATCH_SIZE] = we1.vectors[id1]
    gen_loss_val, recon_gen_loss_val, adv_gen_loss_val, X_gen = gen_train_fn(
        X[:HALF_BATCH_SIZE])

    X[:HALF_BATCH_SIZE] = X_gen
    X[HALF_BATCH_SIZE:] = we2.vectors[id2]

    loss_val = discriminator.train_fn(X, target_mat)

    if batch_id > 10000 and gen_loss_val < gloss_min:
        gloss_min = gen_loss_val
        print >> sys.stderr, batch_id, gloss_min
        save_model()
        W = generator.gen_l_out.W.get_value()
        print >> sys.stderr, 'recon_gen_loss_val', recon_gen_loss_val, '||W^T*W - I||', np.linalg.norm(
            np.dot(W.T, W) - np.identity(d)), 'det(W)', np.linalg.det(W)

        we1.transformed_vectors = np.dot(we1.vectors,
                                         W)  #这里换成generator.gen_out试试?
        # we1.transformed_vectors = generation.get_value()
        we1.save_transformed_vectors(dataDir + 'transformed-' + str(modelID) +
                                     '.' + 'zh')

print >> sys.stderr, (time.time() - start_time) / 60, 'min'
示例#2
0
    loss_val, accuracy_val = discriminator.train_fn(X, target_mat)

    if batch_id % print_every_n == 0:
        W = gen_l_out.W.get_value()
        print '%s %s %s %s %s %s %s %s' % (
            batch_id, accuracy_val, loss_val, gen_loss_val, recon_gen_loss_val,
            adv_gen_loss_val, grad_norm_val,
            np.linalg.norm(np.dot(W.T, W) - np.identity(d)))

    if args.num_save > 0:
        if batch_id % (numBatches / args.num_save) == 0:
            save_model()
            W = gen_l_out.W.get_value()
            print >> sys.stderr, 'recon_gen_loss_val', recon_gen_loss_val, '||W^T*W - I||', np.linalg.norm(
                np.dot(W.T, W) - np.identity(d)), 'det(W)', np.linalg.det(W)
            we1.transformed_vectors = np.dot(we1.vectors, W)
            we1.save_transformed_vectors(dataDir + 'transformed-' +
                                         str(modelID) + '.' + lang1)
            modelID += 1
    else:
        if batch_id > 10000 and gen_loss_val < gloss_min:
            gloss_min = gen_loss_val
            print >> sys.stderr, batch_id, gloss_min
            save_model()
            W = gen_l_out.W.get_value()
            print >> sys.stderr, 'recon_gen_loss_val', recon_gen_loss_val, '||W^T*W - I||', np.linalg.norm(
                np.dot(W.T, W) - np.identity(d)), 'det(W)', np.linalg.det(W)
            we1.transformed_vectors = np.dot(we1.vectors, W)
            we1.save_transformed_vectors(dataDir + 'transformed-' +
                                         str(modelID) + '.' + lang1)
print >> sys.stderr, (time.time() - start_time) / 60, 'min'