感知器没有正确学习
Perceptron does not learn correctly
我尝试做基本的 ML。所以这是我的 class 二进制 classificator 感知器。
class perceptron():
def __init__(self, x, y, threshold=0.5, learning_rate=0.1, max_epochs=10):
self.threshold = threshold
self.learning_rate = learning_rate
self.x = x
self.y = y
self.max_epochs = max_epochs
def initialize(self):
self.weights = np.random.rand(len(self.x[0]))
def train(self):
epoch = 0
while True:
error_count = 0
epoch += 1
for (x,y) in zip(self.x, self.y):
error_count += self.train_observation(x, y, error_count)
print('Epoch: {0} Error count: {1}'.format(epoch, error_count))
if error_count == 0:
print('Training successful')
break
if epoch >= self.max_epochs:
print('Reached max epochs')
break
def train_observation(self, x, y, error_count):
result = np.dot(x, self.weights) > self.threshold
error = y - result
if error != 0:
error_count += 1
for index, value in enumerate(x):
self.weights[index] += self.learning_rate * error * value
return error_count
def predict(self, x):
return int(np.dot(x, self.weights) > self.threshold)
我想class确定,如果列表值的总和 >=0(表示 1)或不等于(表示 0)
所以我做了 50 个 len 10 的数组,每个都有随机的 int 值 [-3, 3]:
def sum01(x):
if sum(x) >= 0:
return 1
else:
return 0
x = np.random.randint(low=-3, high=3, size=(50,10))
y = [sum01(z) for z in a]
然后我初始化并训练:
p = perceptron(x, y)
p.initialize()
p.train()
然后我查了一下,很多预测都不对,我哪里做错了?
predics = [(p.predict(i), sumab(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
print(predics)
使用小错误修复重新运行您的代码,我看到损失减少到 0 并正确输出 -
p = perceptron(x, y)
p.initialize()
p.train()
Epoch: 1 Error count: 196608
Epoch: 2 Error count: 38654836736
Epoch: 3 Error count: 268437504
Epoch: 4 Error count: 0
Training successful
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
print(predics)
[(1, 1), (0, 0), (0, 0), (0, 0), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
解决方案
您的代码需要进行一些快速更改 -
- 定义 x 和 y 时:
x = np.random.randint(low=-3, high=3, size=(50,10))
y = [sum01(z) for z in x] #CHANGE THIS TO x INSTEAD OF a
- 获取预测时:
#CHANGE sumab TO sum01
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
那应该可以了。您的完整代码变为 -
class perceptron():
def __init__(self, x, y, threshold=0.5, learning_rate=0.1, max_epochs=10):
self.threshold = threshold
self.learning_rate = learning_rate
self.x = x
self.y = y
self.max_epochs = max_epochs
def initialize(self):
self.weights = np.random.rand(len(self.x[0]))
def train(self):
epoch = 0
while True:
error_count = 0
epoch += 1
for (x,y) in zip(self.x, self.y):
error_count += self.train_observation(x, y, error_count)
print('Epoch: {0} Error count: {1}'.format(epoch, error_count))
if error_count == 0:
print('Training successful')
break
if epoch >= self.max_epochs:
print('Reached max epochs')
break
def train_observation(self, x, y, error_count):
result = np.dot(x, self.weights) > self.threshold
error = y - result
if error != 0:
error_count += 1
for index, value in enumerate(x):
self.weights[index] += self.learning_rate * error * value
return error_count
def predict(self, x):
return int(np.dot(x, self.weights) > self.threshold)
def sum01(x):
if sum(x) >= 0:
return 1
else:
return 0
x = np.random.randint(low=-3, high=3, size=(50,10))
y = [sum01(z) for z in x]
p = perceptron(x, y)
p.initialize()
p.train()
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
print(predics)
我尝试做基本的 ML。所以这是我的 class 二进制 classificator 感知器。
class perceptron():
def __init__(self, x, y, threshold=0.5, learning_rate=0.1, max_epochs=10):
self.threshold = threshold
self.learning_rate = learning_rate
self.x = x
self.y = y
self.max_epochs = max_epochs
def initialize(self):
self.weights = np.random.rand(len(self.x[0]))
def train(self):
epoch = 0
while True:
error_count = 0
epoch += 1
for (x,y) in zip(self.x, self.y):
error_count += self.train_observation(x, y, error_count)
print('Epoch: {0} Error count: {1}'.format(epoch, error_count))
if error_count == 0:
print('Training successful')
break
if epoch >= self.max_epochs:
print('Reached max epochs')
break
def train_observation(self, x, y, error_count):
result = np.dot(x, self.weights) > self.threshold
error = y - result
if error != 0:
error_count += 1
for index, value in enumerate(x):
self.weights[index] += self.learning_rate * error * value
return error_count
def predict(self, x):
return int(np.dot(x, self.weights) > self.threshold)
我想class确定,如果列表值的总和 >=0(表示 1)或不等于(表示 0) 所以我做了 50 个 len 10 的数组,每个都有随机的 int 值 [-3, 3]:
def sum01(x):
if sum(x) >= 0:
return 1
else:
return 0
x = np.random.randint(low=-3, high=3, size=(50,10))
y = [sum01(z) for z in a]
然后我初始化并训练:
p = perceptron(x, y)
p.initialize()
p.train()
然后我查了一下,很多预测都不对,我哪里做错了?
predics = [(p.predict(i), sumab(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
print(predics)
使用小错误修复重新运行您的代码,我看到损失减少到 0 并正确输出 -
p = perceptron(x, y)
p.initialize()
p.train()
Epoch: 1 Error count: 196608
Epoch: 2 Error count: 38654836736
Epoch: 3 Error count: 268437504
Epoch: 4 Error count: 0
Training successful
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
print(predics)
[(1, 1), (0, 0), (0, 0), (0, 0), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
解决方案
您的代码需要进行一些快速更改 -
- 定义 x 和 y 时:
x = np.random.randint(low=-3, high=3, size=(50,10))
y = [sum01(z) for z in x] #CHANGE THIS TO x INSTEAD OF a
- 获取预测时:
#CHANGE sumab TO sum01
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
那应该可以了。您的完整代码变为 -
class perceptron():
def __init__(self, x, y, threshold=0.5, learning_rate=0.1, max_epochs=10):
self.threshold = threshold
self.learning_rate = learning_rate
self.x = x
self.y = y
self.max_epochs = max_epochs
def initialize(self):
self.weights = np.random.rand(len(self.x[0]))
def train(self):
epoch = 0
while True:
error_count = 0
epoch += 1
for (x,y) in zip(self.x, self.y):
error_count += self.train_observation(x, y, error_count)
print('Epoch: {0} Error count: {1}'.format(epoch, error_count))
if error_count == 0:
print('Training successful')
break
if epoch >= self.max_epochs:
print('Reached max epochs')
break
def train_observation(self, x, y, error_count):
result = np.dot(x, self.weights) > self.threshold
error = y - result
if error != 0:
error_count += 1
for index, value in enumerate(x):
self.weights[index] += self.learning_rate * error * value
return error_count
def predict(self, x):
return int(np.dot(x, self.weights) > self.threshold)
def sum01(x):
if sum(x) >= 0:
return 1
else:
return 0
x = np.random.randint(low=-3, high=3, size=(50,10))
y = [sum01(z) for z in x]
p = perceptron(x, y)
p.initialize()
p.train()
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]
print(predics)