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))
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))
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)
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])
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)
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])