import numpy as np
from DataProcessing import getTrainingData

X, Y = getTrainingData()

M = 9
D = X.shape[1]
K = len(set(Y))

W1= np.random.randn(D,M)
b1=np.zeros(M)
W2= np.random.randn(M,K)
b2= np.zeros(K)


def softmax(a):
    expA = np.exp(a)
    return expA / expA.sum(axis=1, keepdims=True)


def frwd(X, W1, b1, W2, b2):
    Z = np.tanh(X.dot(W1) + b1)
    return softmax(Z.dot(W2) + b2)


P_Y_given_X = frwd(X,W1,b1,W2,b2)
predictions = np.argmax(P_Y_given_X, axis=1)


def classification_rate(Y,P):
    return np.mean(Y == P)
def derivative_b2(T, Y):
    return (T - Y).sum(axis=0)


def derivative_b1(T, Y, W2, Z):
    return ((T - Y).dot(W2.T) * Z * (1 - Z)).sum(axis=0)


def cost(T, Y):
    tot = T * np.log(Y)
    return tot.sum()


#Y is the output column without one-hot encoding
#T is the output column with one-hot encoding
X, T = getTrainingData()
M = 9
D = X.shape[1]
K = T.shape[1]
Y = getOutputColumn()

# randomly initialize weights

W1 = np.random.randn(D, M)
b1 = np.random.randn(M)
W2 = np.random.randn(M, K)
b2 = np.random.randn(K)
learning_rate = 1e-3
costs = []

for epoch in range(1000):
Esempio n. 3
0
import sys

sys.path.append('../ann_logistic_extra')

from DataProcessing import get_data, getTrainingData, getTestingData
from sklearn.neural_network import MLPClassifier
from sklearn.utils import shuffle

# get the data
#X,Y = get_data()
Xtrain, Ytrain = getTrainingData()
Xtest, Ytest = getTestingData()

# split into train and test
Xtrain, Ytrain = shuffle(Xtrain, Ytrain)
Xtest, Ytest = shuffle(Xtest, Ytest)

# create the neural network

model = MLPClassifier(hidden_layer_sizes=(7, ), max_iter=15000)

# train the neural network
model.fit(Xtrain, Ytrain)

# print the train and test accuracy
train_accuracy = model.score(Xtrain, Ytrain)

test_accuracy = model.score(Xtest, Ytest)

print('Sci-Kit Model')
print('Number of nodes at Hidden Layer: 8')