Instantly share code, notes, and snippets.

# bmwant/simple_nn.py

Created November 13, 2018 15:23
Show Gist options
• Save bmwant/a3aac4b1ec506547134477a1a1f3ebf3 to your computer and use it in GitHub Desktop.
Update with predict to simple neural network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class NeuralNetwork(object): ... def predict(self, x): layer1 = sigmoid(np.dot(x, self.weights1)) output = sigmoid(np.dot(layer1, self.weights2)) return output x_seen = np.array([1, 1, 0]) print(nn.predict(x_seen)) # [0.01162975] x_unseen = np.array() print(nn.predict(x_unseen)) # [0.84968032]

### aldwinaldwin commented Apr 18, 2019

@bmwant When teaching the neural network 10 times from scratch again, predicting 4 examples will return different results.

Is it unpredictable what this small neural network is learning?

if __name__ == "__main__":
X = array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])
y = array([[0],[1],[1],[0]])

for _ in range(10):
nn = NeuralNetwork(X,y)
for i in range(15000):
nn.feedforward()
nn.backprop()
print([ round(nn.predict(a)) for a in [ np.array([0, 0, 0]), np.array([0, 1, 0]), np.array([1, 0, 0]), np.array([1, 1, 0]) ] ])
[0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0]
[0.0, 1.0, 1.0, 0.0]
[0.0, 1.0, 1.0, 0.0]
[0.0, 1.0, 1.0, 0.0]
[0.0, 0.0, 0.0, 0.0]
[0.0, 1.0, 1.0, 0.0]
[0.0, 1.0, 1.0, 0.0]
[0.0, 0.0, 0.0, 0.0]