Sunday, March 20, 2022

Learning neural networks 1

1 Simple forward pass of one layer neural network in Python, with numpy

 






# -*- coding: utf-8 -*-
"""
Created on Fri Mar 18 12:39:21 2022

@author: Balaji
"""

import numpy as np
num_neurons = 2;
num_inputs  = 3;
num_dataSets= 1;
print('\n Program for a simple neural network, doing a forward pass with one layer')
print('\n Each row is a data set')
inputs  = np.random.randn(num_dataSets,num_inputs);
weights = np.random.randn(num_neurons ,num_inputs);
bias    = np.random.randn(num_neurons ,1);
results = np.zeros_like(bias)

print('\n Input = ')
print(inputs)

print('\n Weights = ')
print(weights)

print('\n Bias = ')
print(bias)

for input_iter in inputs:
    for weight_iter,bias_iter,result_iter in zip(weights,bias,results):
        print('\n Input data set:')
        print(input_iter);
        print('\n is multiplied with weights')
        print(weight_iter)
        print('\n and added with bias')
        print(bias_iter)
        print('\n to give the result of neuron as')
        result_iter = input_iter.dot(weight_iter) + bias_iter;
        print(result_iter)
 
 

No comments:

Post a Comment