NeuralNetwork.py - # -*- coding: utf-8 -*" Created on Mon...

This preview shows page 1 - 2 out of 4 pages.

# -*- coding: utf-8 -*-"""Created on Mon Oct 21 20:40:07 2019@author: Bartek"""import numpy as npimport pickle#from matplotlib import pyplot as pltclass NeuralNetwork():#Input data x should be a numpy array of form np.array([ [inp1], [inp2], ...]).#Output data y should also be a numpy array of form np.array([ [out1],[out1], ...])def __init__(self, nbr_neurons_in_layers, x, y, seed):self.x = xself.y = ynp.random.seed(seed)self.v = nbr_neurons_in_layersself.nbr_layers = len(self.v)self.weights = [None]*(self.nbr_layers-1)self.biases = [None]*(self.nbr_layers-1)for i in range(self.nbr_layers - 1):self.weights[i] = np.random.randn(self.v[i], self.v[i+1]).Tself.biases[i] = np.random.randn(self.v[i+1]).Tself.copyweights = []self.copybiases = []def accuracy(self):sum = 0for i in range(0, self.x.shape[0]):r = self.propagate(self.x[i])if (np.argmax(r) == np.argmax(self.y[i])):sum +=1return sum/self.x.shape[0]def accuracyTestData(self, x_in, y_in):sum = 0digit_counts = np.zeros(10)hits_per_digit = np.zeros(10)ratios = np.zeros(10)for i in range(0, x_in.shape[0]):r = self.propagate(x_in[i])digit_counts[np.argmax(y_in[i])] += 1if (np.argmax(r) == np.argmax(y_in[i])):sum +=1hits_per_digit[np.argmax(y_in[i])] += 1for i in range(0,10):ratios[i] = hits_per_digit[i]/digit_counts[i]return sum/x_in.shape[0], ratiosdef saveparams(self):self.copyweights = [np.copy(x) for x in self.weights]self.copybiases = [np.copy(x) for x in self.biases]returndef loadparams(self):

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 4 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Three
Professor
professor_unknown
Tags
compute, matplotlib

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture