from nn.neuralnetwork import NeuralNetwork import numpy as np 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=20000) for (x, target) in zip(X, y): 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))
# load the MNIST dataset dataset = datasets.load_digits() # apply min/max scaling to scale the # pixel intensity values to the range [0, 1] data = dataset.data.astype("float") data = (data - data.min()) / (data.max() - data.min()) print("[INFO] samples: {}, dim: {}".format(data.shape[0],data.shape[1])) labels = dataset.target # split training: 75%, testing: 25% (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert labels as vector lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.fit_transform(testY) # train the network print("[INFO] training network ...") model = NeuralNetwork([trainX.shape[1], 32, 16, 10],alpha=0.5) print("[INFO] {}".format(model)) model.fit(trainX, trainY, epochs=1000) # evaluate network print("[INFO] evaluating network...") preds = model.predict(testX) print(classification_report(testY.argmax(axis=1), preds.argmax(axis=1)))
from nn.neuralnetwork import NeuralNetwork from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from sklearn import datasets print('[INFO] loading MNST (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) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print('[INFO] training network...') nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print(f'[INFO] {nn}') nn.fit(trainX, trainY, epochs=1000) print('[INFO] evaluating network...') predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
from nn.neuralnetwork import NeuralNetwork import numpy as np # construct the XOR dataset 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=10000000) # now our network is trained, loop over XOR datapoints for (x, target) in zip(X, y): # make 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))
required=True, help='Enter type of bitwise dataset to apply the perceptron') ap.add_argument('-e', '--epochs', type=int, default=20000, help='# of epochs to fit dataset') args = vars(ap.parse_args()) datasets = { 'and': [[0], [0], [0], [1]], 'or': [[0], [1], [1], [1]], 'xor': [[0], [1], [1], [0]] } # construct the dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array(datasets[args['dataset']]) # define our 2-2-1 neural network and train it print(f'[INFO] training Neural Network for {args["epochs"]} epochs') nn = NeuralNetwork([2, 2, 1], alpha=0.5) nn.fit(X, y, epochs=args['epochs']) print('[INFO] testing the Neural Network') for x, target in zip(X, y): # make a prediciton on the data point and display the result # to our console pred = nn.predict(x) step = 1 if pred > 0.5 else 0 print(f'[INFO] data={x}, ground-truth={target[0]}, pred={pred[0][0]:.4f}, step={step}')
# import the necessary packages from nn.neuralnetwork import NeuralNetwork import numpy as np import matplotlib.pyplot as plt # construct the XOR dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) epochs = 20000 # define our 2-2-1 neural network and train it nn = NeuralNetwork([2, 1], alpha=0.5) losses = nn.fit(X, y, epochs=epochs) # evaluate our model print("[INFO] evaluating...") # 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)) # construct a figure that plots the loss over time plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, len(losses)), losses) plt.title("Training Loss") plt.xlabel("Epoch #")
print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1])) # construct the training and testing splits (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 network print("[INFO] training network...") nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print("[INFO] {}".format(nn)) losses = nn.fit(trainX, trainY, epochs=1000) # evaluate the network print("[INFO] evaluating network...") predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions)) # construct a figure that plots the loss over time plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, len(losses)), losses) plt.title("Training Loss") plt.xlabel("Epoch #") plt.ylabel("Loss") plt.show()
from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from sklearn import datasets print("[INFO] loading MNIST dataset..") digits = datasets.load_digits() data = digits.data.astype("float") # apply normalization over the data using min/max scaling to range of [0,1] data = (data - data.min()) / (data.max() - data.min()) print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1])) # split test/train (trainX, testX, trainY, testY) = train_test_split(data, digits.target, test_size=0.25) # transforms the shape of the array of labels that are benefical for our network trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print("[INFO] training network..") # define the architecture of the neural network nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print("[INFO] {}".format(nn)) nn.fit(trainX, trainY) print("[INFO] evaluation network") predictions = nn.predict(testX) predictions = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
## import packages import os import sys sys.path.append("./nn") from nn.neuralnetwork import NeuralNetwork import numpy as np import matplotlib.pyplot as plt # create XOR datasets X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) Epochs = 20000 nn = NeuralNetwork([2, 2, 1], alpha=0.8) displayLoss = nn.fit(X, y, epochs=Epochs) # predict X print("[INFO] Predicting on XOR...") for (x, target) in zip(X, y): pred = nn.predict(x)[0][0] # because p is 2d array # encode in to 1 or 0 pred_label = 1 if pred > 0.5 else 0 print("[INFO] data={}, gt={}, pred={}, pred_label={}".format( x, target, pred, pred_label)) # plot learning curve plt.figure() plt.plot(np.arange(0, Epochs + 100, 100), displayLoss)
print("[INFO] samples = {}, dim = {}".format(data.shape[0], data.shape[1])) ## split datasets 75%-25% trainX, testX, trainY, testY = train_test_split(data, digits.target, test_size=0.25) # One-hot encoding targets trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) ## train the model print("[INFO] training network....") nn = NeuralNetwork([trainX.shape[1], 32, 16, 16, 10], alpha=0.5) print("[INFO] {}".format(nn)) displayLoss = nn.fit(trainX, trainY, epochs=1000) ## test print("[INFO] evaluating...") pred_probs = nn.predict(testX) pred_labels = pred_probs.argmax(axis=1) print(classification_report(pred_labels, testY.argmax(axis=1))) # plot learning curve plt.figure() plt.plot(np.arange(0, 1100, 100), displayLoss) plt.title("loss of on MNIST samples".format(nn)) plt.xlabel("epoch #") plt.ylabel("loss") plt.show()