def and_prediction():
    and_preceptron = Preceptron(3)
    #     input 1, input 2, bias
    val0 = [0,0, 1]
    val1 = [0,1, 1]
    val2 = [1,0, 1]
    val3 = [1,1, 1]
    val_labels = [0, 0, 0, 1]
    inputs = [val0, val1, val2, val3]

    for _ in range(100):
        rand_num = random.randrange(0, 4)
        input = inputs[rand_num]
        label = val_labels[rand_num]
        # expects target to be -1 or 1
        if label == 0:
            label = -1
        else:
            labe = 1
        # print('randnum:',rand_num, 'input:', input, 'label:', label)
        and_preceptron.train(input, label)

    print("x and y")
    for i in inputs:
        # return -1 or 1
        pred = and_preceptron.feed_forward(i)
        # print("pred", pred)
        if pred == -1:
            pred = 0
        else:
            pred = 1
        print("{} | {} -> {}".format(i[0], i[1], pred))
Beispiel #2
0
def main():
    #               number of inputs
    p_model = Preceptron(2)

    # here is our training data (just some random points generated with a label)
    points = []
    num_points = 100
    for _ in range(num_points):
        points.append(Point())

    # split the data into training and testing data
    train_points, test_points = Point.split_points(points)

    EPOCHS = 10
    for epoch in range(EPOCHS):
        # training happens here
        for _ in range(200):
            rand_num_indx = random.randrange(0, len(train_points)) # choose a random index for points
            rand_train_point = train_points[rand_num_indx]
            # inputs array
            training_inputs = [rand_train_point.x, rand_train_point.y]
            # where is when we adjust the weights by training the model
            p_model.train(training_inputs, rand_train_point.label)

        accuracy = p_model.accuracy(test_points)
        print("Epoch:", epoch+1, "out of", EPOCHS, "accuracy:", accuracy)

        # dont train further if accuracy is perfect
        if accuracy == 1.0:
            break;

    # graph the lines and points of the models
    graph_model(points, p_model)
def not_prediction():
    not_preceptron = Preceptron(2)
    #     input 1, bias
    val0 = [0, 1]
    val1 = [1, 1]
    val_labels = [1, 0]
    inputs = [val0, val1]

    for _ in range(100):
        rand_num = random.randrange(0, 2)
        input = inputs[rand_num]
        label = val_labels[rand_num]
        # expects target to be -1 or 1
        if label == 0:
            label = -1
        else:
            labe = 1
        # print('randnum:',rand_num, 'input:', input, 'label:', label)
        not_preceptron.train(input, label)

    print("not x")
    for i in inputs:
        # return -1 or 1
        pred = not_preceptron.feed_forward(i)
        # print("pred", pred)
        if pred == -1:
            pred = 0
        else:
            pred = 1
        print("{} -> {}".format(i[0], pred))
Beispiel #4
0
def main():
    model_3d = Preceptron(3)

    points = []
    num_points = 100
    for _ in range(num_points):
        points.append(Point())

    # split the data into training and testing data
    train_points, test_points = Point.split_points(points)

    EPOCHS = 10
    BATCH_SIZE = 300
    for epoch in range(EPOCHS):
        # training happens here
        for _ in range(BATCH_SIZE):
            rand_num_indx = random.randrange(len(train_points)) # choose a random index for points
            rand_train_point = train_points[rand_num_indx]
            # inputs array
            training_inputs = [rand_train_point.x, rand_train_point.y, rand_train_point.z]
            # where is when we adjust the weights by training the model
            model_3d.train(training_inputs, rand_train_point.label)

        accuracy = model_3d.accuracy(test_points)
        print("Epoch: {:2d} out of {:2d} accuracy: {:.2f}".format(epoch+1, EPOCHS, accuracy))

        if accuracy == 1.0:
            break

    graph_3d_points(points, model_3d)
Beispiel #5
0
 def test_train_method_2(self):
     inputs = [0, 0]
     weights = [1,1,1]
     target = 0
     p = Preceptron(2, weights)
     p.lr = 1
     result = p.feed_forward(inputs)
     self.assertEqual(result, 1)
     result = p.train(inputs, target)
     self.assertEqual(result.data, [1,1,0])
Beispiel #6
0
    def test_train_method(self):
        # 2 inputs: x1, x2
        # x1*w1 x2*w2 + 1*w3
        inputs = [0,0]
        weights = [0,0,0] # last one is the bias
        target = 1

        p = Preceptron(2, weights)
        self.assertEqual(p.num_weights, 2)
        self.assertEqual(len(p.weights.data), 3)
        p.lr = 1
        result = p.feed_forward(inputs)
        self.assertEqual(result, 1)

        result = p.train(inputs, target)
        self.assertEqual(result.data, [0,0,0])
