def ResidualTabularWPrior(num_classes,
                          dim_in,
                          coupling_layers,
                          k,
                          means_r=1.,
                          cov_std=1.,
                          nperlayer=1,
                          acc=0.9):
    #print(f'Instantiating means with dimension {dim_in}.')
    device = torch.device('cuda')
    inv_cov_std = torch.ones((num_classes, ), device=device) / cov_std
    model = TabularResidualFlow(
        in_dim=dim_in, hidden_dim=k,
        num_per_block=coupling_layers)  #*np.sqrt(1000/dim_in)/3
    dist_scaling = np.sqrt(-8 * np.log(1 - acc))
    means = utils.get_means('random',
                            r=means_r * dist_scaling,
                            num_means=num_classes,
                            trainloader=None,
                            shape=(dim_in),
                            device=device)
    means[0] /= means[0].norm()
    means[0] *= dist_scaling / 2
    means[1] = -means[0]
    model.prior = SSLGaussMixture(means, inv_cov_std, device=device)
    means_np = means.cpu().numpy()
    #print("Pairwise dists:", cdist(means_np, means_np))
    return model
def RealNVPTabularWPrior(num_classes,
                         dim_in,
                         coupling_layers,
                         k,
                         means_r=.8,
                         cov_std=1.,
                         nperlayer=1,
                         acc=0.9):
    #print(f'Instantiating means with dimension {dim_in}.')
    device = torch.device('cuda')
    inv_cov_std = torch.ones((num_classes, ), device=device) / cov_std
    model = RealNVPTabular(num_coupling_layers=coupling_layers,
                           in_dim=dim_in,
                           hidden_dim=k,
                           num_layers=1,
                           dropout=True)  #*np.sqrt(1000/dim_in)/3
    #dist_scaling = np.sqrt(-8*np.log(1-acc))#np.sqrt(4*np.log(20)/dim_in)#np.sqrt(1000/dim_in)
    if num_classes == 2:
        means = utils.get_means('random',
                                r=means_r,
                                num_means=num_classes,
                                trainloader=None,
                                shape=(dim_in),
                                device=device)
        #means = torch.zeros(2,dim_in,device=device)
        #means[0,1] = 3.75
        dist = 2 * (means[0]**2).sum().sqrt()
        means[0] *= 7.5 / dist
        means[1] = -means[0]
        # means[0] /= means[0].norm()
        # means[0] *= dist_scaling/2
        # means[1] = - means[0]
        model.prior = SSLGaussMixture(means, inv_cov_std, device=device)
        means_np = means.cpu().numpy()
    else:
        means = utils.get_means('random',
                                r=means_r * .7,
                                num_means=num_classes,
                                trainloader=None,
                                shape=(dim_in),
                                device=device)
        model.prior = SSLGaussMixture(means, inv_cov_std, device=device)
        means_np = means.cpu().numpy()
    print("Means :", means_np)
    print("Pairwise dists:", cdist(means_np, means_np))
    return model
Exemplo n.º 3
0
def test_basic():
    """
        Description:
        Predict using collaborative filtering without baseline model; using nearest neighbours only.

        Return:
        pred : prediction of ratings using only nearest neighbours
        pred_base : prediction of ratings using baseline along with basic C.F.
        y : actual predictions
        users_items : users corresponding to the predictions made; useful for p@k
    """
    pred = []
    y = []
    users = []
    items = []
    users_items = []
    folder = "./ml-100k/"
    testing = folder + 'u3.test'
    fields = ['user_id', 'item_id', 'rating', 'timestamp']
    #fetch the dataset
    dtset = pd.read_csv(testing, sep='\t', names=fields)
    count = 0
    um = {}
    #save all the values like mean, similarities etc in local variables to avoid redundant calling of the same functions.
    ratings = get_dataset()
    movie_mean, user_mean, mean = get_means(ratings)
    movie_sim = get_sim(ratings, f="movies")
    for row in dtset.itertuples():
        pred.append(
            predict_neighbours(row[1] - 1, row[2] - 1, ratings, movie_sim))
        y.append(row[3] - 1)
        users.append(row[1] - 1)
        items.append(row[2] - 1)
    final_pred = []
    final_y = []
    for i in range(len(pred)):
        #to avoid getting 'nan' values for the final evaluation results, select only those that are not 'nan'.
        if not (math.isnan(y[i]) or y[i] == 0 or math.isnan(pred[i])):
            final_pred.append(pred[i])
            final_y.append(y[i])
            users_items.append([users[i], items[i]])
    return final_pred, final_y, users_items
