Example #1
0
    def test_remote_var_binary_methods(self):
        ''' Unit tests for methods mentioned on issue 1385
            https://github.com/OpenMined/PySyft/issues/1385'''
        hook = TorchHook(verbose=False)
        local = hook.local_worker
        remote = VirtualWorker(hook, 1)
        local.add_worker(remote)

        x = Var(torch.FloatTensor([1, 2, 3, 4])).send(remote)
        y = Var(torch.FloatTensor([[1, 2, 3, 4]])).send(remote)
        z = torch.matmul(x, y.t())
        assert (torch.equal(z.get(), Var(torch.FloatTensor([30]))))
        z = torch.add(x, y)
        assert (torch.equal(z.get(), Var(torch.FloatTensor([[2, 4, 6, 8]]))))
        x = Var(torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])).send(remote)
        y = Var(torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])).send(remote)
        z = torch.cross(x, y, dim=1)
        assert (torch.equal(z.get(), Var(torch.FloatTensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]))))
        x = Var(torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])).send(remote)
        y = Var(torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])).send(remote)
        z = torch.dist(x, y)
        assert (torch.equal(z.get(), Var(torch.FloatTensor([0.]))))
        x = Var(torch.FloatTensor([1, 2, 3])).send(remote)
        y = Var(torch.FloatTensor([1, 2, 3])).send(remote)
        z = torch.dot(x, y)
        print(torch.equal(z.get(), Var(torch.FloatTensor([14]))))
        z = torch.eq(x, y)
        assert (torch.equal(z.get(), Var(torch.ByteTensor([1, 1, 1]))))
        z = torch.ge(x, y)
        assert (torch.equal(z.get(), Var(torch.ByteTensor([1, 1, 1]))))
Example #2
0
 def test_local_var_binary_methods(self):
     ''' Unit tests for methods mentioned on issue 1385
         https://github.com/OpenMined/PySyft/issues/1385'''
     x = torch.FloatTensor([1, 2, 3, 4])
     y = torch.FloatTensor([[1, 2, 3, 4]])
     z = torch.matmul(x, y.t())
     assert (torch.equal(z, torch.FloatTensor([30])))
     z = torch.add(x, y)
     assert (torch.equal(z, torch.FloatTensor([[2, 4, 6, 8]])))
     x = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
     y = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
     z = torch.cross(x, y, dim=1)
     assert (torch.equal(z, torch.FloatTensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]])))
     x = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
     y = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
     z = torch.dist(x, y)
     t = torch.FloatTensor([z])
     assert (torch.equal(t, torch.FloatTensor([0.])))
     x = torch.FloatTensor([1, 2, 3])
     y = torch.FloatTensor([1, 2, 3])
     z = torch.dot(x, y)
     t = torch.FloatTensor([z])
     assert torch.equal(t, torch.FloatTensor([14]))
     z = torch.eq(x, y)
     assert (torch.equal(z, torch.ByteTensor([1, 1, 1])))
     z = torch.ge(x, y)
     assert (torch.equal(z, torch.ByteTensor([1, 1, 1])))
def test(model):
    from scipy import misc
    img = misc.imread('lena_299.png')
    inputs = torch.ones(1,299,299,3)
    #inputs[0] = torch.from_numpy(img)

    inputs[0,0,0,0] = -1
    inputs.transpose_(1,3)
    inputs.transpose_(2,3)

    print(inputs.mean())
    print(inputs.std())

    #inputs.sub_(0.5).div_(0.5)
    #inputs.sub_(inputs)
    # 1, 3, 299, 299

    outputs = model.forward(torch.autograd.Variable(inputs))
    h5f = h5py.File('dump/InceptionResnetV2/Logits.h5', 'r')
    outputs_tf = torch.from_numpy(h5f['out'][()])
    h5f.close()
    outputs = torch.nn.functional.softmax(outputs)
    print(outputs.sum())
    print(outputs[0])
    print(outputs_tf.sum())
    print(outputs_tf[0])
    print(torch.dist(outputs.data, outputs_tf))
    return outputs
Example #4
0
def get_closest(word_embedding, word_to_idx, embeddings, n=5):
    """
    Get the n closest
    words to your word.
    """

    # Calculate distances to all other words
    distances = [(
        w, torch.dist(
            word_embedding, embeddings[word_to_idx[w]])) for w in word_to_idx]
    return sorted(distances, key=lambda x: x[1])[1:n+1]
Example #5
0
def test(model):
    model.eval()
    from scipy import misc
    img = misc.imread('lena_299.png')
    inputs = torch.zeros(1,299,299,3)
    inputs[0] = torch.from_numpy(img)
    inputs.transpose_(1,3)
    inputs.transpose_(2,3)
    # 1, 3, 299, 299
    outputs = model.forward(torch.autograd.Variable(inputs))
    h5f = h5py.File('dump/InceptionV4/Logits.h5', 'r')
    outputs_tf = torch.from_numpy(h5f['out'][()])
    h5f.close()
    outputs = torch.nn.functional.softmax(outputs)
    print(torch.dist(outputs.data, outputs_tf))
    return outputs
Example #6
0
    def test_remote_tensor_binary_methods(self):

        hook = TorchHook(verbose = False)
        local = hook.local_worker
        remote = VirtualWorker(hook, 0)
        local.add_worker(remote)

        x = torch.FloatTensor([1, 2, 3, 4, 5]).send(remote)
        y = torch.FloatTensor([1, 2, 3, 4, 5]).send(remote)
        assert (x.add_(y).get() == torch.FloatTensor([2,4,6,8,10])).all()

        x = torch.FloatTensor([1, 2, 3, 4]).send(remote)
        y = torch.FloatTensor([[1, 2, 3, 4]]).send(remote)
        z = torch.matmul(x, y.t())
        assert (torch.equal(z.get(), torch.FloatTensor([30])))

        z = torch.add(x, y)
        assert (torch.equal(z.get(), torch.FloatTensor([[2, 4, 6, 8]])))

        x = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]]).send(remote)
        y = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]]).send(remote)
        z = torch.cross(x, y, dim=1)
        assert (torch.equal(z.get(), torch.FloatTensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]])))

        x = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]]).send(remote)
        y = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]]).send(remote)
        z = torch.dist(x, y)
        t = torch.FloatTensor([z])
        assert (torch.equal(t, torch.FloatTensor([0.])))

        x = torch.FloatTensor([1, 2, 3]).send(remote)
        y = torch.FloatTensor([1, 2, 3]).send(remote)
        z = torch.dot(x, y)
        t = torch.FloatTensor([z])
        assert torch.equal(t, torch.FloatTensor([14]))

        z = torch.eq(x, y)
        assert (torch.equal(z.get(), torch.ByteTensor([1, 1, 1])))

        z = torch.ge(x, y)
        assert (torch.equal(z.get(), torch.ByteTensor([1, 1, 1])))
Example #7
0
    def test_local_tensor_binary_methods(self):
        ''' Unit tests for methods mentioned on issue 1385
        https://github.com/OpenMined/PySyft/issues/1385'''

        x = torch.FloatTensor([1, 2, 3, 4])
        y = torch.FloatTensor([[1, 2, 3, 4]])
        z = torch.matmul(x, y.t())
        assert (torch.equal(z, torch.FloatTensor([30])))

        z = torch.add(x, y)
        assert (torch.equal(z, torch.FloatTensor([[2, 4, 6, 8]])))

        x = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
        y = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
        z = torch.cross(x, y, dim=1)
        assert (torch.equal(z, torch.FloatTensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]])))

        x = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
        y = torch.FloatTensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
        z = torch.dist(x, y)
        assert (torch.equal(torch.FloatTensor([z]), torch.FloatTensor([0])))

        x = torch.FloatTensor([1, 2, 3])
        y = torch.FloatTensor([1, 2, 3])
        z = torch.dot(x, y)
        # There is an issue with some Macs getting 0.0 instead
        # Solved here: https://github.com/pytorch/pytorch/issues/5609
        assert torch.equal(torch.FloatTensor([z]), torch.FloatTensor([14]))

        z = torch.eq(x, y)
        assert (torch.equal(z, torch.ByteTensor([1, 1, 1])))

        z = torch.ge(x, y)
        assert (torch.equal(z, torch.ByteTensor([1, 1, 1])))

        x = torch.FloatTensor([1, 2, 3, 4, 5])
        y = torch.FloatTensor([1, 2, 3, 4, 5])
        assert (x.add_(y) == torch.FloatTensor([2, 4, 6, 8, 10])).all()
Example #8
0
def lk_target_loss(batch_locs, batch_scos, batch_next, batch_fbak, batch_back,
                   lk_config, video_or_not, mask, nopoints):
    # return the calculate target from the first frame to the whole sequence.
    batch, sequence, num_pts = lk_input_check(batch_locs, batch_scos,
                                              batch_next, batch_fbak,
                                              batch_back)

    # remove the background
    num_pts = num_pts - 1
    sequence_checks = np.ones((batch, num_pts), dtype='bool')

    # Check the confidence score for each point
    for ibatch in range(batch):
        if video_or_not[ibatch] == False:
            sequence_checks[ibatch, :] = False
        else:
            for iseq in range(sequence):
                for ipts in range(num_pts):
                    score = batch_scos[ibatch, iseq, ipts]
                    if mask[ibatch, ipts] == False and nopoints[ibatch] == 0:
                        sequence_checks[ibatch, ipts] = False
                    if score.item() < lk_config.conf_thresh:
                        sequence_checks[ibatch, ipts] = False

    losses = []
    for ibatch in range(batch):
        for ipts in range(num_pts):
            if not sequence_checks[ibatch, ipts]: continue
            loss = 0
            for iseq in range(sequence):

                targets = batch_locs[ibatch, iseq, ipts]
                nextPts = batch_next[ibatch, iseq, ipts]
                fbakPts = batch_fbak[ibatch, iseq, ipts]
                backPts = batch_back[ibatch, iseq, ipts]

                with torch.no_grad():
                    fbak_distance = torch.dist(nextPts, fbakPts)
                    back_distance = torch.dist(targets, backPts)
                    forw_distance = torch.dist(targets, nextPts)

                #print ('[{:02d},{:02d},{:02d}] : {:.2f}, {:.2f}, {:.2f}'.format(ibatch, ipts, iseq, fbak_distance.item(), back_distance.item(), forw_distance.item()))
                #loss += back_distance + forw_distance

                if fbak_distance.item(
                ) > lk_config.fb_thresh or fbak_distance.item(
                ) < lk_config.eps:  # forward-backward-check
                    if iseq + 1 < sequence:
                        sequence_checks[ibatch, ipts] = False
                if forw_distance.item(
                ) > lk_config.forward_max or forw_distance.item(
                ) < lk_config.eps:  # to avoid the tracker point is too far
                    if iseq > 0: sequence_checks[ibatch, ipts] = False
                if back_distance.item(
                ) > lk_config.forward_max or back_distance.item(
                ) < lk_config.eps:  # to avoid the tracker point is too far
                    if iseq + 1 < sequence:
                        sequence_checks[ibatch, ipts] = False

                if iseq > 0:
                    loss += torch.dist(targets, backPts.detach())
                if iseq + 1 < sequence:
                    loss += torch.dist(targets, nextPts.detach())

            if sequence_checks[ibatch, ipts]:
                losses.append(loss)

    avaliable = int(np.sum(sequence_checks))
    if avaliable == 0: return None, avaliable
    else: return torch.mean(torch.stack(losses)), avaliable
