Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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))
# 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)))
    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}')
Ejemplo n.º 5
0
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)
plt.title("larning curve of NN {} on XOR".format([2, 2, 1]))
plt.xlabel("Epoch #")
plt.ylabel("Loss")
plt.show()

## To demonstrate one hidden layer is required to separate nonlinear datasets
Ejemplo n.º 6
0
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()