def rbfnn(args, X, y, Xval, yval):
    from sklearn.cluster import KMeans
    if args.dataset == 'protein' or args.dataset == 'year_prediction':
        n_neurons = 100
    else:
        n_neurons = 50
    args.n_clusters = min(args.n_clusters, X.shape[0])

    y, y_mean, y_std = normalize_y(y)

    cluster_alg = KMeans(args.n_clusters)
    cluster_alg.fit(X)
    c = cluster_alg.cluster_centers_
    if torch.cuda.is_available() and args.cuda:
        c = torch.tensor(c).to(torch.float32).to('cuda')
    else:
        c = torch.tensor(c).to(torch.float32)

    mean = torch.nn.Sequential(torch.nn.Linear(X.shape[1], n_neurons),
                               torch.nn.ReLU(), torch.nn.Linear(n_neurons, 1))
    var = torch.nn.Sequential(RBF(None, None, c, 1.0),
                              PosLinear(args.n_clusters, 1, bias=False),
                              Reciprocal(0.1), PosLinear(1, 1, bias=False))

    if torch.cuda.is_available() and args.cuda:
        mean.cuda()
        var.cuda()
        device = torch.device('cuda')
    else:
        device = torch.device('cpu')

    optimizer = torch.optim.Adam(chain(mean.parameters(), var.parameters()),
                                 lr=args.lr)
    it = 0
    progressBar = tqdm(desc='Training nn', total=args.iters, unit='iter')
    batches = batchify(X, y, batch_size=args.batch_size, shuffel=args.shuffel)
    while it < args.iters:
        switch = 1.0 if it > args.iters / 2 else 0.0
        optimizer.zero_grad()
        data, label = next(batches)
        data = torch.tensor(data).to(torch.float32).to(device)
        label = torch.tensor(label).to(torch.float32).to(device)
        m, v = mean(data), var(data)
        v = switch * v + (1 - switch) * torch.tensor([0.02**2], device=device)
        loss = normal_log_prob(label, m, v).sum()
        (-loss).backward()
        optimizer.step()
        it += 1
        progressBar.update()
        progressBar.set_postfix({'loss': loss.item()})
    progressBar.close()

    data = torch.tensor(Xval).to(torch.float32).to(device)
    label = torch.tensor(yval).to(torch.float32).to(device)
    m, v = mean(data), var(data)
    m = m * y_std + y_mean
    v = v * y_std**2
    log_px = normal_log_prob(label, m, v)
    rmse = ((label - m.flatten())**2).mean().sqrt()
    return log_px.mean().item(), rmse.item()
    def __init__(self, latent_size=2, cuda=True):
        super(rbf, self).__init__(latent_size, cuda)

        self.enc_mu = nn.Sequential(nn.Linear(4, n_neurons), nn.ReLU(),
                                    nn.Linear(n_neurons, self.latent_size))
        self.enc_var = nn.Sequential(nn.Linear(4, n_neurons), nn.ReLU(),
                                     nn.Linear(n_neurons, self.latent_size),
                                     nn.Softplus())
        self.dec_mu = nn.Sequential(nn.Linear(self.latent_size, n_neurons),
                                    nn.ReLU(), nn.Linear(n_neurons, 4))
        self.dec_var = nn.Sequential(RBF(self.latent_size, 50, None, 5),
                                     PosLinear(50, 1, bias=False),
                                     Reciprocal(0.1),
                                     PosLinear(1, 1, bias=False))
示例#3
0
    def fit(self, X, Y):
        if len(Y.shape) == 1:
            Y = Y.reshape(-1, 1)
        likelihood = Normal(np.var(Y, 0))
        n, d = X.shape[0], X.shape[1]
        layers_kernel = []
        for i_layer in range(self.n_layer):
            layers_kernel.append(RBF(d, scale=np.sqrt(d)))

        self.model = DGP(X,
                         Y,
                         self.n_inducing,
                         layers_kernel,
                         likelihood,
                         window_size=self.window_size)

        for i_iter in range(self.n_iter):
            self.model.train()
            if i_iter % 100 == 0:
                print('Iter {}'.format(i_iter))
                self.model.print_MLL()
            if i_iter % 1000 == 0:
                self.model.sampling(self.n_sample, self.sample_space)