Esempio n. 1
0
    return ((y - y_pred)**2).mean()


def accuracy(y, y_pred):
    pred = (y_pred >= 0.5).reshape(-1)
    a = (pred == y).mean()
    p = (y[pred == 1]).mean()
    r = (pred[y == 1]).mean()
    return a, p, r


lr = 0.3
inp = 28 * 28
hidden = 500
epoch = 20
batch_size = 32

n = nn(inp, hidden, lr)

for i in range(epoch):
    for X, y in iiter(trainX, trainY, batch_size):
        n.feedforward(X)
        n.Loss(y)
        n.backprop()

    n.feedforward(trainX)
    print("train loss\t", error(trainY, n.output))
    n.feedforward(testX)
    print("test loss\t", error(testY, n.output))
    print("measures: \t", accuracy(testY, n.output))
Esempio n. 2
0
from NN import NeuralNetwork as nn
import numpy as np

n = nn(3, 2, 0.8)

n.weights1 = np.array([[0.35, 0], [0.15, -0.1], [-0.2, 0.2]])
n.weights2 = np.array([[0.4], [0.25]])
n.bias1 = np.array([0, 0]).reshape(1, 2)
n.bias2 = np.array([0]).reshape(1, 1)
n.lr = 0.8

x = np.array([0.5, 0.3, 0.9]).reshape(1, 3)

n.feedforward(x)
n.Loss(0.8)
n.backprop()

print(n.weights2)
print(n.weights1)
Esempio n. 3
0
import pandas as pd
from DMC import dmc
from KNN import knn
from NN import nn

data = pd.read_csv("iris.csv")
testSet = [[7.2, 3.6, 5.1, 2.5]]
test = pd.DataFrame(testSet)

k = 5
result1, neighbor1 = nn(data, test)
result2, neighbor2 = knn(data, test, k)
result3, neighbor3 = dmc(data, test)

print("\nResultados: ")
print("NN\n\tResults: {} - Vizinho: {}".format(result1, neighbor1))
print("KNN\n\tResults: {} - Vizinho: {}".format(result2, neighbor2))
print("DMC\n\tResults: {} - Vizinho(Centroide): {}".format(result3, neighbor3))