Example #1
0
def solve_logical_examples():
    #create arrays (0,0), (0,1), (1,0), (1,1)
    net_input = [na.array((x1, x2)) for x1 in range(2) for x2 in range(2)]
    # create expected output
    net_output = [-1, 1, 1, 1]

    a, b = dual_perceptron(net_input, net_output)
    print "dual form: ", a, b

    c, d, k = perceptron(net_input, net_output, .5)
    print "perceptron: ", c, d

    print "printing perceptron output: "
    m = -c[0] / c[1]
    d = -d / c[1]
    plot_line(m, d, (-.5, 1.5), (-.5, 1.5))

    #solve for the weight vector
    sum_j = na.zeros(2, na.Float32)
    for j in range(len(net_input)):
        sum_j += a[j] * net_output[j] * net_input[j]
    print "sum_j: ", sum_j

    #solve for x2 (which is sum_j[1]); assume 2-d
    b = -b / sum_j[1]
    co_x1 = -sum_j[0] / sum_j[1]

    print "printing dual form perceptron output: "
    plot_line(co_x1, b, (-.5, 1.5), (-.5, 1.5))
Example #2
0
def main():
    start = time.clock()
    classifier = perceptron()
    # classifier = differentiable_perceptron.diff_perceptron()

    trainingData = readDataFile("digitdata/trainingimages", 5000)
    trainingLabel = readLabelFile("digitdata/traininglabels", 5000)

    testData = readDataFile("digitdata/testimages", 1000)
    testLabel = readLabelFile("digitdata/testlabels", 1000)
    # change the following settings like number of epochs,
    # the ordering of training(random vs natural order), etc.
    classifier.setEpoch(100)
    classifier.setDecay(1000)
    classifier.setRandom(True)
    classifier.setRandomize(True)

    # ready for training
    print("Ready for training.")
    classifier.train(trainingData, trainingLabel)
    print("Training is done.")
    accuracy, confusion_matrix = classifier.classify(testData, testLabel)
    print("classification is done.")
    print("-----------------------------------------------------------")
    print("The accuracy is: ", accuracy)
    print("Execution time is: %fs" % (time.clock() - start))
    print(confusion_matrix)
    classifier.training_curve_plot()
Example #3
0
def solve_logical_examples():
    #create arrays (0,0), (0,1), (1,0), (1,1)
    net_input = [na.array((x1,x2)) for x1 in range(2) for x2 in range(2)]
    # create expected output
    net_output = [-1,1,1,1]
    
    a,b = dual_perceptron(net_input, net_output)
    print "dual form: ", a, b
    
    c,d,k = perceptron(net_input, net_output, .5)
    print "perceptron: ", c, d

    print "printing perceptron output: "
    m = -c[0] / c[1]
    d = -d / c[1]
    plot_line(m, d, (-.5, 1.5), (-.5, 1.5))
    
    #solve for the weight vector
    sum_j = na.zeros(2, na.Float32)
    for j in range(len(net_input)):
        sum_j += a[j] * net_output[j] * net_input[j]
    print "sum_j: ", sum_j
    
    #solve for x2 (which is sum_j[1]); assume 2-d
    b = -b / sum_j[1]
    co_x1 = -sum_j[0] / sum_j[1]

    print "printing dual form perceptron output: " 
    plot_line(co_x1, b, (-.5, 1.5), (-.5, 1.5))
