A Basic Neural Net w/ Python in 16 Lines of Code

What is a neural network?

A neuron

A neuron is the structural and functional unit of a neural network. All neural networks, whether inside the brain or out in the computer, is made of neurons. A neuron can have any number of inputs: In our brain, a neuron gets the input from other neurons through nerve-like thin tubes called dendrites.

Let’s get started

First, we need to import the random module. As this is a Python standard module, there is no need to install it. This will be the only module we will import here.

from random import seed, random
def initialize_network(n_input, n_hidden, n_output):
network = []
hidden_layer = [{'weights': [random() for i in range(n_inputs+ 1)]} for i in range(n_hidden)]
network.append(hidden_layer)
output_layer = [{'weights': [random() for i in range(n_hidden + 1)]} for i in range(n_outputs)]
network.append(output_layer)
seed(1)
network = initialize_network(10, 5, 10)
for layer in network:
print(layer)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adith Raghav

Adith Raghav

I am an 11 year old boy and the founder of www.centeltech.com — teaching kids about HTML and CSS. I also have a website called https://freekode.centeltech.com.