def main(filename='data/iris-virginica.txt'): # Load data data = read_data('%s/%s' % (filepath, filename)) X, y = data[:, :-1].astype(float), data[:, -1] class_vec = list(set(y)) K = len(class_vec) Y = pd.get_dummies(y).astype(int).as_matrix() # Define parameters n = X.shape[0] d = X.shape[1] # # # Define layer sizes print(n, d, K) layers = [d, 5, K] model = NeuralNetwork(layers=layers, num_epochs=1000, learning_rate=0.10, alpha=0.9, activation_func='sigmoid', epsilon=0.001, print_details=True) model.fit(X, Y) Y_hat = model.predict(X) accuracy = compute_acc(Y_hat, Y) print('Model training accuracy:\t%.2f' % (accuracy))
def my_solution2(X_train, X_test, y_train, y_test, hyperparams=None): if hyperparams == None: hyperparams = { 'hidden_layer_sizes': (3, ), 'learning_rate': 0.1, 'epoch': 3852, 'momentum': 0.04, 'tol': 1e-10, 'reg_coef': 0 } if not hasattr(hyperparams, 'batch_size'): hyperparams['batch_size'] = X_train.shape[0] # train with self nn = NeuralNetwork(**hyperparams) nn.fit(X_train, y_train) y_pred = nn.predict(X_test) y_pred = y_pred.argmax(axis=1) y_test = y_test.argmax(axis=1) # evaluate print('My implements2:') print(classification_report(y_test, y_pred)) return nn, y_pred, y_test
def main(filename='data/iris-virginica.txt'): # Load data data = read_data('%s/%s' % (filepath, filename)) X, y = data[:,:-1].astype(float), data[:,-1] class_vec = list(set(y)) K = len(class_vec) Y = pd.get_dummies(y).astype(int).as_matrix() # Define parameters n = X.shape[0] d = X.shape[1] # # # Define layer sizes print(n,d,K) layers = [d, 5, K] model = NeuralNetwork(layers=layers, num_epochs=1000, learning_rate=0.10, alpha=0.9, activation_func='sigmoid', epsilon=0.001, print_details=True) model.fit(X, Y) Y_hat = model.predict(X) accuracy = compute_acc(Y_hat, Y) print('Model training accuracy:\t%.2f' % (accuracy))
from NN import NeuralNetwork from Normalizer import Normalizer import numpy as np from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split cancer = load_breast_cancer() X = cancer['data'] y = cancer['target'] length = len(cancer['feature_names']) nn = NeuralNetwork([length, 30, 20, 10, 5, 1]) X_train, X_test, y_train, y_test = train_test_split(X, y) normalize = Normalizer() normalize.fit(X_train) X_train = normalize.transform(X_train) X_test = normalize.transform(X_test) nn.fit(X_train, y_train, epochs=1000, verbose=False) predictions = nn.predict(X_test) print(nn.cost(predictions, y_test))