线性感知器
上一篇博客讲解的是感知器,本次通过线性感知器引入优化算法的概念,本次使用的优化算法是随机梯度下降算法。
感受:激活函数是线性感知器的灵魂,控制者线性感知器的输出的数值范围,以及优化算法的具体实现,因为优化算法的下降梯度与激活函数的梯度有关。
线性单元的参数修改规则:
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=0∑n(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}));
}
}