SolidWorks Videos

Tuesday, November 30, 2021

Introduction and Feed Forward Back Propagating Neural Network from Scratch without Keras / without TensorFlow using MATLAB Syntax

In this blog, I would try to use machine learning to predict flow fields around wings, propellers and turbines of all sorts using machine learning!

Recently, I have managed to taught myself the basics of machine learning like I taught myself Computational Fluid Dynamics (CFD) [Fluid Dynamics using the Computer] using the resources available on the internet (ME 702), all those years ago!

The little code I wrote in MATLAB is mentioned at the end. This code has one input, output and hidden layer, respectively. In the current configuration, it can predict all the logic gates successfully. To change the gate, just change the input matrices, which are marked by comments. I tried to include as many comments as I could. I would try to write code from scratch without Keras / without TensorFlow using MATLAB Syntax.

The code can be optimized more. For instance, the for loop can be replaced by while. Which I might, might not post. The code was written after I read a certain blue colored book I found called "Make your own neural network" by Tariq Rashid.

%% clear workspace and command window %%

clear; clc;

%% initialize nodes / structure of the neural network %%

inodes = 2; %input nodes

hnodes = 4; %hidden nodes

onodes = 1; %output nodes

%% initialize weights and biases %%

wih = 2*(rand(hnodes,inodes)-0.5); %random weights input to hidden -1 to 1

who = 2*(rand(onodes,hnodes)-0.5); %random weights hidden to output -1 to 1

lr = 0.5; %learning rate

bh=2*(rand(hnodes,1)-0.5); %initialize bias for hidden nodes -1 to 1

bo=2*(rand(onodes,1)-0.5); %initialize bias for hidden nodes -1 to 1

%% inputs and targets (training data) %%

inputs = [1 0; 0 1; 0 0; 1 1].'; %training data

targets = [0; 0; 0; 1].'; %training output

%% training loop %%

for i=1:100000

    hidden_inputs = wih*inputs + bh;

    hidden_outputs = 1./(1+exp(-hidden_inputs)); %hidden layer

    final_inputs = who*hidden_outputs + bo;

    final_outputs = 1./(1+exp(-final_inputs)); %output matrix

    output_errors = targets - final_outputs; %errors output to target

    hidden_errors = who.'*output_errors; %errors input to hidden

    who = who + (lr .* ((output_errors .* final_outputs  .* (1.0 -  final_outputs)) * hidden_outputs.')); %updating of weights hidden to output

    wih = wih + (lr .* ((hidden_errors .* hidden_outputs .* (1.0 - hidden_outputs)) * inputs.')); %updating of weights input to hidden

end

%% predict code %%

inputs_p = [0 1].'; %test inputs

hidden_inputs_p = wih*inputs_p + bh;

hidden_outputs_p = 1./(1+exp(-hidden_inputs_p)); %predicted hidden layer

final_inputs_p = who*hidden_outputs_p + bo;

final_outputs_p = 1./(1+exp(-final_inputs_p)); %predicted output matrix


Lets see what the future bring! If you want to collaborate on the research projects related to turbo-machinery, aerodynamics, renewable energy and well, machine learning please reach out. Thank you very much for reading.