예제 #1
0
 def add_data(self, new_data):
     new_data = [torch.array(nd) for nd in new_data]
     _ = [
         v.add_new_data(new_data)
         if k == 'data' else v.expand_size(new_data)
         for k, v in self.attrs.items()
     ]
예제 #2
0
 def forward(self, inputs):
     # xp = cuda.get_array_module(*inputs)
     x, t = inputs
     t = t[:, None]
     positive, unlabeled = t == self.positive, t == self.unlabeled
     n_positive, n_unlabeled = max([1., torch.sum(positive)
                                    ]), max([1., torch.sum(unlabeled)])
     # x_in = Variable(x)
     x_in = x
     y_positive = self.loss_func(x_in)
     y_unlabeled = self.loss_func(-x_in)
     positive_risk = torch.sum(self.prior * positive / n_positive *
                               y_positive)
     negative_risk = torch.sum(
         (unlabeled / n_unlabeled - self.prior * positive / n_positive) *
         y_unlabeled)
     objective = positive_risk + negative_risk
     if self.nnPU:
         if negative_risk.data < -self.beta:
             objective = positive_risk - self.beta
             self.x_out = -self.gamma * negative_risk
         else:
             self.x_out = objective
     else:
         self.x_out = objective
     loss = torch.array(objective.data, dtype=self.x_out.data.dtype)
     return loss
예제 #3
0
def make_2D_samples_gauss(n, m, sigma, random_state=None):
    """Return n samples drawn from 2D gaussian N(m,sigma)

    Parameters
    ----------
    n : int
        number of samples to make
    m : ndarray, shape (2,)
        mean value of the gaussian distribution
    sigma : ndarray, shape (2, 2)
        covariance matrix of the gaussian distribution
    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Returns
    -------
    X : ndarray, shape (n, 2)
        n samples drawn from N(m, sigma).
    """

    generator = check_random_state(random_state)
    if torch.isscalar(sigma):
        sigma = torch.array([sigma, ])
    if len(sigma) > 1:
        P = sp.linalg.sqrtm(sigma)
        res = generator.randn(n, 2).matmul(P) + m
    else:
        res = generator.randn(n, 2) * torch.sqrt(sigma) + m
    return res
예제 #4
0
 def _optimLSolve(self, x, b):
     raise RuntimeError
     self.X = x.clone()
     _dim = self._k * (self._n + self._m)
     shape = (_dim, _dim)
     sol, _iters = cg(LinearOperator(shape, matvec=self._matvec),
                      convert2numpy(b),
                      tol=1e-6,
                      maxiter=100)
     sol = sol.reshape(self._k, int(sol.shape[0] / self._k))
     del self.X
     alpha, beta = sol[:, :self._n], sol[:, self._n:]
     return torch.array(alpha), proc.array(beta)
예제 #5
0
    def predict_proba(self, idx=None, loader=None):
        if self.loader is not None:
            loader = self.loader
        if loader is None:
            is_test_idx = (len(idx) == MNIST_TEST_SIZE) and (
                torch.array(idx) == torch.arange(MNIST_TEST_SIZE)).all()
            loader = 'test' if is_test_idx else 'train'
        dataset = datasets.MNIST(
            root='/home/zhaok14/example/PycharmProjects/cnn_scratch/MNIST_data',
            train=(loader == 'train'),
            download=True,
            transform=transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize((0.1307, ), (0.3081, ))
            ]))
        # Filter by idx
        if idx is not None:
            if loader == 'train' and len(idx) != MNIST_TRAIN_SIZE:
                dataset.train_data = dataset.train_data[idx]
                dataset.train_labels = dataset.train_labels[idx]
            elif loader == 'test' and len(idx) != MNIST_TEST_SIZE:
                dataset.test_data = dataset.test_data[idx]
                dataset.test_labels = dataset.test_labels[idx]

        loader = torch.utils.data.DataLoader(
            dataset=dataset,
            batch_size=self.batch_size
            if loader == 'train' else self.test_batch_size,
            **self.loader_kwargs)

        # sets model.train(False) inactivating dropout and batch-norm layers
        self.model.eval()

        # Run forward pass on model to compute outputs
        outputs = []
        for data, _ in loader:
            if self.cuda:
                data = data.cuda()
            data = Variable(data, volatile=True)
            output = self.model(data)
            outputs.append(output)

        # Outputs are log_softmax (log probabilities)
        outputs = torch.cat(outputs, dim=0)
        # Convert to probabilities and return the numpy array of shape N x K
        # pred = torch.exp(outputs.data.numpy())
        pred = torch.exp(outputs.data.cpu())
        print(type(pred.numpy()))
        return pred.numpy()