Example #4
0
def Q4():
    for m in M:
        X = np.random.multivariate_normal(mean, cov, m)
        x1, y1 = X.T
        X = np.hstack((X, np.ones((m, 1))))
        y = np.sign(np.dot(X, w_t))
        while (len(np.argwhere(y == 1)) == 0) or (len(np.argwhere(y == -1))
                                                  == 0):
            X = np.random.multivariate_normal(mean, cov, m)
            x1, y1 = X.T
            X = np.hstack((X, np.ones((m, 1))))
            y = np.sign(np.dot(X, w_t))

        svmcls = SVC(C=1e10, kernel='linear')
        p = perceptron()
        svmcls.fit(X, y)
        w_p = p.fit(X, y)
        plt.scatter(x1, y1, c=y)
        plt.plot(u_0, interval(w_t, u_0), label='true')
        plt.plot(u_0, interval(w_p, u_0), label='perceptron')
        plt.plot(u_0, interval(np.squeeze(svmcls.coef_), u_0), label='svm')
        plt.title('SVM and perceptron as fuction of m={}'.format(m))
        plt.legend()
        plt.savefig('m{}'.format(m))
        plt.show()
Example #5
0
def pla(a):
    data_test, data_train, labels_test, labels_train = partitioner_l(a, 40)
    labels_test = binary_class(labels_test)
    labels_train = binary_class(labels_train)
    w = perceptron(data_train, labels_train)

    p_train = check_accuracy(data_train, labels_train, w)
    p_test = check_accuracy(data_test, labels_test, w)

    print("TRAINING ACCURACY: " + str(p_train))
    print("TESTING ACCURACY: " + str(p_test))

    show_line(data_train, labels_train)
Example #6
0
def CIFAR10_predict(I, CIFAR10_data):
    if print_log:
        print "==> START PREDICTION OF NETWORK ", CIFAR10_data["name"], "\n"

    if print_log:
        print "STEP 1 | CONV + RELU + MP"
    # CONVOLUTION 1
    image_out = convolution(I, CIFAR10_data["WEIGHTS_CONV_1"],
                            CIFAR10_data["BIAS_CONV_1"])
    # RELU
    image_out = relu(image_out)

    # MAX_POOL 1
    image_out = max_pool(image_out, CIFAR10_data["SIZE_STRIDE_MAX_POOL_1"][0],
                         CIFAR10_data["SIZE_STRIDE_MAX_POOL_1"][2])

    if print_log:
        print "STEP 2 | CONV + RELU + MP"
    # CONVOLUTION 2
    image_out = convolution(image_out, CIFAR10_data["WEIGHTS_CONV_2"],
                            CIFAR10_data["BIAS_CONV_2"])
    # RELU
    image_out = relu(image_out)

    # MAX_POOL 2
    image_out = max_pool(image_out, CIFAR10_data["SIZE_STRIDE_MAX_POOL_2"][0],
                         CIFAR10_data["SIZE_STRIDE_MAX_POOL_2"][2])

    if print_log:
        print "STEP 3 | CONV + RELU + MP"
    # CONVOLUTION 3
    image_out = convolution(image_out, CIFAR10_data["WEIGHTS_CONV_3"],
                            CIFAR10_data["BIAS_CONV_3"])
    # RELU
    image_out = relu(image_out)

    # MAX_POOL 3
    image_out = max_pool(image_out, CIFAR10_data["SIZE_STRIDE_MAX_POOL_3"][0],
                         CIFAR10_data["SIZE_STRIDE_MAX_POOL_3"][2])

    if print_log:
        print "RESHAPE"
    # RESHAPE
    vector = reshape(image_out)

    if print_log:
        print "PERCEPTRON\n"
    prob = perceptron(vector, CIFAR10_data["PERCEPTRON_WEIGHT"],
                      CIFAR10_data["PERCEPTRON_BIAS"])

    return prob
Example #7
0
def main():
    p = perceptron()
    p.baias = 1
    for i in range(50):
        p.train(numpy.array([0, 0]), 1)
        p.train(numpy.array([1, 0]), 0)
        p.train(numpy.array([0, 1]), 0)
        p.train(numpy.array([1, 1]), 0)

    print(p.calculate([0, 0]))
    print(p.calculate([1, 0]))
    print(p.calculate([0, 1]))
    print(p.calculate([1, 1]))

    print("weights -> ", p.weight)
    print("baias -> ", p.baias)
