反向传播网络实现

这个主要是实现训练过程核心代码

function bpn(t1,t2)%t1 is trainingset,t2 is trainingtarget
%BP Summary of this function goes here
%   Detailed explanation goes here
w11=[1 0.78];
w12=[0.8 0.9];
w21=[1 0.6];
b=[0.7 0.6 0.5];
lr=0.6;%leraning rate
%lr=0.2;
%lr=0.05;
sumerr=0;
%err=1;
c=1;%record the number of iteration
while c>0  
    for i=1:1:130
        x11 = w11*t1(:,i);
        x12 = w12*t1(:,i);
        x1=b(1)-(w11(1)*x11 + w11(2)*x11);
        x2=b(2)-(w12(1)*x11 + w12(2)*x11);
       
        Xout(1) = fun(x1);
        Xout(2) = fun(x2);
       
        x21 = Xout*w21';
        x3=b(3)-(w21(1)*x21 + w21(2)*x21);
        f = fun(x3);
        aout(i)=f;
       
        err = t2(i)-f;
        Q3 = f*(1-f)*err;
        sumerr=sumerr+0.5*err^2;
       
       
        w21(1) = w21(1) + lr*Xout(1)*Q3;
        w21(2) = w21(2) + lr*Xout(2)*Q3;
        b(3) = b(3) - lr*Q3;
       
        Q1 = Xout(1)*(1-Xout(1))*w21(1)*Q3;
        w11(1) = w11(1) + lr*t1(1,i)*Q1;
        w11(2) = w11(2) + lr*t1(2,i)*Q1;
        b(1) = b(1) - lr*Q1;
       
        Q2 = Xout(2)*(1-Xout(2))*w21(2)*Q3;
        w12(1) = w12(1) + lr*t1(1,i)*Q2;
        w12(2) = w12(2) + lr*t1(2,i)*Q2;
        b(2) = b(2) - lr*Q2;
    end
        errset(c)=sumerr/130;
        sumerr=0;
   
    if abs(err)<0.2
        break;
    end
    c=c+1;
end
fprintf('the c:');
fprintf('%3.0f   ',c);
plot(errset);
xlabel('the number of iterations');
ylabel('the sum of the squared error');
title('Error rate analysis');
text(130,0.01,['The total iterations are ',num2str(c)]);
fprintf('the target:');
for j=1:1:130
    fprintf('%3.0f   ',t2(j));
end
fprintf('/n');
fprintf('the output:');
for k=1:1:130
    fprintf('%3.0f   ',aout(k));
end
fprintf('/n');
fprintf('the weights:');
disp(w11);
disp(w12);
disp(w21);
fprintf('the bias:');
disp(b);
 plot(aout,'r*');%to compare the target and output by ploting them respectively
 hold;
 plot(t2,'o');
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Theano 是一个 Python 的数学库,可以用来定义、优化和求解数学表达式,尤其是多维数组的运算,它的主要用途是支持深度学习,可以用它来实现神经网络反向传播算法。 下面是一个简单的代码示例,用 Theano 实现一个单隐层神经网络反向传播算法: ```python import numpy as np import theano import theano.tensor as T # 输入数据和标签 x = T.matrix('x') y = T.matrix('y') # 隐层大小 hidden_size = 100 # 初始化权重和偏置 w1 = theano.shared(np.random.randn(784, hidden_size).astype('float32'), name='w1') b1 = theano.shared(np.zeros(hidden_size).astype('float32'), name='b1') w2 = theano.shared(np.random.randn(hidden_size, 10).astype('float32'), name='w2') b2 = theano.shared(np.zeros(10).astype('float32'), name='b2') # 前向传播 hidden = T.nnet.sigmoid(T.dot(x, w1) + b1) y_pred = T.nnet.softmax(T.dot(hidden, w2) + b2) # 损失函数和梯度 loss = T.mean(T.nnet.categorical_crossentropy(y_pred, y)) gw1, gb1, gw2, gb2 = T.grad(loss, [w1, b1, w2, b2]) # 更新权重和偏置 lr = 0.1 updates = [(w1, w1 - lr * gw1), (b1, b1 - lr * gb1), (w2, w2 - lr * gw2), (b2, b2 - lr * gb2)] # 编译 Theano 函数 train_fn = theano.function(inputs=[x, y], outputs=loss, updates=updates) # 训练网络 for i in range(1000): loss = train_fn(x_train, y_train) if i % 100 == 0: print('Step {}: loss = {}'.format(i, loss)) ``` 这个示例中,我们定义了一个单隐层神经网络,使用 sigmoid 和 softmax 激活函数,用交叉熵函数作为损失函数,使用随机梯度下降法进行优化。在训练过程中,我们依次输入训练集中的每个样本,并计算损失函数和梯度,然后更新权重和偏置。最终得到一个训练好的神经网络模型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值