Exemplo n.º 4
0
def test_base():
    """
        Description:
        Predict using collaborative filtering using baseline model along with nearest neighbours.

        Return:
        pred : prediction of ratings using only nearest neighbours
        pred_base : prediction of ratings using baseline along with basic C.F.
        y : actual predictions
        users_items : users corresponding to the predictions made; useful for p@k
    """
    pred_base = []
    y = []
    users = []
    items = []
    users_items = []
    folder = "./ml-100k/"
    testing = folder + 'u3.test'
    fields = ['user_id', 'item_id', 'rating', 'timestamp']
    dtset = pd.read_csv(testing, sep='\t', names=fields)
    count = 0
    um = {}
    ratings = get_dataset()
    movie_mean, user_mean, mean = get_means(ratings)
    movie_sim = get_sim(ratings, f="movies")
    for row in dtset.itertuples():
        pred_base.append(
            predict_neighbours_baseline(row[1] - 1, row[2] - 1, ratings,
                                        movie_mean, user_mean, mean,
                                        movie_sim))
        y.append(row[3] - 1)
        users.append(row[1] - 1)
        items.append(row[2] - 1)
    final_pred_base = []
    final_y = []
    for i in range(len(pred_base)):
        if not (math.isnan(y[i]) or y[i] == 0 or math.isnan(pred_base[i])):
            final_pred_base.append(pred_base[i])
            final_y.append(y[i])
            users_items.append([users[i], items[i]])
    return final_pred_base, final_y, users_items
Exemplo n.º 5
0
    net = torch.nn.DataParallel(net, args.gpu_ids)
    cudnn.benchmark = True  #args.benchmark

if args.resume is not None:
    print('Resuming from checkpoint at', args.resume)
    checkpoint = torch.load(args.resume)
    net.load_state_dict(checkpoint['net'])
    start_epoch = checkpoint['epoch']

#PAVEL: we need to find a good way of placing the means
r = args.means_r
cov_std = torch.ones((10)) * args.cov_std
cov_std = cov_std.to(device)
means = utils.get_means(args.means,
                        r=args.means_r,
                        trainloader=trainloader,
                        shape=img_shape,
                        device=device)

if args.resume is not None:
    print("Using the means for ckpt")
    means = checkpoint['means']

print("Means:", means)
print("Cov std:", cov_std)
means_np = means.cpu().numpy()
print("Pairwise dists:", cdist(means_np, means_np))

if args.means_trainable:
    print("Using learnable means")
    means = torch.tensor(means_np, requires_grad=True)
Exemplo n.º 6
0
    net = torch.nn.DataParallel(net, args.gpu_ids)
    cudnn.benchmark = True  #args.benchmark

if args.resume is not None:
    print('Resuming from checkpoint at', args.resume)
    checkpoint = torch.load(args.resume)
    net.load_state_dict(checkpoint['net'])
    start_epoch = checkpoint['epoch']

#PAVEL: we need to find a good way of placing the means
r = args.means_r
cov_std = torch.ones((n_class)) * args.cov_std
cov_std = cov_std.to(device)
means = utils.get_means(args.means,
                        r=args.means_r,
                        num_means=n_class,
                        trainloader=trainloader,
                        shape=(embed_size, ),
                        device=device)

if args.resume is not None:
    print("Using the means for ckpt")
    means = checkpoint['means']

print("Means:", means)
print("Cov std:", cov_std)
means_np = means.cpu().numpy()
print("Pairwise dists:", cdist(means_np, means_np))

if args.means_trainable:
    print("Using learnable means")
    means = torch.tensor(means_np, requires_grad=True)