感知器 0与1的识别

w = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];


x1 = [0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0];%"1" pattern #1 
x2 = [0 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 1 0];%"1" pattern #2 
x3 = [0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0];%"1" pattern #3 
x4 = [0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0];%"1" pattern #4 
x5 = [0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0];%"1" pattern #5 
x6 = [0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0];%"1" pattern #6 
xone = [x1; x2; x3; x4; x5; x6];


z1 = [0 1 1 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 1 0];%"0" pattern #1 
z2 = [0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 1 0 0];%"0" pattern #2 
z3 = [0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 1 0];%"0" pattern #3 
z4 = [0 0 1 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 0 1 1 0 0];%"0" pattern #4 
z5 = [0 1 1 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 1 1 1 0];%"0" pattern #5 
z6 = [1 1 1 1 1 1 0 0 0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 1];%"0" pattern #6 
xzero = [z1; z2; z3; z4; z5; z6];


correct=0;
wrong=0;


d = [1 1 1 1 -1 -1 -1 -1];


b = 0; 
alpha = 0.1;


% randomly sort the number from 1-6, choose the first 4 sets as the
% training sets and the last 2 sets as the test sets.
pick_index=randperm(6); 
train_index=pick_index(1:4);
test_index=pick_index(5:6);
train=[xone(train_index,:);xzero(train_index,:)];
test=[xone(test_index,:);xzero(test_index,:)];


%learning
for item=1:100
 
    for i=1:8
        y = sign(train(i,:) * w' + b);
        e = d(i) - y;
        w = w + alpha * e * train(i,:);
        b = b + alpha * e;
   end
end


% testing
for j=1:4
    ytest(j,1) = sign(test(j,:) * w' + b);
end
if ytest(1,1)==1
    correct=correct+1;
else
    wrong=wrong+1;
end
if ytest(2,1)==1
    correct=correct+1;
else
    wrong=wrong+1;
end
if ytest(3,1)==-1
    correct=correct+1;
else
    wrong=wrong+1;
end
if ytest(4,1)==-1
    correct=correct+1;
else
    wrong=wrong+1;
end


 errorrate=wrong/(wrong+correct);   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
感知器是一种二元分类器,它可以用来解决手写数字识别的问题。在 Matlab 中,我们可以使用 Neural Network Toolbox 中的 perceptron 函数来实现感知器模型。以下是实现手写数字识别的步骤: 1. 准备数据集:使用 MNIST 数据集,其中包含了 60000 个训练样本和 10000 个测试样本。 2. 数据预处理:将每个图像转换为向量形式,并对像素值进行标准化处理。 3. 构建感知器模型:定义感知器的输入层和输出层,设置学习速度和迭代次数等参数。 4. 训练模型:使用训练数据集来训练感知器模型。 5. 测试模型:使用测试数据集来测试感知器模型的性能。 以下是一个简单的实现过程: ``` load mnist_train.mat load mnist_test.mat % 数据预处理 Xtrain = double(reshape(Xtrain, [size(Xtrain, 1), 28*28])) / 255; Xtest = double(reshape(Xtest, [size(Xtest, 1), 28*28])) / 255; % 定义感知器模型 net = perceptron; net = configure(net, Xtrain', ytrain'); % 设置学习速度和迭代次数 net.trainParam.lr = 0.1; net.trainParam.epochs = 100; % 训练模型 net = train(net, Xtrain', ytrain'); % 测试模型 ytest_pred = sim(net, Xtest'); accuracy = sum(ytest_pred == ytest') / length(ytest); ``` 在上面的示例中,我们使用 MNIST 数据集来训练和测试感知器模型。首先将数据预处理为向量形式,并进行标准化处理。然后定义感知器模型,并设置学习速度和迭代次数等参数。接着使用训练数据集来训练模型,并使用测试数据集来测试模型的性能。最后计算准确率,以评估模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值