def main(): """Run perceptron algorithm on AND dataset. """ # construct the AND dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [0], [0], [1]]) # define our perceptron and train it print("[INFO] training perceptron...") perceptron = Perceptron(X.shape[1], alpha=0.1) perceptron.fit(X, y, epochs=20) # now that our perceptron is trained we can evaluate it print("[INFO] testing perceptron...") # now that our network is trained, loop over the data points for (value, target) in zip(X, y): # make a prediction on the data point and display the result to our console pred = perceptron.predict(value) print("[INFO] data={}, ground-truth={}, pred={}".format( value, target[0], pred))
# -*- coding: utf-8 -*- # perceptron_or.py import numpy as np from pyimagesearch.nn import Perceptron # construct the OR dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [1]]) # define our perceptron and train it print("[INFO] training perceptron...") p = Perceptron(X.shape[1], alpha=0.1) p.fit(X, y, epochs=20) # now that out perceptron is trained we can evaluate it print("[INFO] testing perceptron...") # now that out network is trained, loop over the data points for (x, target) in zip(X, y): # make a prediction on the data point and display the # result to our console pred = p.predict(x) print("[INFO] data = {}, ground-truth = {}, pred = {}".format( x, target[0], pred))
# USAGE # python perceptron_and.py # import the necessary packages from pyimagesearch.nn import Perceptron import numpy as np # construct the AND dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [0], [0], [1]]) # define our perceptron and train it print("[INFO] training perceptron...") p = Perceptron(X.shape[1], alpha=0.1) p.fit(X, y, epochs=20) # now that our perceptron is trained we can evaluate it print("[INFO] testing perceptron...") # now that our network is trained, loop over the data points for (x, target) in zip(X, y): # make a prediction on the data point and display the result # to our console pred = p.predict(x) print("[INFO] data={}, ground-truth={}, pred={}".format( x, target[0], pred))