Example #9
0
def wachter_recourse(
    torch_model,
    x: np.ndarray,
    cat_feature_indices: List[int],
    binary_cat_features: bool,
    feature_costs: Optional[List[float]],
    lr: float,
    lambda_param: float,
    y_target: List[int],
    n_iter: int,
    t_max_min: float,
    norm: int,
    clamp: bool,
    loss_type: str,
) -> np.ndarray:
    """
    Generates counterfactual example according to Wachter et.al for input instance x

    Parameters
    ----------
    torch_model:
        black-box-model to discover
    x:
        Factual instance to explain.
    cat_feature_indices:
        List of positions of categorical features in x.
    binary_cat_features:
        If true, the encoding of x is done by drop_if_binary.
    feature_costs:
        List with costs per feature.
    lr:
        Learning rate for gradient descent.
    lambda_param:
        Weight factor for feature_cost.
    y_target:
        Tuple of class probabilities (BCE loss) or [Float] for logit score (MSE loss).
    n_iter:
        Maximum number of iterations.
    t_max_min:
        Maximum time amount of search.
    norm:
        L-norm to calculate cost.
    clamp:
        If true, feature values will be clamped to intverval [0, 1].
    loss_type:
        String for loss function ("MSE" or "BCE").

    Returns
    -------
    Counterfactual example as np.ndarray
    """
    device = "cuda" if torch.cuda.is_available() else "cpu"
    # returns counterfactual instance
    torch.manual_seed(0)

    if feature_costs is not None:
        feature_costs = torch.from_numpy(feature_costs).float().to(device)

    x = torch.from_numpy(x).float().to(device)
    y_target = torch.tensor(y_target).float().to(device)
    lamb = torch.tensor(lambda_param).float().to(device)
    # x_new is used for gradient search in optimizing process
    x_new = Variable(x.clone(), requires_grad=True)
    # x_new_enc is a copy of x_new with reconstructed encoding constraints of x_new
    # such that categorical data is either 0 or 1
    x_new_enc = reconstruct_encoding_constraints(x_new, cat_feature_indices,
                                                 binary_cat_features)

    optimizer = optim.Adam([x_new], lr, amsgrad=True)

    if loss_type == "MSE":
        if len(y_target) != 1:
            raise ValueError(
                f"y_target {y_target} is not a single logit score")

        # If logit is above 0.0 we want class 1, else class 0
        target_class = int(y_target[0] > 0.0)
        loss_fn = torch.nn.MSELoss()
    elif loss_type == "BCE":
        if y_target[0] + y_target[1] != 1.0:
            raise ValueError(
                f"y_target {y_target} does not contain 2 valid class probabilities"
            )

        # [0, 1] for class 1, [1, 0] for class 0
        # target is the class probability of class 1
        # target_class is the class with the highest probability
        target_class = torch.round(y_target[1]).int()
        loss_fn = torch.nn.BCELoss()
    else:
        raise ValueError(f"loss_type {loss_type} not supported")

    # get the probablity of the target class
    f_x_new = torch_model(x_new)[:, target_class]

    t0 = datetime.datetime.now()
    t_max = datetime.timedelta(minutes=t_max_min)
    while f_x_new <= DECISION_THRESHOLD:
        it = 0
        while f_x_new <= 0.5 and it < n_iter:
            optimizer.zero_grad()
            x_new_enc = reconstruct_encoding_constraints(
                x_new, cat_feature_indices, binary_cat_features)
            # use x_new_enc for prediction results to ensure constraints
            # get the probablity of the target class
            f_x_new = torch_model(x_new_enc)[:, target_class]

            if loss_type == "MSE":
                # single logit score for the target class for MSE loss
                f_x_loss = torch.log(f_x_new / (1 - f_x_new))
            elif loss_type == "BCE":
                # tuple output for BCE loss
                f_x_loss = torch_model(x_new_enc).squeeze(axis=0)
            else:
                raise ValueError(f"loss_type {loss_type} not supported")

            cost = (torch.dist(x_new_enc, x, norm) if feature_costs is None
                    else torch.norm(feature_costs * (x_new_enc - x), norm))

            loss = loss_fn(f_x_loss, y_target) + lamb * cost
            loss.backward()
            optimizer.step()
            # clamp potential CF
            if clamp:
                x_new.clone().clamp_(0, 1)
            it += 1
        lamb -= 0.05

        if datetime.datetime.now() - t0 > t_max:
            log.info("Timeout - No Counterfactual Explanation Found")
            break
        elif f_x_new >= 0.5:
            log.info("Counterfactual Explanation Found")
    return x_new_enc.cpu().detach().numpy().squeeze(axis=0)
Example #10
0
 def test_dist(self, input, output):
     print(name, 'conv+bias', torch.dist(output.data, output_tf))
Example #11
0
 def test_dist_conv(self, input, output):
     print(name, 'conv', torch.dist(output.data, output_tf_conv))
Example #12
0
def lp_distance(x1, x2, p):
    dist = torch.dist(x1, x2, p)
    return dist
Example #13
0
def test_init(dist):
    model = dist()
    assert model.sample(1).shape[0] == 1
    assert model.sample(64).shape[0] == 64
    assert model.log_prob(model.sample(4)).shape[0] == 4
    model.get_parameters()
