I have created a basic neural network with both Tensorflow and Numpy. Finally, I had compared the results.
Author
Vraj Shah
Published
September 4, 2023
Importing Libraries
import numpy as npimport matplotlib.pyplot as pltimport tensorflow as tffrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense
WARNING:tensorflow:From C:\Users\vrajs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
Dataset
rng = np.random.default_rng(2)X = rng.random(400).reshape(-1, 2)X[:, 1] = X[:, 1] *4+11.5X[:, 0] = X[:, 0] * (285-150) +150Y = np.zeros(len(X))i =0for t, d in X: y =-3/(260-175)*t +21if (t >175and t <260and d >12and d <15and d <= y): Y[i] =1else: Y[i] =0 i +=1plt.scatter(X[Y ==1, 0], X[Y ==1, 1], s=70, marker='x', linewidth=3, c='red', label="Good")plt.scatter(X[Y ==0, 0], X[Y ==0, 1], s=50, marker='o', label="Bad")tr = np.linspace(175, 260, 50)plt.plot(tr, (-3/85) * tr +21, linewidth=1)plt.axhline(y=12, linewidth=1)plt.axvline(x=175, linewidth=1)plt.xlabel("Feature 1", size=12)plt.ylabel("Feature 2", size=12)plt.legend(loc='upper right')plt.show()
WARNING:tensorflow:From C:\Users\vrajs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.
C:\Users\vrajs\AppData\Local\Temp\ipykernel_14280\3457910700.py:5: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
p[i, 0] = my_sequential(X[i], W1, b1, W2, b2)