Beispiel #7
0
def main():
    or_preceptron = Preceptron(3)
    #     input 1, input 2, bias
    val0 = [0, 0, 1]
    val1 = [0, 1, 1]
    val2 = [1, 0, 1]
    val3 = [1, 1, 1]
    val_labels = [0, 1, 1, 1]
    inputs = [val0, val1, val2, val3]

    weights = or_preceptron.weights
    w0 = weights[0]
    w1 = weights[1]
    w2 = weights[2]

    # (sum(x*w0+y*w1+w2))
    value = val0[0] * w0 + val0[1] * w1 + w2
    sign_value = sign(value)

    ##########
    print("{:10s}".format("inputs"))
    # for i in range(len(inputs)):
    space_20 = "                    "
    space_5 = "     "
    dashes_5 = "-----"
    dashes_20 = "--------------------"
    line1 = space_20
    line2 = "   sign[sigma(Ii + wi)]"
    line3 = " = sign[x*w0 + y*w1 + w2]"
    line4 = " = sign[{}*{:.2f}+{}*{:.1f}+{:.1f}]".format(
        val0[0], w0, val0[1], w1, val0[2], w2)
    line5 = " = sign[{:.2f}] {:.11s}".format(value, space_5)
    line6 = " = {:1} {:.14s}".format(sign_value, space_20)
    line7 = space_20
    print("{:.5s} INPUTS/BIAS ".format(space_5), end="")
    print("{:.25s} PRECEPTRON ".format(space_5), end="")
    print("{:.40s} OUTPUT ".format(space_20))
    # print("{:.20s} ----------- ".format(space_20), end="")
    # print("{:.25s} -----------".format(space_5), end="")
    # print("{:.25s} ------ ".format(space_5+space_5))

    print("{:.20s}+{:.30s}+".format(space_20, 2 * dashes_20))
    print("{:.20s}|{:.30s}{:10s}|".format(space_20, line1, space_5))
    print("{:.20s}|{:.30s}{:7s}|".format(space_20, line2, space_5))
    print("{:.20s}|{:.30s}{:5s}|".format(space_20, line3, space_5))
    print("{:.20s}|{:.30s}{:5s}|".format(space_20, line4, space_5))
    print("{:.20s}|{:.30s}{:10s}|".format(space_20, line5, space_5))
    print("{:.20s}|{:.30s}{:10s}|".format(space_20, line6, space_5))
    print("{:.20s}|{:.30s}{:10s}|".format(space_20, line7, space_5))
    print("{:.20s}+{:.30s}+".format(space_20, 2 * dashes_20))

    ##########
    '''
Beispiel #8
0
 def test_init_with_custom_weights(self):
     # give invalid weights
     with self.assertRaises(Exception):
         p = Preceptron(2, [1,1])
Beispiel #9
0
 def test_init(self):
     p = Preceptron(2)
     self.assertEqual(p.lr, 0.1)
Beispiel #10
0
 def test_feed_forward_with_invalid_input(self):
     p = Preceptron(2, [0,0,0])
     
     with self.assertRaises(Exception):
         p.feed_forward([0])
Beispiel #11
0
 def test_feed_forward_with_valid_input(self):
     p = Preceptron(2, [0,0,0])
     result = p.feed_forward([0,0])
     self.assertEqual(result, 1)
from preceptron import Preceptron
from sklearn import datasets
from sklearn.model_selection import train_test_split
import numpy as np
model = Preceptron()


def accuracy(y_true, y_pred):
    acc = np.sum(y_true == y_pred) / len(y_true)
    return acc


X, y = datasets.make_blobs(n_samples=150,
                           n_features=2,
                           centers=2,
                           cluster_std=1.05,
                           random_state=2)
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=123)
model.fit(X_train, y_train)
pred = model.predict(X_test)
print(accuracy(y_test, pred))
def main():

    # generate a 3d ponit with a label
    # point = Point()
    # generate a 3d ponit with a label
    points = [Point() for _ in range(10)]
    # print(points)

    prec_x_val = Preceptron(2)
    prec_y_val = Preceptron(2)
    prec_z_val = Preceptron(2)

    for _ in range(10):
        for _ in range(100):
            indx = random.randrange(len(points))
            point = points[indx]
            prec_x_val.train([point.t, point.r[0]], point.label[0])
            prec_y_val.train([point.t, point.r[1]], point.label[1])
            prec_z_val.train([point.t, point.r[2]], point.label[2])

    point = Point()
    fed = [
        prec_x_val.feed_forward([point.t, point.r[0]]),
        prec_y_val.feed_forward([point.t, point.r[1]]),
        prec_z_val.feed_forward([point.t, point.r[2]])
    ]

    print("predicted:", fed)
    print("expected:", point.label)