Example #14
0
        real_loss = adversarial_loss(discriminator(real_imgs), valid)
        fake_loss = adversarial_loss(discriminator(gen_imgs.detach()), fake)
        d_loss = (real_loss + fake_loss) / 2

        d_loss.backward()
        optimizer_D.step()

        # ---------------------
        #  Train Cleaner
        # ---------------------
        for clean_iter in clean_iters:
            optimizer_C.zero_grad()

            cleaned_noisy = Variable(cleaner(gen_imgs), requires_grad=True)
            cleaned_clean = Variable(cleaner(estimated), requires_grad=True)
            noisy_loss = torch.dist(cleaned_noisy, estimated)
            clean_loss = torch.dist(cleaned_clean, estimated)

            cleaner_loss = noisy_loss + clean_loss
            # print("cleaner loss", cleaner_loss)
            cleaner_loss.backward()
            optimizer_C.step()
        iter += 1
        if iter % print_iter == 0:
            print(
                "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f] [C Loss: %f]"
                % (epoch, n_epochs, i, len(real_eegs), d_loss.item(),
                   g_loss.item(), clean_loss.item()))
        print("cleaner loss", clean_loss)
    save_EEG(gen_imgs.cpu().detach().view(batch_size, 1004,
                                          44).numpy(), 44, 200,
def loss_fn2(genFGen2, args, model):
    #print(list(model.parameters()))

    #logger.info(model)
    #logger.info("Number of trainable parameters: {}".format(count_parameters(model)))

    #with torch.no_grad():
    #    model.eval()
    #
    #    p_samples = toy_data.inf_train_gen(args.data, batch_size=20000)
    #    sample_fn, density_fn = model.inverse, model.forward
    #
    #    plt.figure(figsize=(9, 3))
    #    visualize_transform(p_samples, torch.randn, standard_normal_logprob, transform=sample_fn,
    #                        inverse_transform=density_fn, samples=True, npts=400, device=device)
    #    plt.show()
    #    plt.close()
    #
    #    plt.figure(figsize=(4, 2))
    #    visualize_transform2(p_samples, torch.randn, standard_normal_logprob, transform=sample_fn,
    #                        inverse_transform=density_fn, samples=True, npts=400, device=device)
    #    plt.show()
    #    plt.close()

    #xData = toy_data.inf_train_gen(args.data, batch_size=args.batch_size)
    #xData = torch.from_numpy(xData).type(torch.float32).to(device)

    #second_term_loss = torch.from_numpy(np.array(0.0, dtype='float32')).to(device)
    #for i in genFGen2:
    #    for j in xData:
    #        # second_term_loss += np.linalg.norm(i.cpu().detach().numpy()-j.cpu().detach().numpy())
    #
    #        # second_term_loss += torch.norm(i-j, 2)
    #        # second_term_loss += torch.norm(i-j)
    #
    #        # second_term_loss += torch.dist(i.type(torch.float64), j.type(torch.float64), 2)
    #        second_term_loss += torch.dist(i, j, 2)
    #    second_term_loss /= args.batch_size
    #    #second_term_loss *= 0.1
    #second_term_loss /= args.batch_size

    #print('')
    #print(second_term_loss)

    #second_term_loss = torch.from_numpy(np.array([], dtype='float32')).to(device)
    #for i in genFGen2:
    #    second_term_lossTwo = torch.from_numpy(np.array([], dtype='float32')).to(device)
    #    for j in xData:
    #        second_term_lossTwo = torch.cat((second_term_lossTwo, torch.dist(i, j, 2)), 0)
    #    second_term_loss = torch.cat((second_term_loss, torch.mean(second_term_lossTwo, 0)), 0)
    #    #second_term_loss *= 0.1
    #second_term_loss = torch.mean(second_term_loss)

    #print('')
    #print(second_term_loss)

    #genFGen3 = torch.randn((args.batch_size, 2)).to(device)
    #first_term_loss = compute_loss2(genFGen2, args, model, beta=beta)

    #print('')
    #print(first_term_loss)

    import math
    mu = torch.from_numpy(np.array([2.805741, -0.00889241], dtype="float32")).to(device)
    S = torch.from_numpy(np.array([[pow(0.3442525,2), 0.0], [0.0, pow(0.35358343,2)]], dtype="float32")).to(device)

    storeAll = torch.from_numpy(np.array(0.0, dtype="float32")).to(device)
    for loopIndex_i in range(genFGen2.size()[0]):
        toUse_storeAll = torch.distributions.MultivariateNormal(loc=mu, covariance_matrix=S)
        storeAll += toUse_storeAll.log_prob(genFGen2[loopIndex_i:1+loopIndex_i, :].squeeze(0))
    storeAll /= genFGen2.size()[0]

    #print(storeAll)
    #print('')

    first_term_loss = storeAll

    xData = toy_data.inf_train_gen(args.data, batch_size=args.batch_size)
    xData = torch.from_numpy(xData).type(torch.float32).to(device)

    var2 = []
    for i in genFGen2:
        var1 = []
        for j in xData:
            new_stuff = torch.dist(i, j, 2)  # this is a tensor
            var1.append(new_stuff.unsqueeze(0))
        var1_tensor = torch.cat(var1)
        second_term_loss2 = torch.min(var1_tensor) / args.batch_size
        var2.append(second_term_loss2.unsqueeze(0))
    var2_tensor = torch.cat(var2)
    second_term_loss = torch.mean(var2_tensor) / args.batch_size

    first_term_loss = torch.exp(first_term_loss)
    second_term_loss *= 10.0

    print('')
    print(first_term_loss)
    print(second_term_loss)

    #third_term_loss = torch.from_numpy(np.array(0.0, dtype='float32')).to(device)
    #for i in range(args.batch_size):
    #    for j in range(args.batch_size):
    #        if i != j:
    #            # third_term_loss += ((np.linalg.norm(genFGen3[i,:].cpu().detach().numpy()-genFGen3[j,:].cpu().detach().numpy())) / (np.linalg.norm(genFGen2[i,:].cpu().detach().numpy()-genFGen2[j,:].cpu().detach().numpy())))
    #
    #            # third_term_loss += ((torch.norm(genFGen3[i,:]-genFGen3[j,:], 2)) / (torch.norm(genFGen2[i,:]-genFGen2[j,:], 2)))
    #            # third_term_loss += ((torch.norm(genFGen3[i,:]-genFGen3[j,:])) / (torch.norm(genFGen2[i,:]-genFGen2[j,:])))
    #
    #            # third_term_loss += ((torch.norm(genFGen3[i,:] - genFGen3[j,:])) / (torch.norm(genFGen2[i,:] - genFGen2[j,:])))
    #            third_term_loss += ((torch.dist(genFGen3[i, :], genFGen3[j, :], 2)) / (torch.dist(genFGen2[i, :], genFGen2[j, :], 2)))
    #    third_term_loss /= (args.batch_size - 1)
    #third_term_loss /= args.batch_size
    ##third_term_loss *= 1000.0

    #print(third_term_loss)
    #print('')

    print('')
    #asdfsfa

    #return first_term_loss + second_term_loss + third_term_loss
    return first_term_loss + second_term_loss
Example #16
0
def closest(vec, n=10):
    """
    Find the closest words for a given vector
    """
    all_dists = [(w, torch.dist(vec, get_word(w))) for w in glove.itos]
    return sorted(all_dists, key=lambda t: t[1])[:n]
def train(train_loader, target_train_loader, model, criterion, optimizer, epoch):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    # switch to train mode
    model.train()

    end = time.time()
    current_LR = get_learning_rate(optimizer)[0]
    extra_iterator = iter(target_train_loader)
    t = tqdm(train_loader, desc='Train %d' % epoch)
    for i, (input, target) in enumerate(t):
        # measure data loading time
        data_time.update(time.time() - end)
        try:
            (target_input, target_target) = next(extra_iterator)
        except StopIteration:
            extra_iterator = iter(target_train_loader)
            (target_input, target_target) = next(extra_iterator)
        input = torch.cat([input, target_input])
        target = torch.cat([target, target_target])
        input = input.cuda()
        target = target.cuda()
        target_copy = target_target.cpu().numpy()

        r = np.random.rand(1)
        if args.beta > 0 and r < args.cutmix_prob:
            # generate mixed sample

            lam = np.random.beta(args.beta, args.beta)
            rand_index = torch.randperm(input.size()[0]).cuda()
            target_a = target
            target_b = target[rand_index]
            bbx1, bby1, bbx2, bby2 = rand_bbox(input.size(), lam)
            input[:, :, bbx1:bbx2, bby1:bby2] = input[rand_index,
                                                      :, bbx1:bbx2, bby1:bby2]
            # adjust lambda to exactly match pixel ratio
            lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) /
                       (input.size()[-1] * input.size()[-2]))
            # compute output
            output = model(input)
            loss = criterion(output, target_a).mean() * lam + \
                criterion(output, target_b).mean() * (1. - lam)
            id3 = []
            id5 = []

            for j in range(len(input)):
                if (target_copy[j]) == args.first:
                    id3.append(j)
                elif (target_copy[j]) == args.second:
                    id5.append(j)

            m = nn.Softmax(dim=1)

            p_dist = torch.dist(torch.mean(
                m(output)[id3], 0), torch.mean(m(output)[id5], 0), 2)

            p_dist = 0
            loss2 = loss - args.lam * p_dist
        else:
            # compute output
            output = model(input)
            loss2 = criterion(output[:output.size(
                0) // 2], target[:target.size(0) // 2]).mean()  # - args.lam*p_dist

        losses.update(loss2.item(), input.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss2.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
        t.set_postfix(loss=losses.avg)

        if i % args.print_freq == 0 and args.verbose == True:
            print('Epoch: [{0}/{1}][{2}/{3}]\t'
                  'LR: {LR:.6f}\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  'Top 1-err {top1.val:.4f} ({top1.avg:.4f})\t'
                  'Top 5-err {top5.val:.4f} ({top5.avg:.4f})'.format(
                      epoch, args.epochs, i, len(train_loader), LR=current_LR, batch_time=batch_time,
                      data_time=data_time, loss=losses, top1=top1, top5=top5))

    print('* Epoch: [{0}/{1}]\t Top 1-err {top1.avg:.3f}  Top 5-err {top5.avg:.3f}\t Train Loss {loss.avg:.3f}'.format(
        epoch, args.epochs, top1=top1, top5=top5, loss=losses))

    return losses.avg
Example #18
0
def Analogical_Reasoning_Task(embedding, w2i, i2w):
    #######################  Input  #########################
    # embedding : Word embedding (type:torch.tesnor(V,D))   #
    #########################################################
    start_time = time.time()
    embedding = torch.tensor(embedding)
    # print(w2i)
    f = open("questions-words.txt", 'r')
    g = open("result.txt", 'w')
    total_question = 0.0
    total_correct = 0.0
    N = embedding.shape[0]

    vector = {}
    for word in w2i:
        vector[word] = embedding[w2i[word]]

    l2_norm = {}
    for word in w2i:
        l2_norm[word] = torch.dist(vector[word],
                                   torch.zeros_like(vector[word]), 2)

    num_questions = 0

    while True:
        line = f.readline()
        if not line: break
        if line[0] == ':': continue
        Qwords = line.split()

        if num_questions % 100 == 0:
            print(str(num_questions) + " read ")
            num_questions += 1
        else:
            num_questions += 1

        if Qwords[0].lower() in w2i and Qwords[1].lower(
        ) in w2i and Qwords[2].lower() in w2i and Qwords[3].lower() in w2i:
            total_question += 1.0
            word_1_vec = vector[Qwords[0].lower()]
            word_2_vec = vector[Qwords[1].lower()]
            word_3_vec = vector[Qwords[2].lower()]
            x_vector = word_2_vec - word_1_vec + word_3_vec
            x_vector = torch.tensor(x_vector)
            x_vector_l2_norm = torch.dist(x_vector, torch.zeros_like(x_vector),
                                          2)
            best_similarity = -1.0
            best_idx = None
            for word in w2i:
                word_idx = w2i[word]
                wordvec = vector[word]
                similarity = torch.dot(
                    x_vector, wordvec) / (x_vector_l2_norm * l2_norm[word])
                if similarity > best_similarity and word_idx != w2i[
                        Qwords[0].lower()] and word_idx != w2i[Qwords[1].lower(
                        )] and word_idx != w2i[Qwords[2].lower()]:
                    # if similarity > best_similarity # 문제에 나온 단어들도 포함시키고 싶으면 위 조건문을 주석처리하고 이 문장을 사용
                    best_similarity = similarity
                    best_idx = word_idx
            # print(best_similarity)
            if Qwords[3].lower() == i2w[best_idx].lower():
                g.write("%s %s : %s %s?, Correct !!! \n" %
                        (Qwords[0], Qwords[1], Qwords[2],
                         i2w[best_idx].capitalize()))
                total_correct += 1.0
            else:
                g.write("%s %s : %s %s?, Wrong !!! Answer is %s \n" %
                        (Qwords[0], Qwords[1], Qwords[2],
                         i2w[best_idx].capitalize(), Qwords[3]))
    g.write(
        "Questions = %d, Correct Questions = %d, Hitting Rate = %.4f%% \n" %
        (total_question, total_correct,
         (total_correct / total_question) * 100.0))
    end_time = time.time()
    time_elapsed = end_time - start_time
    g.write("Time Elapsed for Validaiton %02d:%02d:%02d\n" %
            (time_elapsed // 3600, (time_elapsed % 3600 // 60),
             (time_elapsed % 60 // 1)))

    f.close()
    g.close()
    print('Questions = %d, Correct Questions = %d, Hitting Rate = %.4f' %
          (total_question, total_correct,
           (total_correct / total_question) * 100.0))
    print('Time Elapsed for Validaiton %02d:%02d:%02d' %
          (time_elapsed // 3600, (time_elapsed % 3600 // 60),
           (time_elapsed % 60 // 1)))
Example #19
0
 def center_distance_from_point(self, point: torch.Tensor) -> torch.Tensor:
     return torch.dist(self.center, point, 2)
def snr(x, y):

    return 10 * math.log10(
        pow(torch.norm(y, p=2), 2) / pow(torch.dist(x, y, p=2), 2))
Example #21
0
###############################################################################
# The other thing that we notice is that, if we print the parameters, we see that the
# parameter ``weight`` has been moved
print(dict(layer.named_parameters()))

###############################################################################
# It now sits under ``layer.parametrizations.weight.original``
print(layer.parametrizations.weight.original)

###############################################################################
# Besides these three small differences, the parametrization is doing exactly the same
# as our manual implementation
symmetric = Symmetric()
weight_orig = layer.parametrizations.weight.original
print(torch.dist(layer.weight, symmetric(weight_orig)))


###############################################################################
# Parametrizations are first-class citizens
# -----------------------------------------
#
# Since ``layer.parametrizations`` is an ``nn.ModuleList``, it means that the parametrizations
# are properly registered as submodules of the original module. As such, the same rules
# for registering parameters in a module apply to register a parametrization.
# For example, if a parametrization has parameters, these will be moved from CPU
# to CUDA when calling ``model = model.cuda()``.
#
# Caching the value of a parametrization
# --------------------------------------
#
Example #22
0
def euclidian_dist(features1, features2, p=1):
    return torch.dist(features1,features2, p=p)
Example #23
0
        self.log_b.data = vec[1:]

    def prior(self, vec):
        # return smp.normal(vec[:1], 0, 0.25) + smp.normal(vec[1:], 0, 0.25)
        return np.sum(
            np.log(stats.norm.pdf(vec[:1], 0, 0.01) + 0.5 / self.log_a_max)
        ) + np.sum(
            np.log(stats.norm.pdf(vec[1:], 0, 0.01) + 0.5 / self.log_b_max))

    def forward(self, input):
        a = torch.exp(self.log_a)
        b = torch.exp(self.log_b)
        max_value = self.max_input.type_as(input)
        return self.max_input.type_as(input) * (
            1 - (1 - (input / max_value).clamp(min=0, max=1)**a)**b)


if __name__ == '__main__':
    from HyperSphere.feature_map.functionals import phi_reflection_lp
    n = 10
    dim = 10
    input = Variable(torch.FloatTensor(n, dim).uniform_(-1, 1))
    feature_map = Kumaraswamy()
    feature_map.reset_parameters()
    print(torch.exp(feature_map.log_p_minus_one.data)[0] + 1)
    output1 = feature_map(input)
    output2 = phi_reflection_lp(
        input,
        torch.exp(feature_map.log_p_minus_one.data)[0] + 1)
    print(torch.dist(output1, output2))
Example #24
0
 def test_dist_relu(self, input, output):
     print(name, 'relu', torch.dist(output.data, output_tf_relu))
Example #25
0
    def compute_AAN_loss(self, input, source, target):
        M_o = []
        M_s = []
        G_o = []
        G_t = []
        L = ['conv1', 'res2c', 'res3d', 'res4f', 'res5c']
        # model_ft.layer1[0].conv2.register_forward_hook(hook_fn)
        self.models["resnet50"].conv1.register_forward_hook(
            get_activation('conv1'))  # conv1
        self.models["resnet50"].layer1[2].conv3.register_forward_hook(
            get_activation('res2c'))  # res2c
        self.models["resnet50"].layer2[3].conv3.register_forward_hook(
            get_activation('res3d'))  # res3d
        self.models["resnet50"].layer3[5].conv3.register_forward_hook(
            get_activation('res4f'))  # res4f
        self.models["resnet50"].layer4[2].conv3.register_forward_hook(
            get_activation('res5c'))  # res5c
        output = self.models["resnet50"](input)

        for layer in L:
            M_o.append(activation[layer])
            G_o.append(self.generate_style_image(activation[layer]))
            del activation[layer]

        self.models["resnet50"].conv1.register_forward_hook(
            get_activation('conv1'))  # conv1
        self.models["resnet50"].layer1[2].conv3.register_forward_hook(
            get_activation('res2c'))  # res2c
        self.models["resnet50"].layer2[3].conv3.register_forward_hook(
            get_activation('res3d'))  # res3d
        self.models["resnet50"].layer3[5].conv3.register_forward_hook(
            get_activation('res4f'))  # res4f
        self.models["resnet50"].layer4[2].conv3.register_forward_hook(
            get_activation('res5c'))  # res5c
        output = self.models["resnet50"](source)

        for layer in L:
            M_s.append(activation[layer])
            # print("activation layer", layer, activation[layer])
            del activation[layer]

        self.models["resnet50"].conv1.register_forward_hook(
            get_activation('conv1'))  # conv1
        self.models["resnet50"].layer1[2].conv3.register_forward_hook(
            get_activation('res2c'))  # res2c
        self.models["resnet50"].layer2[3].conv3.register_forward_hook(
            get_activation('res3d'))  # res3d
        self.models["resnet50"].layer3[5].conv3.register_forward_hook(
            get_activation('res4f'))  # res4f
        self.models["resnet50"].layer4[2].conv3.register_forward_hook(
            get_activation('res5c'))  # res5c
        output = self.models["resnet50"](target)

        for layer in L:
            # G_t.append(activation[layer])
            G_t.append(self.generate_style_image(activation[layer]))
            del activation[layer]

        alpha = 1e-14
        loss = torch.tensor(0, device=self.device)
        for i in range(len(M_o)):
            loss = loss + torch.dist(
                M_o[i], M_s[i], 2) + alpha * torch.dist(G_o[i], G_t[i], 2)

        # test_loss = Variable(loss, requires_grad=True)
        return loss
Example #26
0
def loss_fn2(genFGen2, args, model):
    first_term_loss = compute_loss2(genFGen2, args, model)
    #first_term_loss2 = compute_loss2(genFGen2, args, model)
    #first_term_loss = torch.log(first_term_loss2 / (1.0 - first_term_loss2))

    #print('')
    #print(first_term_loss)

    #mu = torch.from_numpy(np.array([2.805741, -0.00889241], dtype="float32")).to(device)
    #S = torch.from_numpy(np.array([[pow(0.3442525,2), 0.0], [0.0, pow(0.35358343,2)]], dtype="float32")).to(device)

    #storeAll = torch.from_numpy(np.array(0.0, dtype="float32")).to(device)
    #toUse_storeAll = torch.distributions.MultivariateNormal(loc=mu, covariance_matrix=S)
    #for loopIndex_i in range(genFGen2.size()[0]):
    #    storeAll += torch.exp(toUse_storeAll.log_prob(genFGen2[loopIndex_i:1 + loopIndex_i, :].squeeze(0)))
    #storeAll /= genFGen2.size()[0]

    #print(storeAll)
    #print('')

    #print('')
    #print(compute_loss2(mu.unsqueeze(0), args, model))

    #print(torch.exp(toUse_storeAll.log_prob(mu)))
    #print('')

    #first_term_loss = storeAll

    xData = toy_data.inf_train_gen(args.data, batch_size=args.batch_size)
    xData = torch.from_numpy(xData).type(torch.float32).to(device)

    #var2 = []
    #for i in genFGen2:
    #    var1 = []
    #    for j in xData:
    #        new_stuff = torch.dist(i, j, 2)  # this is a tensor
    #        var1.append(new_stuff.unsqueeze(0))
    #    var1_tensor = torch.cat(var1)
    #    second_term_loss2 = torch.min(var1_tensor) / args.batch_size
    #    var2.append(second_term_loss2.unsqueeze(0))
    #var2_tensor = torch.cat(var2)
    #second_term_loss = torch.mean(var2_tensor) / args.batch_size
    #second_term_loss *= 100.0

    #print('')
    #print(second_term_loss)

    # If you know in advance the size of the final tensor, you can allocate
    # an empty tensor beforehand and fill it in the for loop.

    #x = torch.empty(size=(len(items), 768))
    #for i in range(len(items)):
    #    x[i] = calc_result

    #print(len(genFGen2))
    #print(genFGen2.shape[0])
    # len(.) and not .shape[0]

    #print(len(xData))
    #print(xData.shape[0])
    # Use len(.) and not .shape[0]
    """
    #second_term_loss = torch.empty(size=(len(genFGen2), len(xData))).to(device)
    #second_term_loss = torch.empty(size=(len(genFGen2), len(xData)), device=device, requires_grad=True)
    #second_term_loss3 = torch.empty(size=(len(genFGen2), len(xData)), device=device, requires_grad=True)
    #second_term_loss3 = torch.empty(size=(len(genFGen2), len(xData)), device=device, requires_grad=False)
    second_term_loss3 = torch.empty(size=(args.batch_size, args.batch_size), device=device, requires_grad=False)
    #for i in range(len(genFGen2)):
    for i in range(args.batch_size):
        #for j in range(len(xData)):
        for j in range(args.batch_size):
            #second_term_loss[i, j] = torch.dist(genFGen2[i,:], xData[j,:], 2)
            #second_term_loss[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1)
            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1)

            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1)
            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1)

            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1)
            #second_term_loss3[i, j] = torch.tensor(0.1, requires_grad=True)

            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1)
            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1).requires_grad_()

            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 1).requires_grad_()
            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 2).requires_grad_()**2

            #second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 2).requires_grad_()**2
            second_term_loss3[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 2).requires_grad_()

            #second_term_loss[i, j] = torch.dist(genFGen2[i, :], xData[j, :], 2)**2
    #second_term_loss2, _ = torch.min(second_term_loss, 1)
    second_term_loss2, _ = torch.min(second_term_loss3, 1)
    #second_term_loss = 5000.0 * torch.mean(second_term_loss2) / (args.batch_size**2)
    #second_term_loss = lambda1 * torch.mean(second_term_loss2) / (args.batch_size ** 2)
    #second_term_loss = lambda1 * torch.mean(second_term_loss2)
    second_term_loss = torch.mean(second_term_loss2)

    #print(second_term_loss)
    #print('')

    print('')
    print(first_term_loss)
    print(second_term_loss)

    print('')
    """

    second_term_loss32 = torch.empty(args.batch_size,
                                     device=device,
                                     requires_grad=False)
    for i in range(args.batch_size):
        #second_term_loss22 = torch.norm(genFGen2[i, :] - xData, p='fro', dim=1).requires_grad_()
        second_term_loss22 = torch.norm(genFGen2[i, :] - xData, p=None,
                                        dim=1).requires_grad_()
        #print(second_term_loss22.shape)
        second_term_loss32[i] = torch.min(second_term_loss22)
    #print(second_term_loss32)
    #print(second_term_loss32.shape)
    #print(torch.norm(genFGen2 - xData, p=None, dim=0).shape)
    #second_term_loss22 = torch.min(second_term_loss32)
    #print(second_term_loss22)
    #print(second_term_loss22.shape)
    second_term_loss2 = torch.mean(second_term_loss32)
    #print(second_term_loss2)
    #print(second_term_loss2.shape)

    #print('')

    #print(second_term_loss)
    #print(second_term_loss2)

    print('')
    print(first_term_loss)
    print(second_term_loss2)

    #third_term_loss = torch.from_numpy(np.array(0.0, dtype='float32')).to(device)
    #for i in range(args.batch_size):
    #    for j in range(args.batch_size):
    #        if i != j:
    #            # third_term_loss += ((np.linalg.norm(genFGen3[i,:].cpu().detach().numpy()-genFGen3[j,:].cpu().detach().numpy())) / (np.linalg.norm(genFGen2[i,:].cpu().detach().numpy()-genFGen2[j,:].cpu().detach().numpy())))
    #
    #            # third_term_loss += ((torch.norm(genFGen3[i,:]-genFGen3[j,:], 2)) / (torch.norm(genFGen2[i,:]-genFGen2[j,:], 2)))
    #            # third_term_loss += ((torch.norm(genFGen3[i,:]-genFGen3[j,:])) / (torch.norm(genFGen2[i,:]-genFGen2[j,:])))
    #
    #            # third_term_loss += ((torch.norm(genFGen3[i,:] - genFGen3[j,:])) / (torch.norm(genFGen2[i,:] - genFGen2[j,:])))
    #            third_term_loss += ((torch.dist(genFGen3[i, :], genFGen3[j, :], 2)) / (torch.dist(genFGen2[i, :], genFGen2[j, :], 2)))
    #    third_term_loss /= (args.batch_size - 1)
    #third_term_loss /= args.batch_size
    ##third_term_loss *= 1000.0

    genFGen3 = torch.randn([args.batch_size, 2],
                           device=device,
                           requires_grad=True)
    #third_term_loss = torch.from_numpy(np.array(0.0, dtype='float32')).to(device)
    third_term_loss3 = torch.empty(size=(args.batch_size, args.batch_size),
                                   device=device,
                                   requires_grad=False)
    for i in range(args.batch_size):
        for j in range(args.batch_size):
            if i != j:
                # third_term_loss += ((np.linalg.norm(genFGen3[i,:].cpu().detach().numpy()-genFGen3[j,:].cpu().detach().numpy())) / (np.linalg.norm(genFGen2[i,:].cpu().detach().numpy()-genFGen2[j,:].cpu().detach().numpy())))

                # third_term_loss += ((torch.norm(genFGen3[i,:]-genFGen3[j,:], 2)) / (torch.norm(genFGen2[i,:]-genFGen2[j,:], 2)))
                # third_term_loss += ((torch.norm(genFGen3[i,:]-genFGen3[j,:])) / (torch.norm(genFGen2[i,:]-genFGen2[j,:])))

                # third_term_loss += ((torch.norm(genFGen3[i,:] - genFGen3[j,:])) / (torch.norm(genFGen2[i,:] - genFGen2[j,:])))
                #third_term_loss += ((torch.dist(genFGen3[i, :], genFGen3[j, :], 2)) / (torch.dist(genFGen2[i, :], genFGen2[j, :], 2)))

                #third_term_loss += ((torch.dist(genFGen3[i, :], genFGen3[j, :], 2)) / (torch.dist(genFGen2[i, :], genFGen2[j, :], 2)))
                #third_term_loss3[i][j] = ((torch.dist(genFGen3[i, :], genFGen3[j, :], 2).requires_grad_()) / (torch.dist(genFGen2[i, :], genFGen2[j, :], 2).requires_grad_()))

                third_term_loss3[i][j] = (
                    (torch.dist(genFGen3[i, :], genFGen3[j, :],
                                2).requires_grad_()) /
                    (torch.dist(genFGen2[i, :], genFGen2[j, :],
                                2).requires_grad_()))
    #third_term_loss /= (args.batch_size - 1)
    #third_term_loss2 = third_term_loss3 / (args.batch_size - 1)
    third_term_loss2 = torch.mean(third_term_loss3, 1)
    #third_term_loss /= args.batch_size
    #third_term_loss = third_term_loss2 / args.batch_size
    #third_term_loss = torch.mean(third_term_loss2)
    #third_term_loss = 0.01 * torch.mean(third_term_loss2)
    #third_term_loss = lambda2 * torch.mean(third_term_loss2)
    third_term_loss = torch.mean(third_term_loss2)
    #third_term_loss *= 1000.0

    print(third_term_loss)
    print('')

    #print('')
    #asdfsfa

    #return first_term_loss + second_term_loss + third_term_loss
    #return first_term_loss + second_term_loss

    #return second_term_loss
    #return first_term_loss + second_term_loss
    #return first_term_loss + second_term_loss + third_term_loss

    #return first_term_loss + second_term_loss + third_term_loss
    return first_term_loss + second_term_loss2 + third_term_loss
Example #27
0
def train_pose_vel(model: torch.nn.Module, training_generator: DataLoader,
                   test_generator: DataLoader, num_epochs: int, device: torch.device,
                   lr: Union[int, float] = 0.02, limit: Union[int, float] = 10e7, validate: bool = True, logging=False):
    """

    :param logging:
    :param validate:
    :param model: model for predicting the poses.  input shape are [numped, history, 2]
    :param training_generator:  torch training generator to get data
    :param test_generator: torch training generator to get data
    :param num_epochs: number of epochs to train
    :param device: torch device. (cpu/cuda)
    :param lr: learning rate
    :param limit: limit the number of training batches
    :return:
    """

    optimizer = optim.Adam(model.parameters(), lr=lr)
    drop_every_epochs = 1
    drop_rate = 0.95
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, drop_every_epochs,
                                                drop_rate)  # drop_every_epochs epochs drop by drop_rate lr
    writer = None
    dir_path = os.path.dirname(os.path.realpath(__file__))
    if logging:
        writer, experiment_name, save_model_path, folder_path = setup_experiment(model.name + str(lr) + "_hd_"
                                                                                 + str(model.lstm_hidden_dim)
                                                                                 + "_ed_" + str(model.embedding_dim)
                                                                                 + "_nl_" + str(model.num_layers))
        shutil.copyfile(f"{dir_path}/train_parallel.py", f"{folder_path}/train_parallel.py")
        shutil.copyfile(f"{dir_path}/model/model_cvae_parallel.py", f"{folder_path}/model_cvae_parallel.py")
    prev_epoch_loss = 0
    min_ade = 1e100

    for epoch in range(0, num_epochs):
        model.train()
        epoch_loss = 0
        epoch_loss_nll = 0
        epoch_loss_kl = 0
        epoch_loss_std = 0
        start = time.time()
        num_skipped = 0
        num_processed = 0

        if writer is not None:
            visualize_model_weights(epoch, model, writer)

        batch_id = 0
        pbar = tqdm.tqdm(training_generator)
        for batch_id, local_batch in enumerate(pbar):
            if batch_id - num_skipped > limit:
                # skip to next epoch
                break

            # angle = torch.rand(1) * math.pi
            # rot = torch.tensor([[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]])
            # rotrot = torch.zeros(4, 4)
            # rotrot[:2, :2] = rot.clone()
            # rotrot[2:, 2:] = rot.clone()
            # local_batch[:, :, :, 2:6] = local_batch[:, :, :, 2:6] @ rotrot
            x, neighbours = local_batch
            x = x.to(device)
            gt = x.clone()
            model = model.to(device)
            model.zero_grad()
            mask, full_peds = get_batch_is_filled_mask(gt)
            num_processed += full_peds

            num_skipped += gt.size(0) - full_peds
            if full_peds == 0:
                continue
            mask = mask.to(device)

            x = x[:, :, 2:8]
            if torch.sum(mask) == 0.0:
                num_skipped += 1
                continue
            prediction, kl = model(x[:, :, 0:6], neighbours, train=True)
            gt_prob = torch.cat(([prediction[i].log_prob(gt[:, 8 + i, 2:4]) for i in range(12)])).reshape(-1, 12)
            loss_nll = -torch.sum(gt_prob * mask[:, :, 0])
            epoch_loss_nll += loss_nll
            epoch_loss_kl += kl

            means = get_mean_prediction_multiple_timestamps(prediction)
            distance_loss = torch.dist(means* mask, gt[:, 8:, 2:4] * mask)
            loss_stdved = 0.5 * torch.sum(torch.cat(([prediction[i].stddev for i in range(12)])))
            epoch_loss_std += loss_stdved

            info = "kl: {kl:0.4f} loss_nll {loss_nll:0.2f}," \
                   " l2_d {l2_d:0.2f}".format(kl=epoch_loss_kl.detach().cpu().item() / num_processed,
                                              loss_nll=epoch_loss_nll.detach().cpu().item() / num_processed,
                                              l2_d=distance_loss)
            pbar.set_description(info)
            kl_weight = min((10*epoch+1)/100.0, 1.0)
            loss = 0.01 * loss_nll + kl_weight * kl + STD_KOEF * loss_stdved + DIST_KOEF*distance_loss

            loss.backward()

            optimizer.step()

            epoch_loss += loss.item()

        epoch_loss = epoch_loss / num_processed
        epoch_loss_kl = epoch_loss_kl / num_processed
        epoch_loss_nll = epoch_loss_nll / num_processed
        epoch_loss_std = epoch_loss_std / num_processed
        print("\tepoch {epoch} loss {el:0.4f}, time taken {t:0.2f},"
              " delta {delta:0.3f}".format(epoch=epoch, el=epoch_loss,
                                           t=time.time() - start,
                                           delta=-prev_epoch_loss + epoch_loss))

        if writer is not None:
            writer.add_scalar(f"loss_epoch", epoch_loss, epoch)
            writer.add_scalar(f"train/kl", epoch_loss_kl.detach().cpu(), epoch)
            writer.add_scalar(f"train/std", epoch_loss_std.detach().cpu(), epoch)
            writer.add_scalar(f"train/nll", epoch_loss_nll.cpu(), epoch)
            for param_group in optimizer.param_groups:
                lr = param_group['lr']
            writer.add_scalar(f"train/lr", lr, epoch)
        prev_epoch_loss = epoch_loss

        # ## VALIDATION ###
        if validate and (epoch % 1 == 0):
            model.eval()
            start = time.time()
            with torch.no_grad():
                ade, fde, nll = get_ade_fde_vel(test_generator, model)
                ade_t = torch.Tensor(ade).reshape(-1, 1)
                fde_t = torch.Tensor(fde).reshape(-1, 1)
                if writer is not None:
                    writer.add_histogram(f"hist/test/ade", ade_t, epoch)
                    writer.add_histogram(f"hist/test/fde", fde_t, epoch)
                    writer.add_histogram(f"hist/test/nll", nll, epoch)

                nll = torch.mean(nll).item()
                ade = sum(ade) / len(ade)
                fde = sum(fde) / len(fde)

                if min_ade > ade:
                    min_ade = ade
                    if writer is not None:
                        torch.save(model.state_dict(), save_model_path)

                if writer is not None:
                    writer.add_scalar(f"test/ade", ade, epoch)
                    writer.add_scalar(f"test/fde", fde, epoch)
                    writer.add_scalar(f"test/nll", nll, epoch)
                t_ade, t_fde, nll = get_ade_fde_vel(training_generator, model, 2)
                if writer is not None:
                    pass
                    ade_t = torch.Tensor(t_ade).reshape(-1, 1)
                    fde_t = torch.Tensor(t_fde).reshape(-1, 1)
                    writer.add_histogram(f"hist/train/ade", ade_t, epoch)
                    writer.add_histogram(f"hist/train/fde", fde_t, epoch)
                    writer.add_histogram(f"hist/train/nll", nll, epoch)

                nll = torch.mean(nll).item()
                t_ade = sum(t_ade) / len(t_ade)
                t_fde = sum(t_fde) / len(t_fde)

                # TODO: calc during training
                if writer is not None:
                    writer.add_scalar(f"train/ade", t_ade, epoch)
                    writer.add_scalar(f"train/fde", t_fde, epoch)
                    # writer.add_scalar(f"train/nll", nll, epoch)
            print("\t\t epoch {epoch} val ade {ade:0.4f}, val fde {fde:0.4f}"
                  " time taken {t:0.2f}".format(epoch=epoch, ade=ade,
                                                t=time.time() - start,
                                                fde=fde))
            if writer is not None:
                images = []
                for i in range(5):
                    visualize_single_parallel(model, test_generator)
                    buf = io.BytesIO()
                    plt.savefig(buf, format='jpeg')
                    buf.seek(0)
                    image = PIL.Image.open(buf)
                    images.append(ToTensor()(image))
                    plt.close()
                images = torch.cat(images, dim=1)
                writer.add_image('p_distr', images, epoch)

        scheduler.step()
    if writer is not None:
        torch.save(model.state_dict(), save_model_path)
Example #28
0
def predictor_response_train_model_neodroid_observations(
    model,
    *,
    train_iterator,
    criterion,
    optimizer,
    scheduler,
    writer,
    interrupted_path,
    val_data_iterator=None,
    num_updates: int = 250000,
    device=global_torch_device(),
    early_stop=None,
):
    best_model_wts = copy.deepcopy(model.state_dict())
    best_val_loss = 1e10
    since = time.time()

    try:
        sess = tqdm.tqdm(range(num_updates), leave=False, disable=False)
        val_loss = 0
        update_loss = 0
        val_acc = 0
        last_val = None
        last_out = None
        with torch.autograd.detect_anomaly():
            for update_i in sess:
                for phase in [Split.Training, Split.Validation]:
                    if phase == Split.Training:
                        with TorchTrainSession(model):

                            input, true_label = zip(*next(train_iterator))

                            rgb_imgs = torch_vision_normalize_batch_nchw(
                                uint_hwc_to_chw_float_tensor(
                                    rgb_drop_alpha_batch_nhwc(to_tensor(input))
                                )
                            )
                            true_label = to_tensor(
                                true_label, dtype=torch.long, device=device
                            )
                            optimizer.zero_grad()

                            pred = model(rgb_imgs)
                            loss = criterion(pred, true_label)
                            loss.backward()
                            optimizer.step()

                            if last_out is None:
                                last_out = pred
                            else:
                                if not torch.dist(last_out, pred) > 0:
                                    print(f"Same output{last_out},{pred}")
                                last_out = pred

                            update_loss = loss.data.cpu().numpy()
                            writer.scalar(f"loss/train", update_loss, update_i)

                            if scheduler:
                                scheduler.step()
                    elif val_data_iterator:
                        with TorchEvalSession(model):
                            test_rgb_imgs, test_true_label = zip(
                                *next(val_data_iterator)
                            )
                            test_rgb_imgs = torch_vision_normalize_batch_nchw(
                                uint_hwc_to_chw_float_tensor(
                                    rgb_drop_alpha_batch_nhwc(to_tensor(test_rgb_imgs))
                                )
                            )

                            test_true_label = to_tensor(
                                test_true_label, dtype=torch.long, device=device
                            )

                            with torch.no_grad():
                                val_pred = model(test_rgb_imgs)
                                val_loss = criterion(val_pred, test_true_label)

                            _, cat = torch.max(val_pred, -1)
                            val_acc = torch.sum(cat == test_true_label) / float(
                                cat.size(0)
                            )
                            writer.scalar(f"loss/acc", val_acc, update_i)
                            writer.scalar(f"loss/val", val_loss, update_i)

                            if last_val is None:
                                last_val = cat
                            else:
                                if all(last_val == cat):
                                    print(f"Same val{last_val},{cat}")
                                last_val = cat

                            if val_loss < best_val_loss:
                                best_val_loss = val_loss

                                best_model_wts = copy.deepcopy(model.state_dict())
                                sess.write(
                                    f"New best validation model at update {update_i} with test_loss {best_val_loss}"
                                )
                                torch.save(model.state_dict(), interrupted_path)

                        if early_stop is not None and val_pred < early_stop:
                            break
                sess.set_description_str(
                    f"Update {update_i} - {phase} "
                    f"update_loss:{update_loss:2f} "
                    f"val_loss:{val_loss}"
                    f"val_acc:{val_acc}"
                )

    except KeyboardInterrupt:
        print("Interrupt")
    finally:
        pass

    model.load_state_dict(best_model_wts)  # load best model weights

    time_elapsed = time.time() - since
    print(f"{time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s")
    print(f"Best val loss: {best_val_loss:3f}")

    return model
Example #29
0
    def cauchy_cross_entropy(self,
                             u,
                             label_u,
                             v=None,
                             label_v=None,
                             gamma=1,
                             normed=True):
        """
        Input tensor:
                                u: 			batch size x embedding_dim
                                label_u:	batch_size x 1

        """
        # print("u",u.size())
        # print("label_u",label_u.size())
        # print("u shape",u.size())
        u = u.float()
        label_u = label_u.float()
        if v is None:
            v, label_v = u, label_u
        else:
            v = v.float()
            label_v = label_v.float()
        """ label_ip: batch_size x batch_size"""

        if len(label_u.shape) == 1:
            label_ip = label_u.unsqueeze(1) @ label_v.unsqueeze(0)
        else:
            label_ip = label_u @ label_v.t()
        """ s: batch_size x batch_size, lies in [0,1]"""

        s = torch.clamp(label_ip, 0.0, 1.0)
        # print("="*30)
        # print(s)
        # print("="*30)
        # assert 1==0
        if normed:  # hamming distance
            """
                    Literal translation:

            ip_1 = u @ v.t()
            mod_1 =  torch.sqrt(torch.sum(u**2,1).unsqueeze(1) @ (torch.sum(v**2,1)+0.000001).unsqueeze(0))
            dist = (self.output_dim/2.0) * (1.0- ip_1/mod_1) + 0.000001
            """

            # Compact code:
            w1 = u.norm(p=2, dim=1, keepdim=True)
            w2 = w1 if u is v else v.norm(p=2, dim=1, keepdim=True)
            dist = (self.output_dim / 2.0) * (1.0 - torch.mm(u, v.t()) /
                                              (w1 * w2.t() + 1e-6)) + 1e-6
            if self.nan_debug:
                print("Numerator: ", torch.mm(u, v.t()))
                print("Denominator: ", w1 * w2.t())
                torch.save(u, "nan_aaya_uska_u.pt")
                torch.save(label_u, "nan_aaya_uska_u_label.pt")
            # print("u: ",u)
            # print("dist: ",dist)
        else:  # Euclidean distance
            """
                    Literal translation:

                    r_u = torch.sum(u**2, 1)
                    r_v = torch.sum(v**2, 1)
                    dist = r_u - 2 * (u @ v.t()) + r_v.unsqueeze(1) + 0.001
            """

            # Compact code
            dist = torch.dist(u, v) + 1e-3

        cauchy = gamma / (dist + gamma)
        # print("size of cauchy is: {}".format(cauchy.size()))
        """ s_t: batch_size x batch_size, [-1,1]"""
        s_t = 2 * (s - 0.5)
        sum_1 = torch.sum(s)
        sum_all = torch.sum(torch.abs(s_t))
        balance_param = torch.abs(
            s - 1
        ) + s * sum_all / sum_1  # Balancing similar and non-similar classes (wij in paper)

        mask = torch.eye(u.shape[0]) == 0
        # print("mask",mask)
        cauchy_mask = cauchy[mask]
        s_mask = s[mask]
        balance_p_mask = balance_param[mask]

        all_loss = -s_mask * torch.log(cauchy_mask + 1e-5) - (
            1 - s_mask) * torch.log(1 - cauchy_mask + 1e-5)
        loss = torch.sum(all_loss * balance_p_mask)
        if torch.isnan(loss).any():
            print("NAN Cauchy CE loss")
            self.nan_debug = True
            # print("u: ",u)
            # assert 1==0
        return loss
Example #30
0
    def CD(self, v0, epoch, eta, K=1):
        """
            Contrastive divergence (CD)

                Arguments:
                    v0 : torch.tensor of shape (num_patterns, num_v)
                        Patterns to store
                    epoch : int
                        Current training epoch
                    eta : float
                        Learning rate
                    K : int, optional
                        Number of CD steps
                            Default value = 1

                Returns:
                    L2 : float
                        L2 distance between the original patterns v0 and the reconstructions vK
        """
        batch_size = v0.size(0)

        # Positive phase
        v_data = v0
        pos_hid_probs, pos_hid_states = self.Ph_v(v_data)

        if self.v_type == "Bernoulli":
            pos_delta_bv, pos_delta_W, pos_delta_bh = self.delta(
                v_data, pos_hid_probs)
        else:
            pos_delta_bv, pos_delta_W, pos_delta_bh, pos_delta_sigma = self.delta(
                v_data, pos_hid_probs)

        # Gibbs sampling
        h_K = pos_hid_states
        if K > 1:
            for _ in range(K - 1):
                pv_K, v_K = self.Pv_h(h_K)
                vis = pv_k if self.v_type == "Bernoulli" else v_k
                ph_K, h_K = self.Ph_v(vis)

        # Negative phase
        neg_vis_probs, neg_vis_states = self.Pv_h(h_K)
        neg_vis = neg_vis_probs if self.v_type == "Bernoulli" else neg_vis_states
        neg_hid_probs, neg_hid_states = self.Ph_v(neg_vis)

        if self.v_type == "Bernoulli":
            neg_delta_bv, neg_delta_W, neg_delta_bh = self.delta(
                neg_vis_probs, neg_hid_probs)
        else:
            neg_delta_bv, neg_delta_W, neg_delta_bh, neg_delta_sigma = self.delta(
                neg_vis_states, neg_hid_probs)

        # Gradients
        self.dtheta["bv"] = nn.Parameter(
            (pos_delta_bv - neg_delta_bv).reshape(self.theta["bv"].size()) /
            batch_size)
        self.dtheta["W"] = nn.Parameter(
            (pos_delta_W - neg_delta_W).reshape(self.theta["W"].size()) /
            batch_size)
        self.dtheta["bh"] = nn.Parameter(
            (pos_delta_bh - neg_delta_bh).reshape(self.theta["bh"].size()) /
            batch_size)
        if self.v_type == "Gaussian":
            self.dtheta["sigma"] = nn.Parameter(
                (pos_delta_sigma - neg_delta_sigma).reshape(
                    self.theta["sigma"].size()) / batch_size)

        # Optimization
        self.adam(epoch + 1, eta)

        # Reconstruction error
        return torch.dist(v0, neg_vis, 2)
Example #31
0
    def train(self):

        # Switch model to train mode
        self.model.train()

        # Check if maxEpochs have elapsed
        if self.curEpoch >= self.maxEpochs:
            print('Max epochs elapsed! Returning ...')
            return

        # Variables to store stats
        rotLosses = []
        transLosses = []
        totalLosses = []
        rotLoss_seq = []
        transLoss_seq = []
        totalLoss_seq = []

        # Handle debug mode here
        if self.args.debug is True:
            numTrainIters = self.args.debugIters
        else:
            numTrainIters = len(self.train_set)

        # Initialize a variable to hold the number of sampes in the current batch
        # Here, 'batch' refers to the length of a subsequence that can be processed
        # before performing a 'detach' operation
        elapsedBatches = 0

        # Choose a generator (for iterating over the dataset, based on whether or not the
        # sbatch flag is set to True). If sbatch is True, we're probably running on a cluster
        # and do not want an interactive output. So, could suppress tqdm and print statements
        if self.args.sbatch is True:
            gen = range(numTrainIters)
        else:
            gen = trange(numTrainIters)

        # Run a pass of the dataset
        for i in gen:

            if self.args.profileGPUUsage is True:
                gpu_memory_map = get_gpu_memory_map()
                tqdm.write('GPU usage: ' + str(gpu_memory_map[0]),
                           file=sys.stdout)

            # Get the next frame
            inp, rot_gt, trans_gt, _, _, _, endOfSeq = self.train_set[i]

            # Feed it through the model
            rot_pred, trans_pred = self.model.forward(inp)

            # Compute loss
            # self.loss_rot += self.args.scf * self.loss_fn(rot_pred, rot_gt)
            if self.args.outputParameterization == 'mahalanobis':
                # Compute a mahalanobis norm on the output 6-vector
                # Note that, although we seem to be computing loss only over rotation variables
                # rot_pred and rot_gt are now 6-vectors that also include translation variables.
                self.loss += self.loss_fn(rot_pred, rot_gt,
                                          self.train_set.infoMat)
                tmpLossVar = Variable(
                    torch.mm(
                        rot_pred - rot_gt,
                        torch.mm(self.train_set.infoMat,
                                 (rot_pred - rot_gt).t())),
                    requires_grad=False).detach().cpu().numpy()
                # tmpLossVar = Variable(torch.dist(rot_pred, rot_gt) ** 2, requires_grad = False).detach().cpu().numpy()
                totalLosses.append(tmpLossVar[0])
                totalLoss_seq.append(tmpLossVar[0])
            else:
                curloss_rot = Variable(self.args.scf *
                                       (torch.dist(rot_pred, rot_gt)**2),
                                       requires_grad=False)
                curloss_trans = Variable(torch.dist(trans_pred, trans_gt)**2,
                                         requires_grad=False)
                self.loss_rot += curloss_rot
                self.loss_trans += curloss_trans

                #if np.random.normal() < -0.9:
                #					tqdm.write('rot: ' + str(rot_pred.data) + ' ' + str(rot_gt.data), file = sys.stdout)
                #					tqdm.write('trans: ' + str(trans_pred.data) + ' ' + str(trans_gt.data), file = sys.stdout)

                self.loss += sum([self.args.scf * self.loss_fn(rot_pred, rot_gt), \
                 self.loss_fn(trans_pred, trans_gt)])

                curloss_rot = curloss_rot.detach().cpu().numpy()
                curloss_trans = curloss_trans.detach().cpu().numpy()
                rotLosses.append(curloss_rot)
                transLosses.append(curloss_trans)
                totalLosses.append(curloss_rot + curloss_trans)
                rotLoss_seq.append(curloss_rot)
                transLoss_seq.append(curloss_trans)
                totalLoss_seq.append(curloss_rot + curloss_trans)

            # Handle debug mode here. Force execute the below if statement in the
            # last debug iteration
            if self.args.debug is True:
                if i == numTrainIters - 1:
                    endOfSeq = True

            elapsedBatches += 1
            self.iters += 1
            if self.iters % 50 == 0:
                print(self.iters)

            # if endOfSeq is True:
            if elapsedBatches >= self.args.trainBatch or endOfSeq is True:

                elapsedBatches = 0

                # Regularize only LSTM(s)
                if self.args.gamma > 0.0:
                    paramsDict = self.model.state_dict()
                    # print(paramsDict.keys())
                    if self.args.numLSTMCells == 1:
                        reg_loss = None
                        reg_loss = paramsDict['lstm1.weight_ih'].norm(2)
                        reg_loss += paramsDict['lstm1.weight_hh'].norm(2)
                        reg_loss += paramsDict['lstm1.bias_ih'].norm(2)
                        reg_loss += paramsDict['lstm1.bias_hh'].norm(2)
                    else:
                        reg_loss = None
                        reg_loss = paramsDict['lstm2.weight_ih'].norm(2)
                        reg_loss += paramsDict['lstm2.weight_hh'].norm(2)
                        reg_loss += paramsDict['lstm2.bias_ih'].norm(2)
                        reg_loss += paramsDict['lstm2.bias_hh'].norm(2)
                        reg_loss += paramsDict['lstm2.weight_ih'].norm(2)
                        reg_loss += paramsDict['lstm2.weight_hh'].norm(2)
                        reg_loss += paramsDict['lstm2.bias_ih'].norm(2)
                        reg_loss += paramsDict['lstm2.bias_hh'].norm(2)
                    self.loss = sum([self.args.gamma * reg_loss, self.loss])

                # Print stats
#				if self.args.outputParameterization != 'mahalanobis':
#					tqdm.write('Rot Loss: ' + str(np.mean(rotLoss_seq)) + ' Trans Loss: ' + \
#						str(np.mean(transLoss_seq)), file = sys.stdout)
#				else:
#					tqdm.write('Total Loss: ' + str(np.mean(totalLoss_seq)), file = sys.stdout)
                rotLoss_seq = []
                transLoss_seq = []
                totalLoss_seq = []

                # Compute gradients	# ???
                self.loss.backward()

                # Monitor gradients
                l = 0
                paramList = list(
                    filter(lambda p: p.grad is not None,
                           [param for param in self.model.parameters()]))
                totalNorm = sum([(p.grad.data.norm(2.)**2.)
                                 for p in paramList])**(1. / 2)
                #				tqdm.write('gradNorm: ' + str(totalNorm.item()))

                # Perform gradient clipping, if enabled
                if self.args.gradClip is not None:
                    torch.nn.utils.clip_grad_norm_(self.model.parameters(),
                                                   self.args.gradClip)

                # Update parameters
                self.optimizer.step()

                # If it's the end of sequence, reset hidden states
                if endOfSeq is True:
                    self.model.reset_LSTM_hidden()
                self.model.detach_LSTM_hidden()  # ???

                # Reset loss variables
                self.loss_rot = torch.zeros(1, dtype=torch.float32).cuda()
                self.loss_trans = torch.zeros(1, dtype=torch.float32).cuda()
                self.loss = torch.zeros(1, dtype=torch.float32).cuda()

                # Flush gradient buffers for next forward pass
                self.model.zero_grad()

        # Return loss logs for further analysis
        if self.args.outputParameterization == 'mahalanobis':
            return [], [], totalLosses
        else:
            return rotLosses, transLosses, totalLosses
Example #32
0
def inverse_distance(h, h_i, epsilon=1e-3):
  return 1 / (torch.dist(h, h_i) + epsilon)
Example #33
0
    def _act(self, state):

        y = self.normalize_y(state[self.indice_y])
        y_star = self.normalize_y(state[self.indice_y_star])
        c = self.normalize_c(state[self.indice_c])

        self.log_y.append(np.copy(y))
        y = torch.FloatTensor(y).unsqueeze(0)
        y_star = torch.FloatTensor(y_star).unsqueeze(0)
        c = torch.FloatTensor(c).unsqueeze(0)

        self.delta_u = self.env.u_bounds[:, 1] - self.env.u_bounds[:, 0]
        self.mid_u = np.mean(self.env.u_bounds, axis=1)
        x = torch.FloatTensor(np.hstack((y, y_star, c))).unsqueeze(0)

        act = torch.nn.Parameter(2 * torch.rand((1, self.env.size_yudc[1])) -
                                 1)
        opt = torch.optim.SGD(params=[act], lr=0.03)

        act_list = []
        act_list.append(np.copy(act.data.numpy()).squeeze())
        iters_cnt = 0
        while True:
            old_act = act.clone()
            diff_U = torch.FloatTensor(self.u_bounds[:, 1] -
                                       self.u_bounds[:, 0])
            det_u = torch.nn.functional.linear(input=act,
                                               weight=torch.diag(diff_U / 2))
            # penalty_u = (det_u.mm(torch.FloatTensor(self.env.penalty_calculator.S)).mm(
            #     det_u.t()
            # )).diag().unsqueeze(dim=1)
            penalty_u = (det_u.mm(
                torch.FloatTensor(self.env.penalty_calculator.S)).mm(
                    det_u.t())).diag()

            y.requires_grad = True
            y_pred = self.model_nn(torch.cat((y, act, c), dim=1))
            J_costate = self.critic_nn(torch.cat((y_pred, y_star, c), dim=1))
            #penalty_u = torch.zeros(J_pred.shape)
            J_loss = penalty_u + self.gamma * torch.mul(y_pred,
                                                        J_costate).sum()
            J_loss = J_loss.mean()
            opt.zero_grad()
            J_loss.backward()
            opt.step()
            act.data = torch.nn.Parameter(torch.clamp(act, min=-1, max=1)).data
            act_list.append(np.copy(act.data.numpy()).squeeze())
            if torch.dist(act, old_act) < 1e-8:
                break
            iters_cnt += 1
            if iters_cnt >= self.max_u_iters:
                break

        print('step:', self.step, 'find u loop', iters_cnt)
        act = act.detach().numpy()

        # endregion
        #print("final act:", act)
        #print('#'*20)

        self.last_act = np.copy(act)
        # print(act)
        # make the output action locate in bounds of constraint
        # U = (max - min)/2 * u + (max + min)/2

        A = np.matrix(np.diag(self.delta_u / 2))
        B = np.matrix(self.mid_u).T
        act = A * np.matrix(act).T + B
        act = np.array(act).reshape(-1)
        # self.actor_nn[-1].weight.data = torch.FloatTensor()
        # self.actor_nn[-1].bias.data = torch.FloatTensor(self.mid_u)
        # self.actor_nn[-1].weight.requires_grad = False
        # self.actor_nn[-1].bias.requires_grad = False

        # if (self.step-1) % 20  == 0:
        #     self.test_critic_nn(title='round:'+str(self.step), cur_state=y, act_list=act_list)
        # if 195<self.step-1<220:
        #     self.test_critic_nn(title='round:'+str(self.step), cur_state=y, act_list=act_list)
        if self.step % self.policy_visual_period == 0:
            self.policy_visual(title='round ' + str(self.step) + ' find u',
                               act_list=act_list)
        return act
Example #34
0
ideal_colle = model1_human(
    torch.tensor(data_humanities[['stu_rank', 'stu_long', 'stu_lati']].values))

model2_data_humanities = pd.DataFrame(
    columns=['uni_rank', 'long_diff', 'lati_diff', 'label'])

for i in range(len(data_humanities)):
    student = data_humanities.iloc[i]
    colle = ideal_colle.iloc[i]

    distances = []  # 用于存储每所可选大学与预测大学的距离
    better_schools = []
    for school in data_university:
        distances.append(
            torch.dist(
                torch.tensor(school[['uni_rank', 'uni_long', 'uni_lati']]),
                colle))

    better_schools_index = list(
        map(distances.index,
            heapq.nsmallest(10, distances)))  # 选取可选大学中,与预测大学距离最近的10个大学索引
    best_school_index = map(distances.index, heapq.nsmallest(1, distances))
    better_schools_index.pop(
        better_schools_index.index(best_school_index))  #去除最接近的大学
    for better_school_index in better_schools_index:
        better_schools.append(data_university.iloc[better_school_index])
    best_school = data_university.iloc[best_school_index]

    for better_school in better_schools:
        model2_data_humanities.append(better_school['uni_rank'].append(
            student[['stu_long', 'stu_lati']] -
Example #35
0
            _, var_input_map = inference_input_map.predict(x_pred_points[i:i +
                                                                         1])
            var_input_map_list.append(var_input_map)
        pred_var_input_map = torch.cat(var_input_map_list, 0)
        jitter = torch.from_numpy(np.array(jitter_list)).view(-1, 1).type_as(
            x_pred_points.data)
        shadow_jitter = jitter.clone().fill_(inference_shadow.jitter)
        data = torch.cat([
            pred_var_shadow.data, pred_var_input_map.data, jitter,
            shadow_jitter
        ], 1)
        print(
            torch.min(pred_var_shadow).data[0],
            torch.max(pred_var_shadow).data[0])
        print('l2 distance',
              torch.dist(pred_var_shadow, pred_var_input_map).data[0])
        print(
            'l-inf distance',
            torch.max(torch.abs(pred_var_shadow - pred_var_input_map)).data[0])

        mask_more = (pred_var_shadow < pred_var_input_map).data
        print('fake data var < element wise var', torch.sum(mask_more))
        ind_differ = torch.sort(
            mask_more, 0, descending=True)[1][:torch.sum(mask_more)].squeeze()
        print('decreased jitter', torch.sum(jitter < shadow_jitter))

        mask_less = (pred_var_shadow > pred_var_input_map).data
        print('fake data var > element wise var', torch.sum(mask_less))
        if torch.sum(mask_less) > 0:
            ind_less = torch.sort(
                mask_less, 0,
Example #36
0
with torch.no_grad():
    for data in testloader:
        images, labels = data
        images, labels = images.to(device), labels.to(device)

        matlabels = labels_to_R9(labels)
        outputs = net(images)

        alllabels = torch.zeros(10, 9)
        for i in range(1, 10):
            alllabels[i, i - 1] = 1

        distance = torch.zeros(batch_size, 10)
        for i in range(batch_size):
            for j in range(10):
                distance[i, j] = torch.dist(outputs[i],
                                            alllabels[j].to(device), 2)

        _, predicted = torch.min(distance, 1)

        #predicted=prediction(outputs)

        total += matlabels.size(0)
        correct += (predicted.to(device) == labels.to(device)).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' %
      (100 * correct / total))

class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
    for data in testloader:
            Kk = Pkk.mm(H.t()).mm(torch.inverse(H.mm(Pkk).mm(H.t()) + R0))
            Xkk = Xkk + Kk.mm(Zyuce[i, j] - H.mm(Xkk))
            Pkk = Pkk - Kk.mm(H).mm(Pkk)

            # time update
            Xkk = F.mm(Xkk)  # size is n*1
            Pkk = F.mm(Pkk).mm(F.t()) + G.mm(Q0).mm(G.t())  # size is n*n

            # save
            Xkk_save[i, j] = Xkk
            Pkk_save[i, j] = Pkk

        # DI I CI SHIYAN
        result[i] = (Xyuce[i, -1] - Xkk).mm((Xyuce[i, -1] - Xkk).t())
        resultPkkjy[i] = Pkk
        montecarlo[i] = torch.dist(
            torch.mean(result[:i + 1], 0), resultPkkjy[i], p=2) / torch.dist(
                resultPkkjy[i], torch.zeros(n, n), p=2)

    ########################################
    # plot
    # gen zong
    plt.subplot(211)
    plt.plot(Xyuce[i, ::10, 0, 0].numpy(), Xyuce[i, ::10, 1, 0].numpy(), 'o-')
    plt.plot(Xkk_save[i, ::10, 0, 0].numpy(), Xkk_save[i, ::10, 1, 0].numpy(),
             '*-')
    # montecarlo
    plt.subplot(212)
    plt.plot(montecarlo.numpy()[::10], 'o-')
    plt.show()
import collections
s = collections.OrderedDict()


i=0
for key in state_dict.keys():
    new = torch.from_numpy(netparams[i])
    s[key] = new
    if s[key].dim() == 4:
        pass#s[key].transpose_(2,3)
    i += 1

net.load_state_dict(s)

net.conv1.register_forward_hook(lambda self, input, output: \
    print('conv1', torch.dist(output.data, netoutputs[0])))
net.bn1.register_forward_hook(lambda self, input, output: \
    print('bn1', torch.dist(output.data, netoutputs[1])))
net.relu.register_forward_hook(lambda self, input, output: \
    print('relu', torch.dist(output.data, netoutputs[2])))
net.maxpool.register_forward_hook(lambda self, input, output: \
    print('maxpool', torch.dist(output.data, netoutputs[3])))
net.layer1.register_forward_hook(lambda self, input, output: \
    print('layer1', torch.dist(output.data, netoutputs[4])))
net.layer2.register_forward_hook(lambda self, input, output: \
    print('layer2', torch.dist(output.data, netoutputs[5])))
net.layer3.register_forward_hook(lambda self, input, output: \
    print('layer3', torch.dist(output.data, netoutputs[6])))
net.layer4.register_forward_hook(lambda self, input, output: \
    print('layer4', torch.dist(output.data, netoutputs[7])))
net.avgpool.register_forward_hook(lambda self, input, output: \
def lk_target_loss(batch_locs, batch_scos, batch_next, batch_fbak, batch_back, lk_config, video_or_not, mask, nopoints):
  # return the calculate target from the first frame to the whole sequence.
  batch, sequence, num_pts = lk_input_check(batch_locs, batch_scos, batch_next, batch_fbak, batch_back)

  # remove the background
  num_pts = num_pts - 1
  sequence_checks = np.ones((batch, num_pts), dtype='bool')

  # Check the confidence score for each point
  for ibatch in range(batch):
    if video_or_not[ibatch] == False:
      sequence_checks[ibatch, :] = False
    else:
      for iseq in range(sequence):
        for ipts in range(num_pts):
          score = batch_scos[ibatch, iseq, ipts]
          if mask[ibatch, ipts] == False and nopoints[ibatch] == 0:
            sequence_checks[ibatch, ipts] = False
          if score.item() < lk_config.conf_thresh:
            sequence_checks[ibatch, ipts] = False

  losses = []
  for ibatch in range(batch):
    for ipts in range(num_pts):
      if not sequence_checks[ibatch, ipts]: continue
      loss = 0
      for iseq in range(sequence):
      
        targets = batch_locs[ibatch, iseq, ipts]
        nextPts = batch_next[ibatch, iseq, ipts]
        fbakPts = batch_fbak[ibatch, iseq, ipts]
        backPts = batch_back[ibatch, iseq, ipts]

        with torch.no_grad():
          fbak_distance = torch.dist(nextPts, fbakPts)
          back_distance = torch.dist(targets, backPts)
          forw_distance = torch.dist(targets, nextPts)

        #print ('[{:02d},{:02d},{:02d}] : {:.2f}, {:.2f}, {:.2f}'.format(ibatch, ipts, iseq, fbak_distance.item(), back_distance.item(), forw_distance.item()))
        #loss += back_distance + forw_distance

        if fbak_distance.item() > lk_config.fb_thresh or fbak_distance.item() < lk_config.eps: # forward-backward-check
          if iseq+1 < sequence: sequence_checks[ibatch, ipts] = False
        if forw_distance.item() > lk_config.forward_max or forw_distance.item() < lk_config.eps: # to avoid the tracker point is too far
          if iseq   > 0       : sequence_checks[ibatch, ipts] = False
        if back_distance.item() > lk_config.forward_max or back_distance.item() < lk_config.eps: # to avoid the tracker point is too far
          if iseq+1 < sequence: sequence_checks[ibatch, ipts] = False

        if iseq > 0:
          if lk_config.stable: loss += torch.dist(targets, backPts.detach())
          else               : loss += torch.dist(targets, backPts)
        if iseq + 1 < sequence:
          if lk_config.stable: loss += torch.dist(targets, nextPts.detach())
          else               : loss += torch.dist(targets, nextPts)

      if sequence_checks[ibatch, ipts]:
        losses.append(loss)

  avaliable = int(np.sum(sequence_checks))
  if avaliable == 0: return None, avaliable
  else             : return torch.mean(torch.stack(losses)), avaliable