Example #8
0
def solve_apples_bananas():
    print "generating points"
    pts = gen_random_points(100)
    bananas = [pt[0] for pt in pts if pt[1] < 0]
    apples = [pt[0] for pt in pts if pt[1] > 0]
    plot_bananas(bananas, apples, 'points.png')

    print "running perceptron"
    cat = na.array([pt[1] for pt in pts])
    pts = na.array([pt[0] for pt in pts])
    w, b, k = perceptron(pts, cat, .3, 0)

    print "perceptron output: ", w, b, k
    m = -w[0] / w[1]
    b = -b / w[1]
    plot_bananas(bananas, apples, 'line.png', m, b)
Example #9
0
def solve_apples_bananas():
    print "generating points"
    pts = gen_random_points(100)
    bananas = [pt[0] for pt in pts if pt[1] < 0]
    apples = [pt[0] for pt in pts if pt[1] > 0]
    plot_bananas(bananas, apples, 'points.png')
    
    print "running perceptron"
    cat = na.array([pt[1] for pt in pts])
    pts = na.array([pt[0] for pt in pts])
    w, b, k = perceptron(pts, cat, .3, 0)

    print "perceptron output: ", w, b, k
    m = -w[0] / w[1]
    b = -b / w[1]
    plot_bananas(bananas, apples, 'line.png', m, b)
Example #10
0
 def question3(self):
     if self.count % 5 == 0:
         self.text.delete(1.0, END)
     self.count += 1
     self.text.insert(
         END, "Test case " + str(self.count) + " - Requirement 2:\n" + "\n")
     training_data, training_class = self.read_training_data_type2(
         self.data)
     test_data, test_class = self.gen_test_data2()
     error1 = perceptron(training_data, training_class, test_data,
                         test_class)
     error2 = single_layer_neural_network(training_data, training_class,
                                          test_data, test_class)
     self.text.insert(END, "Perceptron's error rate " + str(error1) + "\n")
     self.text.insert(END,
                      "Neural network's error rate " + str(error2) + "\n")
     self.text.insert(END, "------------\n")
     print("---------")
    def initNER(self):
        # ori_train_set = self.load_sentences(self.training_set)
        # print ori_train_set
        # self.get_dictionary(self.training_set)
        # line_eg = "Sheep NNP I-NP O"
        # feature generator got from training data
        # fea_vector_eg = self.fea_generator.feature_vector_gen(line_eg,'O')

        self.sentences = self.load_sentences(self.training_set)
        self.test_sentences = self.load_sentences(self.dev_set)
        self.fea_generator = fea_gen(self.training_set, self.unk_threshold,
                                     self.sentences, self.test_sentences)
        # token_eg = Token(line_eg)
        self.netags_list = list(self.fea_generator.netags.keys())
        print self.netags_list
        self.all_features_train_dict = self.fea_generator.compute_feature_matrix(
            self.sentences)
        self.all_features_test_dict = self.fea_generator.compute_feature_matrix(
            self.test_sentences)

        print self.fea_generator.feature_name_list

        ran_n = random.randint(1, 1000)
        output_name1 = 'all_feature_dict_' + str(
            ran_n) + self.training_set + '.p'
        output_name2 = 'all_feature_dict_' + str(ran_n) + self.dev_set + '.p'
        with open(output_name1, 'wb') as fp:
            pickle.dump(self.all_features_train_dict, fp)

        with open(output_name2, 'wb') as fp2:
            pickle.dump(self.all_features_test_dict, fp2)

        # do perceptron learn
        print("begin training")
        model = perceptron(self.fea_generator, self.sentences,
                           self.test_sentences, output_name1, output_name2)
        learned_w = model.uni_NEtagging()
        # print ("learned weights:")
        # # print learned_w
        # np.savetxt('learned_weights',learned_w,delimiter=',')
        print("begin inference")
        model.perceptron_predict()
        model.result_report()
        return None
Example #12
0
 def question2(self):
     if self.count % 5 == 0:
         self.text.delete(1.0, END)
     self.count += 1
     # print("Test case ", self.count)
     training_data, training_class = self.read_training_data_type1(
         self.data)
     test_data, test_class = self.gen_test_data()
     # print(len(test_data))
     error1 = perceptron(training_data, training_class, test_data,
                         test_class)
     error2 = logistic_algorithm(training_data, training_class, test_data,
                                 test_class)
     self.text.insert(
         END,
         "Test case " + str(self.count) + " - Requirement 1:\n " + "\n")
     self.text.insert(END, "Perceptron's error rate" + str(error1) + "\n")
     self.text.insert(
         END, "Logistic function's error rate " + str(error2) + "\n")
     self.text.insert(END, "------------\n")
     print("---------")
Example #13
0
def Q5():
    errs1 = np.zeros((5, 1))
    errs2 = np.zeros((5, 1))
    for k, m in enumerate(M):
        for i in range(500):
            # ****************** GENERATING DATA ****************** #
            X = np.random.multivariate_normal(mean, cov, m)
            y = true_h(X)
            while (len(np.argwhere(y == 1)) == 0) or (len(np.argwhere(y == -1))
                                                      == 0):
                X = np.random.multivariate_normal(mean, cov, m)
                y = true_h(X)
            # ****************** CLASSIFIERS ****************** #
            # SVM
            svmcls = SVC(C=1e10, kernel='linear')
            svmcls.fit(X, y)
            # Perceptron
            p = perceptron()
            X = np.hstack((X, np.ones((m, 1))))
            p.fit(X, y)
            # ****************** GENERATING TEST ****************** #
            test_points = np.random.multivariate_normal(mean, cov, TEST_SIZE)
            test_labels = true_h(test_points)
            # ****************** MAKE PREDICTIONS ****************** #
            svms_y_hat = svmcls.predict(test_points)
            test_points = np.hstack((test_points, np.ones((TEST_SIZE, 1))))
            ps_y_hat = p.predict(test_points)
            # ****************** GET ACCURACY ****************** #
            errs1[k] += get_accuracy(test_labels, svms_y_hat)
            errs2[k] += get_accuracy(test_labels, ps_y_hat)
    plt.plot(M, np.true_divide(errs1, 500), label='SVM')
    plt.plot(M, np.true_divide(errs2, 500), label="Perceptron")
    plt.title('Mean accuracy as function of m')
    plt.xlabel('samples(m)')
    plt.ylabel('Mean Accuracy')
    plt.legend()
    plt.savefig('Q5')
    plt.show()
Example #14
0
# variables init
n = int(content[0])							# no of perceptrons
variablecount = int(content[1])				# no of externalinputs
content = content[2:]						
content = [x.split(" ") for x in content]	
initwt = 1									# initialweights of all
network = []								# all perceptrons
intermediates = []							# inputs + intermediate perceptron outputs

# init intermediate
for i in range(variablecount+n):
	intermediates.append(0.0)

# init network
for i in range(n):
	p = perceptron(initwt)
	network.append(p)

# attach inputs to perceptron
for i in range(n):
	for j in content[i]:
		if j[0] == '_':
			network[i].inputs.append(int(j[1]))
		else:
			network[i].inputs.append(int(j[0])+variablecount)

# train network
with open('train.txt') as trainingfile:
	content = trainingfile.readlines()
content = [x.strip().split("\t") for x in content]
input = [x[0].split(' ')  for x in content]
                    help='test_num for knn')

