{ "info": { "author": "Andrew Farley", "author_email": "amf7crazy@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# NeuralNetworkPY\nA basic, easy-to-use, neural network library built from scratch in python. Only dependency is numpy.\n\n## Examples\nI find examples are what I want when I go to a readme so I'm going to start with it. References for everything in this package can be found below.\n\n### Creating a Network to Understand XOR\n```python\n #Initialize the network with 2 inputs, 1 hidden layer with 5 neurons, 2 outputs\n nn = NeuralNetwork(2, [5], 2) \n\n #Setting up the training data\n data = []\n data.append(np.array([0, 1]))\n data.append(np.array([1, 0]))\n data.append(np.array([1, 1]))\n data.append(np.array([0, 0]))\n out = [[0, 1], [0, 1], [1, 0], [1, 0]] #The expected output for every input added above \n #(i.e. 0 xor 1 = 1 which in array form is [0, 1] because the guess function returns the index of the highest value, 1 in this case)\n\n #Train the network\n import random as rand\n for i in range(50000):\n index = rand.randint(0, 3) #randomizes which value we train with on each iteration\n nn.train(data[index], out[index])\n\n #Print the guess for every input\n print('')\n for ar in data:\n print(str(ar[0]) + \" xor \" + str(ar[1]) + \" = \" + str(nn.guess(ar)))\n```\n\n### NeuralNetwork and MNIST Example (\"The hello world! of machine learning\")\n```python\n#This may take a long time to run, 2-10 minutes, depending on the speed of your cpu\n#I know it isn't commented the best, I've been writing documentation for hours at this point lol\n\nimport NeuralNetworkPY\nimport numpy as np\nimport struct\nimport random as rand\n\n#Data obtained from http://yann.lecun.com/exdb/mnist/. These files MUST be in same directory\n\n#Function obtained from https://gist.github.com/tylerneylon/ce60e8a06e7506ac45788443f7269e40\ndef read_idx(filename):\n with open(filename, 'rb') as f:\n zero, data_type, dims = struct.unpack('>HBB', f.read(4))\n shape = tuple(struct.unpack('>I', f.read(4))[0] for d in range(dims))\n return np.frombuffer(f.read(), dtype=np.uint8).reshape(shape)\n\ndef prepImgs(raw_images):\n images = np.array([None for i in range(len(raw_images))])\n for i in range(len(raw_images)):\n images[i] = raw_images[i].flatten()\n images[i] = images[i] / 255\n return images\n\ndef createOutput(val):\n out = []\n for i in range(10):\n if (i == val):\n out.append(1.0)\n else:\n out.append(0.0)\n return np.array([out]).transpose()\n\ndef guessToNum(guess):\n max = guess[0]\n index = 0\n for i in range(len(guess)-1):\n if (guess[i+1] > max):\n max = guess[i+1]\n index = i+1\n return index\n\n#Load images and labels from files\nraw_images = read_idx('train-images.idx3-ubyte')\nlabels = read_idx('train-labels.idx1-ubyte')\n\n#Prep the images into a big array (need to normalize all values and flatten the 2D array to a 1D)\nimages = prepImgs(raw_images)\n\n#Setup network with 784 inputs (the images are 28*28 = 784), one hidden layer with 50 neurons, and 10 outputs (there are 10 possible values)\ndigitRecog = NeuralNetworkPY.NeuralNetwork(784, [50], 10)\ndigitRecog.setLearningRate(0.05)\n\nfor n in range(10): #train the network in 10 epochs\n for i in range(len(images)): #in each epoch, train the network with a random image n times where n is the number of images\n index = rand.randint(0, len(images)-1) #choose random image\n digitRecog.train(images[index], createOutput(labels[index])) #train the network for one iteration\n\n#get and setup testing data\ntest_raw_images = read_idx('t10k-images.idx3-ubyte')\ntest_images = prepImgs(test_raw_images)\ntest_labels = read_idx('t10k-labels.idx1-ubyte')\ncorrect = 0\ntotal = len(test_images)\n\n#loop through all test images, count how many the network got right\nfor i in range(len(test_images)):\n guess = digitRecog.feedForward(test_images[i])\n if (guessToNum(guess) == test_labels[i]):\n correct = correct + 1\n\n#print results\nprint(\"The Network guessed \" + str(correct) + \"/\" + str(total) + \" (\" + str(100*correct/total) + \"%) correct\")\n```\n\n\n## Object References\n### NeuralNetwork:\n - Description: The NeuralNetwork object which is what is trained and where inputs are passed in. This is what you'll want\n - Functions:\n - `init(inputNum, hiddenNum, outputNum)` (the constructor):\n - Description: Creates a NeuralNetwork object with the structure given as inputs\n - Inputs:\n - `inputNum`\n - Description: The number of inputs to the network (size of input array)\n - Type Required: int\n - `hiddenNum`\n - Description: The size of every hidden layer in the network\n - Type Required: list of int\n - `outputNum`\n - Description: The number of outputs from the network (size of output array)\n - Type Required: int\n - `feedForward(inputData)`:\n - Description: Computes the feed forwards algorithm for the network. Will propagate a given input array through every layer to produce an output array.\n - Inputs:\n - `inputData`\n - Description: The input array to be propogated through the network.\n - Type Required: 1D python list or numpy array (such as [1, 2, 3])\n - Outputs:\n - `probabilities`\n - Description: The confidence values for every possible output.\n - Type: Column vector in a numpy array. Will have shape (n, 1) where n is the number of elements.\n - `train(inputData, expectedOutput)`:\n - Description: Processes one iteration of network training, will adjust the weights and bias based on the error between the expected output and the current output for a given input.\n - Inputs:\n - `inputData`\n - Description: The input array to be trained with\n - Type Required: 1D python list or numpy array (such as [1, 2, 3])\n - `expectedOutput`\n - Description: What the given input array should produce in the network\n - Type Required: 1D python list or numpy array (such as [1, 2, 3])\n - `guess(inputData)`:\n - Description: Produces a guess using forward propagation for a given input. The guess is the highest confidence value in the output array from feedForward()\n - Inputs:\n - `inputData`\n - Description: The input array to get a guess from\n - Type Required: 1D python list or numpy array (such as [1, 2, 3])\n - Outputs:\n - `output`\n - Description: The index of the highest confidence value in the feedForward output.\n - Type: int\n - `setLearningRate(newRate)`:\n - Description: Sets the learning rate of the network\n - Inputs:\n - `newRate`\n - Description: The new learning rate\n - Type Required: float\n - `getLearningRate()`:\n - Description: Gets the learning rate of the network\n - Outputs:\n - `output`\n - Description: The learning rate of the current network.\n - Type: float\n - `save(fileName)`:\n - Description: Saves the network to a text file for later use relative to the current directory. Format is provided below.\n - Inputs:\n - `fileName`\n - Description: The name of the file (ex. \"test.txt\").\n - Type Required: str\n - `setLayers(weights, biases)`:\n - Description: Sets the given network based on a list of weights and biases.\n - Inputs:\n - `weights`\n - Description: A python list (must be the same size as self.layers) containing the new weight matricies for each layer.\n - Type Required: Python list of 2D numpy arrays\n - `biases`\n - Description: A python list (must be the same size as self.layers) containing the new bias matricies for each layer.\n - Type Required: Python list of 2D numpy arrays\n\n### Layer:\n - Description: Used by the NeuralNetwork object for every layer (the inputs not being a layer). Contains information for every neuron in the layer which includes the weight connections and biases.\n - Functions:\n - `init(inputSize, selfSize)` (the constructor):\n - Description: Creates a Layer object with the structure as given by parameters\n - Inputs:\n - `inputSize`\n - Description: The number of inputs from the previous layer. Could also be thought of as the number of neurons on the previous layer.\n - Type Required: int\n - `selfSize`\n - Description: The number of neurons on the current layer.\n - Type Required: int\n - `compute(inputData)`:\n - Description: Propagates a given array of inputs through the current layer. This involves computing the necessary weighted sums for every neuron and adding the bias. Matrix math is used.\n - Inputs:\n - `inputData`\n - Description: The input array to propagate through this layer\n - Type Required: A column vector of inputs between -1 and 1. The shape of the numpy array is (n, 1) where n is the number of elements.\n - Outputs:\n - `out`\n - Description: The output from the current layer with the given input.\n - Type: A column vector of inputs between -1 and 1. The shape of the numpy array is (n, 1) where n is the number of elements.\n - `stringify()`:\n - Description: Gives a string representation of the layer. Used for saving the network to a text file.\n - Outputs:\n - `weightsStr`\n - Description: The string of the weights matrix, stripped of newline characters.\n - Type: str\n - `biasStr`\n - Description: The string of the bias matrix, stripped of newline characters.\n - Type: str\n - `setVals(weights, bias)`:\n - Description: Sets the weights and bias of the current layer to the given inputs.\n - Inputs:\n - `weights`\n - Description: A matrix of the weights, needs to match the size of the current one.\n - Type Required: 2D numpy array\n - `bias`\n - Description: A matrix of the bias, needs to match the size of the current one.\n - Type Required: 2D numpy array\n\n## Helper Functions\n### `checkReform(data)`\n - Description: Checks an input data list or array, ensures it is a column vector, reshapes it if neccessary, returns the clean output.\n - Inputs:\n - `data`\n - Description: A list or array that is going to be propagated through the array.\n - Type Required: python list or numpy array\n - Outputs:\n - `out`\n - Description: The cleaned input, is ready for use by a NeuralNetwork object\n - Type: A column vector with the shape of the numpy array being (n, 1) where n is the number of elements.\n\n### `writeLine(file, text)`\n - Description: Writes a line to an open file object with write permissions.\n - Inputs:\n - `file`\n - Description: The file object to write to, must have write permissions.\n - Type Required: _io.TextIOWrapper object\n - `text`\n - Description: The text to write to the line\n - Type Required: str\n\n### `readLine(file)`\n - Description: Reads the next line from an open file object with read permissions evaluated as a python variable.\n - Inputs:\n - `file`\n - Description: The file object to read from, must have read permissions.\n - Type Required: _io.TextIOWrapper object\n - Outputs:\n - `output`\n - Description: Evaluates the line recieved as a python value. (i.e. the string \"array([1, 2, 3])\" would evaluate as a numpy array containing [1, 2, 3])\n - Type: depends on the string evaluated\n\n### `loadNetwork(fileName)`\n - Description: Reads a network info file with the given name and recreates a Neural Network out of it.\n - Inputs:\n - `fileName`\n - Description: The name of the saved Neural Network data file. (i.e. \"test.txt\")\n - Type Required: str\n - Outputs:\n - `out`\n - Description: A Neural Network with the same specifications as outlined by the file data.\n - Type: NeuralNetwork object\n\n\n## Save File Format\n**line** **value**\n1. inputNum \n2. hiddenNum\n3. outputNum\n4. learningRate \n5. number of layers (layerNum) \n6. layer[0] Weights \n7. layer[0] Bias \n\n......... \n\n(5+2xlayerNum-1). layer[layerNum-1] Weights \n\n(5+2xlayerNum). layer[layerNum-1] Bias \n\n\n## Dev Notes\nI am currently working on building a Convolutional Neural Network from scratch modelled after the Neural Network created here. The code that is being worked on can be found here as well.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/And1210/NeuralNetworkPY", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "NeuralNetworkPY", "package_url": "https://pypi.org/project/NeuralNetworkPY/", "platform": "", "project_url": "https://pypi.org/project/NeuralNetworkPY/", "project_urls": { "Homepage": "https://github.com/And1210/NeuralNetworkPY" }, "release_url": "https://pypi.org/project/NeuralNetworkPY/0.0.2/", "requires_dist": null, "requires_python": "", "summary": "An easy-to-use Neural Network library I'm building from scratch.", "version": "0.0.2" }, "last_serial": 4643185, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "3f6b11ed3543be56d781a67d16204087", "sha256": "ed37762ec2a7b6fdf8ec8e57e8b3d811adf22c4aaa01c5f40a05c6f8c87473a6" }, "downloads": -1, "filename": "NeuralNetworkPY-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "3f6b11ed3543be56d781a67d16204087", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5464, "upload_time": "2018-12-28T08:58:23", "url": "https://files.pythonhosted.org/packages/06/8d/a295c3cf602baca62f4ee7d4b87996102f96807e93a5f924793c46c21e60/NeuralNetworkPY-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e5552ac411b1c643e39573a7e2bb4902", "sha256": "3f429f565a83cd28c56747a80689013b7414167cd70de60523c1fcf739ccc269" }, "downloads": -1, "filename": "NeuralNetworkPY-0.0.1.tar.gz", "has_sig": false, "md5_digest": "e5552ac411b1c643e39573a7e2bb4902", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4211, "upload_time": "2018-12-28T08:58:25", "url": "https://files.pythonhosted.org/packages/8b/90/aeef5ebb067c622c472afc3e97475c36b1f654dc39f08f6419f79682b762/NeuralNetworkPY-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "6bc7463c0f35c4e0b4943a428e7e78fd", "sha256": "7dbab38c9b8db5c6bf5ccd4dd0479055a3ab232e58b5d7389f46085adbee423a" }, "downloads": -1, "filename": "NeuralNetworkPY-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "6bc7463c0f35c4e0b4943a428e7e78fd", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8901, "upload_time": "2018-12-29T04:21:39", "url": "https://files.pythonhosted.org/packages/23/22/9b45954957b47232707c6f8cd9d91ac6db0513b13422166f169f05f34b7a/NeuralNetworkPY-0.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "894bf8241f9f5806e51aaafca19e779f", "sha256": "7e7e5e2b020d46f789bfb8a870d116e3dcb325881ae7d8efa8353244a3ca16e3" }, "downloads": -1, "filename": "NeuralNetworkPY-0.0.2.tar.gz", "has_sig": false, "md5_digest": "894bf8241f9f5806e51aaafca19e779f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8202, "upload_time": "2018-12-29T04:21:41", "url": "https://files.pythonhosted.org/packages/d0/fd/548cf931ffddcc656ff23883fa2f4688a7e28a0fc5252f4943da4398b222/NeuralNetworkPY-0.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6bc7463c0f35c4e0b4943a428e7e78fd", "sha256": "7dbab38c9b8db5c6bf5ccd4dd0479055a3ab232e58b5d7389f46085adbee423a" }, "downloads": -1, "filename": "NeuralNetworkPY-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "6bc7463c0f35c4e0b4943a428e7e78fd", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8901, "upload_time": "2018-12-29T04:21:39", "url": "https://files.pythonhosted.org/packages/23/22/9b45954957b47232707c6f8cd9d91ac6db0513b13422166f169f05f34b7a/NeuralNetworkPY-0.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "894bf8241f9f5806e51aaafca19e779f", "sha256": "7e7e5e2b020d46f789bfb8a870d116e3dcb325881ae7d8efa8353244a3ca16e3" }, "downloads": -1, "filename": "NeuralNetworkPY-0.0.2.tar.gz", "has_sig": false, "md5_digest": "894bf8241f9f5806e51aaafca19e779f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8202, "upload_time": "2018-12-29T04:21:41", "url": "https://files.pythonhosted.org/packages/d0/fd/548cf931ffddcc656ff23883fa2f4688a7e28a0fc5252f4943da4398b222/NeuralNetworkPY-0.0.2.tar.gz" } ] }