예제 #6
0
        self.decoder_1.append(nn.Conv1d(1,1,kernel_size=2))
        
        self.decoder_2 = nn.ModuleList()
        self.decoder_2.append(nn.Conv1d(1,1,kernel_size=2))
    def forward(self, x, decoder_idx):
        for f in self.encoder:
            x = f(x)
        if idx == 1:
            for f in self.decoder_1:
                x = f(x)
        elif idx == 2:
            for f in self.decoder_2:
                x = f(x)
                x = f(x)
        return x

model = myModel()

x = torch.array([10])

optimizer = 

out = model(x, 1)
optimizer.zero_grad()
loss.backward()
optimizer.step()

out = model(x, 2)
optimizer.zero_grad()
loss.backward()
optimizer.step()
예제 #7
0
def make_data_classif(dataset, n, nz=.5, theta=0, random_state=None, **kwargs):
    """Dataset generation for classification problems

    Parameters
    ----------
    dataset : str
        type of classification problem (see code)
    n : int
        number of training samples
    nz : float
        noise level (>0)
    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Returns
    -------
    X : ndarray, shape (n, d)
        n observation of size d
    y : ndarray, shape (n,)
        labels of the samples.
    """
    generator = check_random_state(random_state)

    if dataset.lower() == '3gauss':
        y = torch.floor((np.arange(n) * 1.0 / n * 3)) + 1
        x = torch.zeros((n, 2))
        # class 1
        x[y == 1, 0] = -1.
        x[y == 1, 1] = -1.
        x[y == 2, 0] = -1.
        x[y == 2, 1] = 1.
        x[y == 3, 0] = 1.
        x[y == 3, 1] = 0

        x[y != 3, :] += 1.5 * nz * generator.randn(sum(y != 3), 2)
        x[y == 3, :] += 2 * nz * generator.randn(sum(y == 3), 2)

    elif dataset.lower() == '3gauss2':
        y = torch.floor((np.arange(n) * 1.0 / n * 3)) + 1
        x = torch.zeros((n, 2))
        y[y == 4] = 3
        # class 1
        x[y == 1, 0] = -2.
        x[y == 1, 1] = -2.
        x[y == 2, 0] = -2.
        x[y == 2, 1] = 2.
        x[y == 3, 0] = 2.
        x[y == 3, 1] = 0

        x[y != 3, :] += nz * generator.randn(sum(y != 3), 2)
        x[y == 3, :] += 2 * nz * generator.randn(sum(y == 3), 2)

    elif dataset.lower() == 'gaussrot':
        rot = torch.array(
            [[torch.cos(theta), torch.sin(theta)], [-torch.sin(theta), torch.cos(theta)]])
        m1 = torch.Tensor([-1, 1])
        m2 = torch.Tensor([1, -1])
        y = torch.floor((torch.arange(n) * 1.0 / n * 2)) + 1
        n1 = torch.sum(y == 1)
        n2 = torch.sum(y == 2)
        x = torch.zeros((n, 2))

        x[y == 1, :] = get_2D_samples_gauss(n1, m1, nz, random_state=generator)
        x[y == 2, :] = get_2D_samples_gauss(n2, m2, nz, random_state=generator)

        x = x.matmul(rot)

    else:
        x = torch.zeros(1)
        y = torch.zeros(1)
        print("unknown dataset")

    return x, y.int()