if __name__ == '__main__':
    args = parser.parse_args()
    if args.exp_name == 'knn':
        args.split = 'both'

    # load data
    if args.dataset == 'mnist':
        data = Mnist(args)
    else:
        data = Mnist(args)

    # perform experiment
    if args.exp_name == 'perceptron':
        X, y = extract_1_5(data.X, data.y)

        # normalize to [-1, 1]
        lower_bound = -1 * np.ones(X.shape)
        X = lower_bound + 2 * (X / (X.max() - X.min()))

        perceptron(X, y, args)

    elif args.exp_name == 'knn':
        acc = [0 for _ in range(args.k_range)]
        # perform knn and "k" search
        for i in range(args.k_range):
            acc[i] = knn(data, args)
            args.k = args.k + 1
        draw_search(acc, args)
Example #16
0
from perceptron import *
import matplotlib.pyplot as plt

feature = np.array([
    [3,3],
    [4,3],
    [1,1],
])

label = np.array([
    [1],
    [1],
    [-1],
])

a = perceptron(feature, label)
a.train()

test_point = np.array([
    [+5,+5],
    [-1,-1]])
print a.prediction(test_point)

w,b = a.get_wandb()


#########plot##############################
def plot_function(samples, labels, w, b):
    index = 0
    for i in samples:
        if labels[index] == 1:
 def perceptron(self):
     self.rango_a = self.val_ra.get()
     self.epocas_max = self.val_epocas_max.get()
     self.ptron = perceptron(self)
     self.convergio = self.ptron.proceso()
Example #18
0
            else:
                cat2.append(row[:-1])
            target.append(row[-1])

c = list(zip(input, target))
shuffle(c)
input = [x[0] for x in c]
target = [x[1] for x in c]

input = np.array(input, float)
target = np.array(target, float)

targetp = np.array([x * 2 - 1 for x in target])

w = lms(input, target, args.eta, 100000)
wp = perceptron(input, targetp, args.eta, 100000)

validation = []
with open('classdataVal.csv', newline='') as f:
    r = csv.reader(f)
    for row in r:
        if row:
            validation.append(row)

validation = np.transpose(np.array(validation, float))
cat1 = np.transpose(np.array(cat1, float))
cat2 = np.transpose(np.array(cat2, float))
plt.scatter(cat1[0],
            cat1[1],
            c="b",
            marker=".",
Example #19
0
    description=
    'Clasificación de Iris setosa usando el algoritmo del perceptron.')
parser.add_argument('--eta',
                    type=float,
                    help='Tasa de aprendizaje. Por defecto es 0.05',
                    default=0.05)
args = parser.parse_args()

input = []
target = []
with open('bezdekIris.data', newline='') as f:
    r = csv.reader(f)
    for row in r:
        if row:
            input.append([1] + row[:-1])
            if row[-1] == 'Iris-setosa':
                target.append(1)
            else:
                target.append(-1)

c = list(zip(input, target))
shuffle(c)
input = [x[0] for x in c]
target = [x[1] for x in c]

input = np.array(input, float)
target = np.array(target)

w = perceptron(input, target, args.eta, 15000)

print("Pesos finales: ", w)
Example #20
0
from perceptron import *

import numpy as np

if __name__ == '__main__':
    print("Homework 1a")
    X = np.array([[-1, -1], [1, 0], [-1, 1.5]])
    y = np.array([1, -1, 1])
    perceptron(X, y, 2)

    print("Homework 1b")
    X = np.array([[-1, 1.5], [-1, -1], [1, 0]])
    y = np.array([1, 1, -1])
    perceptron(X, y, 2)

    print("Homework 1c")
    X = np.array([[-1, -1], [1, 0], [-1, 10]])
    y = np.array([1, -1, 1])
    perceptron(X, y, 2)

    print("Homework 1d")
    X = np.array([[1, 0], [-1, 10], [-1, -1]])
    y = np.array([-1, 1, 1])
    perceptron(X, y, 2)

    print("Homework 2")
    X = np.array([[-4, 2], [-2, 1], [-1, -1], [2, 2], [1, -2]])
    y = np.array([1, 1, -1, -1, -1])
    perceptron(X, y, 2)