Reputation: 21
I've been trying to write a neural network in python and I've been stuck on this part for a while. I have this code:
import numpy as np
np.random.seed(0)
X = [[1.0, 2.0, 3.0, 2.5],
[2.0, 5.0, -1.0, 2.0],
[-1.5, 2.7, 3.3, -0.8]]
class Layer:
def __init__ (self, inputCount, neronCount):
self.weights = 0.10 * np.random.randn(inputCount, neronCount)
self.biases = np.zeros((1, neronCount))
def forward(self, inputs):
self.output = np.dot(inputs, self.weights) + self.biases
layer1 = Layer(4, 5)
layer2 = Layer(5, 2)
layer1.forward(X)
layer2.forward(layer1.output)
print(layer2.output)
and this outputs:
[[ 0.148296 -0.08397602]
[ 0.14100315 -0.01340469]
[ 0.20124979 -0.07290616]]
so I made an array Y
that I would use as an expected output so I could compare them. For testing proposes I set the expected output as the actual output so I would know that it works.
Y = np.array([[0.148296, -0.08397602],
[0.14100315, -0.01340469],
[0.20124979, -0.07290616]])
I also wanted to compare each variable so I could know how much they got wrong but when I compare them
for i in range (0, 3):
for j in range(0, 2):
if np.asarray(layer2.output[i, j], dtype=np.float32) == Y[i, j]:
print("TEST")
it outputs nothing, when it should output TEST
3 times.
Any suggestions?
whole script:
import numpy as np
np.random.seed(0)
X = [[1.0, 2.0, 3.0, 2.5],
[2.0, 5.0, -1.0, 2.0],
[-1.5, 2.7, 3.3, -0.8]]
Y = np.array([[0.148296, -0.08397602],
[0.14100315, -0.01340469],
[0.20124979, -0.07290616]])
class Layer:
def __init__ (self, inputCount, neronCount):
self.weights = 0.10 * np.random.randn(inputCount, neronCount)
self.biases = np.zeros((1, neronCount))
def forward(self, inputs):
self.output = np.dot(inputs, self.weights) + self.biases
layer1 = Layer(4, 5)
layer2 = Layer(5, 2)
layer1.forward(X)
layer2.forward(layer1.output)
print(layer2.output)
for i in range (0, 3):
for j in range(0, 2):
if np.asarray(layer2.output[i, j], dtype=np.float32) == Y[i, j]:
print("TEST")
Upvotes: 0
Views: 71
Reputation: 1349
for i in range(0, 3):
for j in range(0, 2):
if layer2.output[i][j] - Y[i][j] <= 1e-13:
print("TEST")
You should allow for a relative error value like 1e-13
because they are floating point numbers which can't be compared precisely by the ==
operator.
Upvotes: 1