I found another question with almost the same title, but it was about logical AND, and I have a problem with logical OR.
This is my code:
from numpy import array, random, dot
from random import choice
from pylab import ylim, plot
from matplotlib import pyplot as plt
def step_function(x): return 0 if x < 0 else 1
training_dataset = [
(array([0, 1, 0.2345678]), 1),
(array([1, 0, 1]), 1),
(array([1, 1, 0]), 0),
(array([1, 1, 0.5]), 1),
]
weights = random.rand(3)
error = []
learning_rate = 0.00000001
n = 100
for j in range(n):
x, expected = choice(training_dataset)
result = dot(weights, x)
err = expected - step_function(result)
error.append(err)
weights = weights + learning_rate * err * x
for i in range(100):
result = dot(i, weights)
print("{}: {} -> {}".format(i, result, step_function(result)))
ylim([-1,1])
plot(error)
plt.show()
I was trying to make a perceptron. I don't know what the problem is, but ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
happened when I changed the model evaluation loop from for i, _ in training_dataset:
to for i in range(100):
. I did that because it would show me the output only 4 times, and I wanted to see the program run a 100 times. Do you know why this is happening? How do I solve it?
The for-loop iterated over the training_dataset, not over a range. You need to supply the dot function with a training dataset.
from numpy import array, random, dot
from random import choice
from pylab import ylim, plot
from matplotlib import pyplot as plt
def step_function(x): return 0 if x < 0 else 1
training_dataset = [
(array([0, 1, 0.2345678]), 1),
(array([1, 0, 1]), 1),
(array([1, 1, 0]), 0),
(array([1, 1, 0.5]), 1),
]
weights = random.rand(3)
error = []
learning_rate = 0.1
n = 100
for j in range(n):
x, expected = choice(training_dataset)
result = dot(weights, x)
err = expected - step_function(result)
error.append(err)
weights = weights + learning_rate * err * x
for i in range(100):
k = random.randint(len(training_dataset))
result = dot(training_dataset[k][0], weights)
print("{}: {} -> {}".format(i, result, step_function(result)))
ylim([-1,1])
plot(error)
plt.show()