示例#1
0
def part_b(file_n):
    f = open(file_n, "r")
    ip = int(f.readline())
    op = int(f.readline())
    batch = int(f.readline())
    n = int(f.readline())
    h = (f.readline()).rstrip().split(" ")
    h = map(int, h)
    h = [ip] + h + [op]
    if f.readline() == "relu\n":
        non_lin = 1
    else:
        non_lin = 0
    if f.readline() == "fixed\n":
        eta = 0
    else:
        eta = 1
    print ip, op, batch, n
    print h
    print non_lin, eta
    start = timeit.default_timer()
    net = NeuralNet(h, bool(non_lin))
    net.grad_des(x[:, 0:-1], x[:, -1], batch, bool(eta))
    stop = timeit.default_timer()
    t_acc = 100 * net.score(x[:, 0:-1], x[:, -1])
    ts_acc = 100 * net.score(tests[:, 0:-1], tests[:, -1])
    print "Train accuracy ", t_acc
    print "Test accuracy ", ts_acc
    print "Training time ", (stop - start)
    conf = confusion_matrix(tests[:, -1].tolist(), net.pred(tests[:, 0:-1]))
    plot_confusion(conf, list(set(tests[:, -1].flatten().tolist())),
                   "For layers " + str(h))
示例#2
0
def part_d(eta_a=False, rlu=False):
    tt = np.zeros((5, 2))
    m = 0
    h = [85, 0, 0, 10]
    l = [5, 10, 15, 20, 25]
    for i in [5, 10, 15, 20, 25]:
        print "For 2 layer ", i, eta_a, rlu
        h[1] = i
        h[2] = i
        start = timeit.default_timer()
        net = NeuralNet(h, rlu)
        net.grad_des(x[:, 0:-1], x[:, -1], 100, eta_a)
        stop = timeit.default_timer()
        t_acc = 100 * net.score(x[:, 0:-1], x[:, -1])
        ts_acc = 100 * net.score(tests[:, 0:-1], tests[:, -1])
        f_ptr.write("\nFor double layer " + str(eta_a) + str(rlu))
        f_ptr.write(str(i))
        f_ptr.write("\nTraining acc ")
        f_ptr.write(str(t_acc))
        f_ptr.write("\nTesting acc ")
        f_ptr.write(str(ts_acc))
        f_ptr.write("\nTrainig time ")
        f_ptr.write(str(stop - start))
        print "Train accuracy ", t_acc
        print "Test accuracy ", ts_acc
        print "Training time ", (stop - start)
        tt[m, 0] = t_acc
        tt[m, 1] = ts_acc
        m = m + 1
        conf = confusion_matrix(tests[:, -1].tolist(), net.pred(tests[:,
                                                                      0:-1]))
        plot_confusion(conf, list(set(tests[:, -1].flatten().tolist())),
                       "For 2 layers " + str(h) + str(eta_a) + str(rlu))
    print tt
    plot_metric(tt, l, "For two hidden layers " + str(eta_a) + str(rlu))