山东大学类脑计算实验 2

github
一个网页

构建训练数据(真值表)

train_data = [
    ([0, 0], 0),  # AND
    ([0, 1], 0),
    ([1, 0], 0),
    ([1, 1], 1),
]

train_data_or = [
    ([0, 0], 0),  # OR
    ([0, 1], 1),
    ([1, 0], 1),
    ([1, 1], 1),
]

train_data_not = [
    ([0], 1),  # NOT
    ([1], 0),
]

定义感知机类

import numpy as np

class Perceptron:
    def __init__(self, input_size):
        self.weights = np.random.rand(input_size + 1)  # self.weights[0]是偏执项  

    def activation(self, x):  # 激活函数
        return 1 if x >= 0 else 0

    def predict(self, inputs):  # 预测
        summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
        return self.activation(summation)

    def train(self, training_data, epochs=10, learning_rate=0.1):
        for _ in range(epochs):
            for inputs, label in training_data:
                prediction = self.predict(inputs)
                error = label - prediction  # 计算误差
                self.weights[1:] += learning_rate * error * np.array(inputs)  # 更新权重
                self.weights[0] += learning_rate * error  # 更新偏执项

构建和训练感知机

perceptron_and = Perceptron(input_size=2)
perceptron_and.train(train_data)

perceptron_or = Perceptron(input_size=2)
perceptron_or.train(train_data_or)

perceptron_not = Perceptron(input_size=1)
perceptron_not.train(train_data_not)

测试

test_data_and = [([1, 1], 'AND'), ([0, 0], 'AND')]
test_data_or = [([1, 0], 'OR'), ([0, 1], 'OR')]
test_data_not = [([1], 'NOT'), ([0], 'NOT')]

for data, operation in test_data_and:
    print(f"{operation} output: {perceptron_and.predict(data)}")

for data, operation in test_data_or:
    print(f"{operation} output: {perceptron_or.predict(data)}")

for data, operation in test_data_not:
    print(f"{operation} output: {perceptron_not.predict(data)}")

运行结果

AND output: 1
AND output: 0
OR output: 1
OR output: 1
NOT output: 0
NOT output: 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值