def main(): """Train neural network implementation on the MNIST dataset. """ # load the MNIST dataset and apply min/max scaling to scale the pixel intensity values # to the range [0, 1] (each image is represented by an 8 x 8 = 64-dim feature vector) print("[INFO] loading MNIST (sample) dataset...") digits = datasets.load_digits() data = digits.data.astype("float") # pylint: disable=no-member data = (data - data.min()) / (data.max() - data.min()) print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1])) # construct the training and testing splits (train_x, test_x, train_y, test_y) = \ train_test_split(data, digits.target, test_size=0.25) # pylint: disable=no-member # convert the labels from integers to vectors using one-hot-encoding train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) # train the network print("[INFO] training network...") network = NeuralNetwork([train_x.shape[1], 32, 16, 10]) print("[INFO] {}".format(network)) network.fit(train_x, train_y, epochs=1000) # evaluate the network print("[INFO] evaluating network...") predictions = network.predict(test_x) predictions = predictions.argmax(axis=1) print(classification_report(test_y.argmax(axis=1), predictions))
def main(): """Run neural network on XOR dataset. """ # construct the XOR dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) # define our 2-2-1 neural network and train it network = NeuralNetwork([2, 2, 1], alpha=0.5) network.fit(X, y, epochs=20000) # now that our network is trained, loop over the XOR data points for (value, target) in zip(X, y): # make a prediction on the data point and display the result # to our console pred = network.predict(value)[0][0] step = 1 if pred > 0.5 else 0 print("[INFO] data={}, ground-truth={}, pred={:.4f}, step={}".format( value, target[0], pred, step))
from pyimagesearch.nn import NeuralNetwork from sklearn import datasets # load the MNIST dataset and apply min/max scaling to scale the # pixel intensity values to the range [0, 1] (each image is # represented by an 8 x 8 = 64-dim feature vector) print('[INFO] loading MNIST(sample) dataset') digits = datasets.load_digits() data = digits.data.astype('float') data = (data - data.min()) / (data.max() - data.min()) print(f'[INFO] samples: {data.shape[0]}, dim: {data.shape[1]}') trainX, testX, trainY, testY = train_test_split(data, digits.target, test_size=0.25) # convert the labels from integers to vectors trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) # train the neural network print('[INFO] training neurall network...') nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print(f'[INFO] {nn}') nn.fit(trainX, trainY, epochs=1000) # Evaluate network print('[INFO] evaluating network...') predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
# import the neccessary packages from pyimagesearch.nn import NeuralNetwork import numpy as np # contruct the XOR dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) # define our 2-2-1 neural network and train it nn = NeuralNetwork([2, 2, 1], alpha=0.5) nn.fit(X, y, epochs=20000) # now that our network is trained, loop over the XOR data points for (x, target) in zip(X, y): # make a prediction on the data point and display the result # to our console pred = nn.predict(x)[0][0] step = 1 if pred > 0.5 else 0 print("[INFO] data={}, ground-truth={}, pred={:.4f}, step={}".format( x, target[0], pred, step))
from pyimagesearch.nn import NeuralNetwork import numpy as np X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) # define a neural network nn = NeuralNetwork([2, 2, 1], alpha=0.5) nn.fit(X, y, epochs=6500) # print results preds = [] for (x,target) in zip(X,y): #predict result then apply threshold pred = nn.predict(x)[0][0] preds.append([pred]) #apply step function step = 1 if pred > 0.5 else 0 print("[INFO] data={}, prediction={:.7f}, ground_truth={}, step={}".format(x, pred, target[0], step)) print("[INFO] network architecture is: {}".format(nn.__repr__()))
from pyimagesearch.nn import NeuralNetwork import numpy as np import argparse ap = argparse.ArgumentParser() ap.add_argument("-e", "--epochs", default="20000", help="number of epochs to train the neural network") args = vars(ap.parse_args()) X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) nn = NeuralNetwork([2, 2, 1], alpha=0.5) nn.fit(X, y, epochs=int(args["epochs"])) for (x, target) in zip(X, y): pred = nn.predict(x)[0][ 0] # [0][0] to get the predict value from the array step = 1 if pred > 0.5 else 0 print("[INFO] data={}, ground-truth={}, pred={:.4f}, step={}".format( x, target, pred, step))