Last active
December 31, 2019 14:19
-
-
Save sonnyksimon/470397108d9c24aa345e2afaecda32e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| nnsigmoid | |
| ~~~~~~~~~ | |
| A simple neural network using the sigmoid | |
| function in Python. | |
| :copyright: Pancubs.org | |
| :license: MIT | |
| """ | |
| import numpy as np | |
| import math as math | |
| import random | |
| def dotproduct(inputs, weights): | |
| return sum(np.multiply(inputs, weights)) | |
| class Neuron: | |
| def __init__(self, weights, bias): | |
| self.weights = weights | |
| self.bias = bias | |
| def activate(self, inputs): | |
| return 1 / (1 + math.exp(-dotproduct(inputs, self.weights)) - self.bias) | |
| class Layers: | |
| def __init__(self, neurons): | |
| self.neurons = neurons | |
| def activate(self, inputs): | |
| return list(neuron.activate(inputs) for neuron in self.neurons) | |
| def startlayer(neuronscount, weightscount): | |
| return Layers(Neuron(np.random.rand(weightscount), random.random()) for _ in range(neuronscount)) | |
| class NeuralNetwork: | |
| def __init__(self, n, m, k): | |
| self.layers = [startlayer(n, 1), startlayer(m, n), startlayer(k, m)] | |
| def activate(self, inputs): | |
| return list(layer.activate(inputs) for layer in self.layers) | |
| def main(): | |
| NeuralNetwork(1, 2, 1) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment