示例#1
0

for epoch in (range(1,num_epoches)):
# print("give me a clue")
    data, label = mm.batch_next(batch_size,[0,1,2,3,4,5,6,7,8,9],shuffle=True)
    # dnet.zero_grad()
    dnet.zero_grad()
    gnet.zero_grad()
    data = torch.from_numpy(data).cuda()# pick data
    data_old = Variable(data)
    _,f1,_ = enet(data_old.detach()) # pick feature map
    g_f1 = model.get_feature(f1,feature_d, batch_size)
    g_sampler = Variable((torch.randn([batch_size,noise_d,hidden_d,hidden_d])-0.2)*0.5).cuda()
    g_f1_output = gnet(torch.cat([g_f1,g_sampler],1)).detach() # generate data
    # c_v = model.set_condition(g_f1.data, 1,batch_size=batch_size)
    c_v = Variable(torch.from_numpy(model.set_label_ve(label).astype('int'))).cuda()  # d_false_decision = dnet(torch.cat([g_f1_output, c_v], 1))# get false decision
    d_false_decision = dnet(torch.cat([g_f1_output,c_v],1))# get false decision

    d_false_error = criterion(d_false_decision, Variable(torch.zeros(batch_size)).cuda())

    data, label = mm.batch_next(batch_size, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], shuffle=True)
    data = Variable(torch.from_numpy(data).cuda())
    _, f1, _ = enet(data.detach())
    g_f1 = model.get_feature(f1,feature_d,batch_size)
    # c_v = model.set_condition(g_f1.data, 1, batch_size = batch_size)
    c_v = Variable(torch.from_numpy(model.set_label_ve(label).astype('float32'))).cuda()  # d_false_decision = dnet(torch.cat([g_f1_output, c_v], 1))# get false decision  # d_real_decision = dnet(torch.cat([data, c_v], 1))# get right decision
    d_real_decision = dnet(torch.cat([data,c_v],1))# get right decision
    d_real_error = criterion(d_real_decision,Variable(torch.ones(batch_size)).cuda())
    # D for fake data
    error = d_real_error + d_false_error
    error.backward()
示例#2
0
check_points = 500
num_epoches = 100000
criterion = nn.BCELoss()

for epoch in (range(1, num_epoches)):
    # print("give me a clue")
    data, label = mnist.train.next_batch(batch_size)
    dnet.zero_grad()
    gnet.zero_grad()

    data = Variable(torch.from_numpy(data)).cuda()
    data.data.resize_(batch_size, 1, 28, 28)

    # data, label = Variable(data), Variable(label)
    c_v = Variable(
        torch.from_numpy(model.set_label_ve(label).astype('float32'))).cuda()

    _, _, f2 = enet(data.detach())
    # f2 = torch.mean(f2,1)
    # print(np.shape(owntool.extract(f2)))
    g_f2 = f2.cuda()

    # D Part
    g_sampler = Variable(torch.randn([batch_size, 1, hidden_d,
                                      hidden_d])).cuda()
    # print(g_sampler.data.tolist()[0][0][0][0])
    g_f2_output = gnet(torch.cat([g_f2, g_sampler], 1)).detach()
    d_real_decision = dnet(data)
    d_real_error = criterion(d_real_decision,
                             Variable(torch.ones(batch_size)).cuda())
    # D for fake data
示例#3
0
zeros_label = Variable(torch.zeros(mb_size)).cuda()

criterion = nn.BCELoss()
criterion_r = nn.MarginRankingLoss(margin=0.1, size_average=False)

for it in range(100000):

    # Sample data
    z = Variable(torch.randn(mb_size, Z_dim)).cuda()
    X, c = mm.batch_next(mb_size)
    X = Variable(torch.from_numpy(X)).cuda()

    c_v = Variable(torch.from_numpy(
        model.set_label_ve_ma(c).astype('float32'))).cuda()  # label for g c
    c_t = Variable(torch.from_numpy(
        model.set_label_ve(c).astype('float32'))).cuda()  # label for d c(true)

    # Dicriminator forward-loss-backward-update
    D.zero_grad()
    G.zero_grad()
    # For D
    x_g = torch.cat([z, c_v], 1).t()
    x_g.data.resize_(mb_size, Z_dim + 10, 1, 1)

    G_sample = G(x_g).detach()
    X.data.resize_(mb_size, 1, 28, 28)
    D_real = D(X)
    D_fake = D(G_sample)

    D_loss_real = criterion(D_real, ones_label)
    D_loss_fake = criterion(D_fake, zeros_label)
示例#4
0
label_select = np.array([5, 6, 7, 8, 9])  # change
label_size = 5

for it in range(100000):

    # Sample data
    z = Variable(torch.randn(mb_size, Z_dim)).cuda()
    X, c = mm.batch_next(mb_size, label_select)
    X = Variable(torch.from_numpy(X.astype('float32'))).cuda()

    c_v = Variable(
        torch.from_numpy(
            model.set_label_ve_ma(c.astype('int')).astype('float32'))).cuda()
    c = Variable(
        torch.from_numpy(
            model.set_label_ve(c.astype('int')).astype('float32'))).cuda()

    # Dicriminator forward-loss-backward-update
    D.zero_grad()
    G.zero_grad()

    x_g = torch.cat([z, c_v], 1).t()
    x_g.data.resize_(mb_size, Z_dim + 10, 1, 1)

    G_sample = G(x_g)
    X.data.resize_(mb_size, 1, 28, 28)
    D_real = D(torch.cat([X, c], 1))
    D_fake = D(torch.cat([G_sample, c], 1))

    D_loss_real = criterion(D_real, ones_label)
    D_loss_fake = criterion(D_fake, zeros_label)
    for opt in D_solver_list:
        opt.step()


for it in range(100000):

    # Sample data
    z = Variable(torch.randn(mb_size, Z_dim)).cuda()
    X, c = mnist.train.next_batch(mb_size)
    X = Variable(torch.from_numpy(X)).cuda()
    label_m = np.nonzero(c)[1]
    c_v = Variable(torch.from_numpy(c.astype('float32'))).cuda()
    c_list = [
        Variable(
            torch.from_numpy(
                model.set_label_ve(
                    (label_m + i) % d_num).astype('float32'))).cuda()
        for i in range(d_num)
    ]

    # Dicriminator forward-loss-backward-update
    reset_d_grad()
    G.zero_grad()

    x_g = torch.cat([z, c_v], 1).t()
    x_g.data.resize_(mb_size, Z_dim + 10, 1, 1)

    G_sample = G(x_g).detach()
    X.data.resize_(mb_size, 1, 28, 28)

    D_real_list = [
        D_list[i](torch.cat([X, c_list[i]], 1)) for i in range(d_num)