线性感知器

线性感知器

上一篇博客讲解的是感知器,本次通过线性感知器引入优化算法的概念,本次使用的优化算法是随机梯度下降算法。

感受:激活函数是线性感知器的灵魂,控制者线性感知器的输出的数值范围,以及优化算法的具体实现,因为优化算法的下降梯度与激活函数的梯度有关。

线性单元的参数修改规则:
w n e w = w o l d + η ∑ i = 0 n ( y ( i ) − y ‾ ( i ) ) x ( i ) w_{new}=w_{old}+\eta\sum_{i=0}^n(y^{(i)}-\overline{y}^{(i)})x^{(i)} wnew=wold+ηi=0n(y(i)y(i))x(i)

面向对象编程的思想,我们可以通过继承上一章的Perceptron类,来实现简易的线性感知器。与上一章的二分类问题输出不同(只要零或者壹),本次的激活函数 f ( x ) = x f(x) = x f(x)=x就确定了输出范围限定在实数域,从而经过少量的训练数据拟合出一条线性函数。

public class LinePerceptron extends Perceptron {

    public LinePerceptron(int inputLength, double rate) {
        super(inputLength, rate);
    }

    public double activotor(double x) {
        return x;
    }

    public static void main(String[] args) {
        double[][] trainData = {{5},{3},{8},{1.4},{10.1}};
        double[] labels = {5500, 2300, 7600, 1800, 11400};
        LinePerceptron linePerceptron = new LinePerceptron(1,0.01);
        linePerceptron.train(trainData,labels,10);

        System.out.println(String.format("Work 3.4 years, monthly salary = %.2f",linePerceptron.predict(new double[]{3.4})));
        System.out.println("Work 15 years, monthly salary = " + linePerceptron.predict(new double[]{15}));
        System.out.println("Work 1.5 years, monthly salary = " + linePerceptron.predict(new double[]{1.5}));
        System.out.println("Work 6.3 years, monthly salary = " + linePerceptron.predict(new double[]{6.3}));
        System.out.println("Work 8 years, monthly salary = " + linePerceptron.predict(new double[]{8}));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来日可期1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值