Esempio n. 1
0
        classes = [[x for x, y in zip(X, Y) if y == c] for c in np.unique(Y)]
        self.model = self.stat(classes)
        return self

    # calculate the gaussian probability given mean and sd
    def gaussian(self, x, mu, sd):
        if sd == 0: # when sd is 0 emulate a Dirac delta function
            if x == mu:
                return 1;
            else:
                return 0;
        exponent = np.exp(-((x - mu)**2/(2*sd**2)))
        gaussian_prob = exponent/(np.sqrt(2*np.pi)*sd)
        if gaussian_prob == 0:
            return 0
        return np.log(gaussian_prob)

    # predict an unlabeled dataset
    def predict(self, X):
        prob_mat = [[sum(self.gaussian(i, *s) for s, i in zip(stat, x)) for stat in self.model] for x in X]
        return np.argmax(prob_mat, axis=1)

if __name__ == '__main__':
    train_x, train_y, test_x, test_y = util.load_mnist()
    model = GaussianNB()
    model.fit(train_x, train_y)
    pred = model.predict(test_x)
    cm = util.confusion_matrix(test_y, pred)
    print(cm)
    print(util.accuracy(cm))
Esempio n. 2
0
def test_confusion():
    x = util.confusion_matrix("toy.csv", "a", "b")
    assert np.array_equal(x[0], np.matrix([[0.75, 0.25], [0, 1.0]])) == True
    assert x[1] == ["0", "6"]
Esempio n. 3
0
            writer.add_scalar('loss/text', mean_losses[0], global_idx)
            writer.add_scalar('loss/gate', mean_losses[1], global_idx)
            writer.add_scalar('loss_total', mean_losses[2], global_idx)

            writer.add_scalar('grad_norm/enc', mean_metrics[0], global_idx)
            writer.add_scalar('grad_norm/dec', mean_metrics[1], global_idx)
            writer.add_scalar('alpha/enc', mean_metrics[2], global_idx)
            writer.add_scalar('alpha/dec', mean_metrics[3], global_idx)
            writer.add_scalar('gate_val', mean_metrics[4], global_idx)
            mean_losses = np.zeros_like(mean_losses)
            mean_metrics = np.zeros_like(mean_metrics)
            summ_counter = 0

            writer.add_figure(
                'batch_CM',
                confusion_matrix(text_out, text_data, [*_symbol_to_id]),
                global_idx)

            writer.add_text(
                f'text/input',
                text_id_to_string(text_data[:4], text_len=text_len[:4]),
                global_idx)

            gate_out[:, -1] = 1
            text_len = (gate_out.squeeze(-1) > .9).to(torch.float)
            text_len = text_len / (
                torch.arange(gate_out.size(1), device=device) + 1.)
            text_len = text_len.argmax(-1) + 1

            text_out = text_out.argmax(-1)
            writer.add_text(
Esempio n. 4
0
def test_confusion():
    x = util.confusion_matrix('toy.csv', 'a', 'b')
    assert np.array_equal(x[0], np.matrix([[0.75, 0.25], [0, 1.0]])) == True
    assert x[1] == ['0', '6']