Perceptron Logical OR ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Perceptron Logical OR ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我发现了另一个标题几乎相同的问题,但它是关于逻辑与的,而我对逻辑或有疑问。
这是我的代码:
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()
我正在尝试制作一个感知器。我不知道问题是什么,但是 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
当我将模型评估循环从 for i, _ in training_dataset:
更改为 for i in range(100):
时发生。我这样做是因为它只会向我显示 4 次输出,而我想查看程序 运行 100 次。你知道为什么会这样吗?我该如何解决?
for 循环遍历 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()
我发现了另一个标题几乎相同的问题,但它是关于逻辑与的,而我对逻辑或有疑问。
这是我的代码:
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()
我正在尝试制作一个感知器。我不知道问题是什么,但是 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
当我将模型评估循环从 for i, _ in training_dataset:
更改为 for i in range(100):
时发生。我这样做是因为它只会向我显示 4 次输出,而我想查看程序 运行 100 次。你知道为什么会这样吗?我该如何解决?
for 循环遍历 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()