`
#感知器
class Perceptron: def init(self, num_inputs=2, weights=[1,1]): self.num_inputs = num_inputs self.weights = weights
#加权和
def weighted_sum(self, inputs): weighted_sum = 0 for i in range(self.num_inputs): weighted_sum += self.weights[i]*inputs[i] return weighted_sum
#激活函数
def activation(self, weighted_sum): if weighted_sum >= 0: return 1 if weighted_sum < 0: return -1
#训练
def training(self, training_set): foundLine = False while not foundLine: total_error = 0 for inputs in training_set: prediction = self.activation(self.weighted_sum(inputs)) actual = training_set[inputs] error = actual - prediction total_error += abs(error)
for i in range(self.num_inputs):
self.weights[i] = self.weights[i] + (error * inputs[i])
if total_error == 0:
foundLine = True
cool_perceptron = Perceptron() small_training_set = {(0,3):1, (3,0):-1, (0,-3):-1, (-3,0):1}
print(cool_perceptron.weights) cool_perceptron.training(small_training_set) print(cool_perceptron.weights)
[1, 1] [-5, 1] `