Neural Network Implementation in MicroPython For TinyML - by Subir Maity - Medium
Neural Network Implementation in MicroPython For TinyML - by Subir Maity - Medium
The problem statement considered for this article is the same as the previous one i.e.
Detection of diabetes. With minor modification, it can be applied to any real-time data.
procedure:
1. Train the model with a large number of available training data in PC/Google Colab
using TensorFlow.
2. Export the weights and bias values of the trained network from TensorFlow and dump
them in a text file.
3. Develop a feed-forward network using basic python code without Numpy (because
microPython does not have Numpy)
5. Develop code from scratch for conversion of probability to a 0–1 scale with a threshold
of 0.5.
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 2/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 3/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
The above-mentioned script is used to develop and train a feed-forward network with 4
neurons in the input layer, 2 neurons in the hidden layer, and a single neuron in the output
layer. Relu activation is used for the input and hidden layer, whereas sigmoid activation is
used for the output layer.
weights = model.get_weights()
print(weights)
Output :
Export weight and bias in a text file (optional, for future use)
file.write(content)
file.close()
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 4/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
for i in range(nunit):
b[i], activation)
z = neuron(x, w[i],
# print(z)
res.append(z)
return res
tmp = zeros1d(x[0])
for i in range(len(x)):
if activation == "sigmoid":
yp = sigmoid([tmp[i] +
b for i in range(len(tmp))])
elif activation == "relu":
yp = relu([tmp[i] + b
for i in range(len(tmp))])
else:
return yp
C. Activation functions
##Relu activation
y = []
for i in range(len(x)):
if x[i] >= 0:
y.append(x[i])
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 5/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
else:
y.append(0)
# print(y)
return y
##Sigmoid function
def sigmoid(x):
import math
z = [1 / (1
+ math.exp(-x[kk])) for kk in range(len(x))]
return z
D. Classification report
TP = 0
TN = 0
FP = 0
FN = 0
for i
in range(len(ytrue)):
# For accuracy calculation
if ytrue[i] == ypred[i]:
tmp += 1
if ytrue[i]
== 0 and ypred[i] == 0: # find true negative
TN += 1
if ytrue[i]
== 0 and ypred[i] == 1: # find false positive
FP += 1
print(print_matrix(conf_matrix))
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 6/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
w2 = [[ 1.000371 , 1.5499793 ],
[-2.840075 , 0.2513094 ],
[ 0.07223273, -0.5448719 ],
[ 0.25136417, 0.60296375]]
b2 = [0.4844855 , 0.61291546]
w3 = [[1.1305474],[-1.3455248]]
b3 = [0.91277504]
w2 = transpose(w2)
w3 = transpose(w3)
print(ypred)
ypred_class
print(classification_report(ytrue,ypred_class))
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 7/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 8/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 9/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 10/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 11/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 12/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 13/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 14/15
9/19/22, 9:24 AM Neural Network Implementation in MicroPython for TinyML | by Subir Maity | Medium
Note: In the main program, the Xtest and Ytrue value is directly taken from the training
code after scaling (normalization) and splitting. The same network architecture is used i.e.
4 neurons in the input layer, 2 neurons in the hidden layer, and 1 neuron in the final output
layer.
Verify the result with Tensorflow predicted value in Google Colab (previously attached code
in Model training section):
https://medium.com/@subirmaity/a-simple-neural-network-implementation-approach-in-micropython-for-deep-learning-application-760ab35cb538 15/15