コード例 #1
0
ファイル: cloudpred.py プロジェクト: bryanhe/CloudPred
def train(Xtrain, Xvalid, centers=2, regression=False):
    X = np.concatenate([x for (x, *_) in Xtrain])

    gm = []
    for X, *_ in Xtrain:
        gm.append(X)

    gm = np.concatenate(gm)
    model = sklearn.mixture.GaussianMixture(centers, "diag")
    gm = model.fit(gm)

    component = [
        Gaussian(torch.Tensor(gm.means_[i, :]),
                 torch.Tensor(1. / gm.covariances_[i, :]))
        for i in range(centers)
    ]
    mixture = Mixture(component, gm.weights_)
    classifier = DensityClassifier(mixture, centers, 2)

    X = torch.cat([
        mixture(torch.Tensor(X)).unsqueeze_(0).detach()
        for (X, y, *_) in Xtrain
    ])
    if regression:
        y = torch.FloatTensor([y for (X, y, *_) in Xtrain])
    else:
        y = torch.LongTensor([y for (X, y, *_) in Xtrain])

    Xv = torch.cat([
        mixture(torch.Tensor(X)).unsqueeze_(0).detach()
        for (X, y, *_) in Xvalid
    ])
    if regression:
        yv = torch.FloatTensor([y for (X, y, *_) in Xvalid])
    else:
        yv = torch.LongTensor([y for (X, y, *_) in Xvalid])

    logger = logging.getLogger(__name__)
    # Set weights of classifier
    for lr in [1e2, 1e1, 1e0, 1e-1, 1e-2, 1e-3]:
        optimizer = torch.optim.SGD(classifier.pl.parameters(),
                                    lr=lr,
                                    momentum=0.9)
        if regression:
            criterion = torch.nn.modules.MSELoss()
        else:
            criterion = torch.nn.modules.CrossEntropyLoss()
        best_loss = float("inf")
        best_model = copy.deepcopy(classifier.pl.state_dict())
        logger.debug("Learning rate: " + str(lr))
        for i in range(1000):
            z = classifier.pl(X)
            if regression:
                z = z[:, 1]
            loss = criterion(z, y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            zv = classifier.pl(Xv)
            if regression:
                zv = zv[:, 1]
            loss = criterion(zv, yv)
            if i % 100 == 0:
                logger.debug(str(loss.detach().numpy()))
            if loss < best_loss:
                best_loss = loss
                best_model = copy.deepcopy(classifier.pl.state_dict())
        classifier.pl.load_state_dict(best_model)

    reg = None
    return cloudpred.utils.train_classifier(Xtrain,
                                            Xvalid, [],
                                            classifier,
                                            regularize=reg,
                                            iterations=1000,
                                            eta=1e-4,
                                            stochastic=True,
                                            regression=regression)
コード例 #2
0
def get_ll(X, mu):
    sm = 0
    for x in X:
        sm += log(mixture(x, [0.5, 0.5], mu))
    return sm
コード例 #3
0
def log_lh(X, mu):
    s = 0
    for i in X:
        s += np.log(mixture(i, [0.5, 0.5], mu))
    return s
コード例 #4
0
def get_ll(X, mu):
    sm = 0
    for x in X:
        sm += log(mixture(x, [0.5, 0.5], mu))
    return sm