0
\$\begingroup\$

There are a lot of Neural Networks Frameworks available but in order to understand how things work internally reimplementing can be a good exercise

In this case the goal is to develop a super-minimal Neural Network both in terms of dependencies and features in Python language

In terms of dependencies, let's just use NumPy

In terms of features, for now let's just focus on the inference (no training, basically let's assume the weights are pre-computed) and on a 3 layers (input layer, latent layer and output layer) fully connected architecture

The proposed solution consists of a class with - constructor, where it is possible to specify the number of neurons and the transfer function - the forward method which performs the inference


import numpy as np
import math

# The Transfer Function is the typical sigmoid 
def sigmoid(x):
  return 1 / (1 + math.exp(-x))


# The only relevant aspects of input and output vectors for NN Architecture are the shapes as they define the number of weights 
x = np.random.rand(1,4)
y = np.random.rand(1,1)


class NN: 
  def __init__(self, N, tf, x, y): 
    # Number of Neurons 
    self.N = N 

    # Transfer Function 
    self.tf = np.vectorize(tf)

    # Weights 
    self.weights_in = np.random.rand(x.shape[1], self.N)
    self.weights_out = np.random.rand(self.N, y.shape[0])

  def forward(self, x): 
    self.layer1 = self.tf(np.dot(x, self.weights_in))
    return self.tf( np.dot(self.layer1, self.weights_out) )

  def to_str(self): 
    return "Input Weights \n" + np.array2string(self.weights_in) + "\n Output Weights \n" + np.array2string(self.weights_out)


# Test 


temp = NN(5, sigmoid, x, y)

temp.forward(x)

Output (it is a random number)

array([[0.84957092]])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.