近来迷上机器学习了,特别是神经网络这一段,很有科幻片的味道,这里把神经网络的内容整理成一篇博客,欢迎大家点评。
神经网络是让计算机模仿人的神经网络结构,设计出的一种算法,(简写ANN),有时候也称为连接模型(Connection Model),具体的说,就是模仿动物的神金网络行为特征,来做分布式并行信息处理的算法数学模型。这个数学模型依靠系统的复杂程度,通过调整神经网络中大量节点之间的互相连接关系,以及每一个节点的权重(也称自由参数),来达到“学习”的目的。
话不多说,先举个例子:
假设房屋中介公司需为了减少公司的运营成本,要制作一个估算房价的程序,当我们输入某个房子的各种信息的时候(比如说:房子距离城中心的距离、房子所处城市的人口数量、房子所处地段的交通拥堵情况、房子周围的中小学的数量),这个程序可以根据这些影响房价的各种因素,估算出这个房子的房价;
这里我们用JAVA语言写一段代码:
package demo_1;
import java.awt.Color;
class the_main {
// 神经网络的算法中分层依据可能是: 假设第一、二、三层是ai、bi、ci、
// 那么有 bi= weighti * fi(a1,a2,a3···an),ci= weighti *
// fi(a1,a2····an,b1,b2,b3····bn)
// 预计需要实现的功能:用二元二次方程作为数学基础, 实现房地产价格,和 与市中心的距离、交通拥堵情况、城市人口数量、周围重点中小学的数量
// 建立模型 :
// 地产价格=权重(负值)*与市中心距离的平方根+权重(负值)*交通拥堵情况+权重(正值)*(城市人口数量)的平方+权重(正值)*周围重点中小学数量的二分之三次方。
// 建立损失函数:偏差=(Yi-Y)的绝对值
// 建立优化算法:ωj = ωj - λ ∂Fj(ωj) / ∂ωj 其中λ是学习效率 learning_rate, F是误差函数, 其中偏导公式是
// (f(x+0.1)-f(x))/0.1
// 一次元的权重参数的调整方法, w=w-a(Yj(wj)-y)
double[] house_price = { 0.9, 1.5, 2.7, 4, 4.9, 5.5,7.1 };// 用来描述房地产价格,单位
double[] ex_house_price = new double[7]; // 万/平方米
double[] distans_center = { 3, 2.6, 2.3, 1.9, 1.9, 1.3, 1.1 }; // 描述与市中心的距离,
// 单位:KM
double[] traffic_jam = { 5, 4, 3, 3, 3, 2, 2 };// 描述交通拥堵指数 数值越高,堵塞越严重
double[] population = { 2, 2, 3, 3.1, 3.5, 3.9, 5 };// 描述城市人口,单位:百万
double[] education = { 1, 1, 2, 4, 3, 3, 5 };// 描述周围重点中小学的数量 单位:个
// 这里定义权重,
double wei_distans_center = 1, wei_traffic_jam = 1, wei_population = 1,
wei_education = 1;
// 学习效率
double a = 0.005;
// 这个函数用来计算每一类情况下的房地产价格,
double get_expect_house_price(int i) {
return wei_distans_center * distans_center[i]
+ wei_traffic_jam * traffic_jam[i] + wei_population
* population[i] * population[i] + wei_education
* education[i];
}
// 这个函数用来修正每一个权重参数的值,
double updata_weight(double wei, int i) {
wei -= a * (ex_house_price[i] - house_price[i]);
return wei;
}
double updata_weight2(double wei, int i) {
wei -= a * 1.2 * (ex_house_price[i] - house_price[i]);
return wei;
}
double updata_weight3(double wei, int i, int ix) {
if (ix == 1) {
wei_distans_center += 0.01;
}
if (ix == 2) {
wei_education += 0.01;
}
if (ix == 3) {
wei_population += 0.01;
}
if (ix == 4) {
wei_traffic_jam += 0.01;
}
wei -= a
* (Math.abs(get_expect_house_price(i) - house_price[i]) - Math
.abs(ex_house_price[i] - house_price[i])) / (0.01);
wei_population -= 0.01;
return wei;
}
double updata_weight4(double wei, int i) {
wei -= a * 1.4 * (ex_house_price[i] - house_price[i]);
return wei;
}
// 这个函数用来设计学习过程
void learning() {
for (int i2 = 0; i2 < 700; i2++) {
// 首先计算每一组的当前期望值:
for (int i = 0; i < 7; i++) {
ex_house_price[i] = get_expect_house_price(i);
System.out.println("第" + i + "种情况的预期房价为" + ex_house_price[i]);
}
System.out.println();
System.out.println("第" + i2 + "次迭代------------------------->");
// 开始修正参数
double a1 = 0, a2 = 0, a3 = 0, a4 = 0;
for (int i = 0; i < 7; i++) {
// 这里需要累加,然后求平均值
a1 += updata_weight(wei_distans_center, i);
a2 += updata_weight2(wei_education, i);
a3 += updata_weight3(wei_population, i, 3);
a4 += updata_weight4(wei_traffic_jam, i);
}
wei_distans_center = a1 / 7;
wei_education = a2 / 7;
wei_population = a3 / 7;
wei_traffic_jam = a4 / 7;
System.out.println("城心距离权重为" + wei_distans_center);
System.out.println("教育资源权重为" + wei_education);
System.out.println("城市人口权重为" + wei_population);
System.out.println("交通堵塞权重为" + wei_traffic_jam);
}
}
public static void main(String[] args) {
the_main a = new the_main();
a.learning();
}
}
按照神经网络的构建顺序(1.分析关系,2.构建模型,3.找出误差函数(又叫做损失函数),4.设计合适的优化算法) 写代码之前,我们要先分析一下这几者之间的关系,房价是包括1.房子距离城中心的距离、2.房子所处城市的人口数量、3.房子所处地段的交通拥堵情况、4.房子周围的中小学的数量等各种因素共同影响而决定的,那么我们就可以大致找出一个函数:房价=f(距城心距离,人口数量,交通状况,周围学校数量)
这里我们设计模型为:房价=权重1*距城心距离+权重2*人口数量*人口数量+权重3*交通状况+权重4*周围学校 ,也就是代码中的
// 这个函数用来计算每一类情况下的房地产价格,
double get_expect_house_price(int i) {
return wei_distans_center * distans_center[i]
+ wei_traffic_jam * traffic_jam[i] + wei_population
* population[i] * population[i] + wei_education
* education[i];
}
然后我们找出误差函数: 误差函数具体可以用梯度下降法来定义:
公式:
它具体的原理可以这样被理解:J(θ )是我们定义的误差函数 , θ使我们的一个参数。
表现在代码里,就是:
wei -= a* (Math.abs(get_expect_house_price(i) - house_price[i]) - Math
.abs(ex_house_price[i] - house_price[i])) / (0.01);
最后再介绍优化算法的方法:这里我们的机器学习过程,主要就是要让我们的四个参数更加精确,所以这里的优化算法也要围绕这四个参数进行。
优化算法的代码为:
// 这个函数用来修正每一个权重参数的值,
double updata_weight(double wei, int i) {
wei -= a * (ex_house_price[i] - house_price[i]);
return wei;
}
double updata_weight2(double wei, int i) {
wei -= a * 1.2 * (ex_house_price[i] - house_price[i]);
return wei;
}
double updata_weight3(double wei, int i, int ix) {
if (ix == 1) {
wei_distans_center += 0.01;
}
if (ix == 2) {
wei_education += 0.01;
}
if (ix == 3) {
wei_population += 0.01;
}
if (ix == 4) {
wei_traffic_jam += 0.01;
}
wei -= a
* (Math.abs(get_expect_house_price(i) - house_price[i]) - Math
.abs(ex_house_price[i] - house_price[i])) / (0.01);
wei_population -= 0.01;
return wei;
}
double updata_weight4(double wei, int i) {
wei -= a * 1.4 * (ex_house_price[i] - house_price[i]);
return wei;
}
最后再来介绍我们的学习过程: 迭代法: 机器学习过程中为了让程序最大效率的学习我们输入程序的素材,我们会设计方法,让程序重复学习素材(称为迭代,一般迭代次数为700到5000次)。 程序中我们设定了七组数据,让后喂给程序,程序在学习的过程中,会让参数逐渐向这些数据靠拢,这里可能有朋友要问,明明只有四个参数,为什么不直接用类似于线性代数的方法,直接求出这四个参数?这里我们要明确一点,那就是我们所收集到的数据,是有误差的,(另外现实世界中,各种互相存在关系的量也并不会完全呈线性分布,因为有很多细微的干扰因素,这些数据往往都是分布在函数曲线的周围的 如图:
所以我们这里并不能直接用现行代数的方法来求出这四个参数,我们需要借助统计学上的办法,用迭代来不断修正参数。)
“迭代法学习” 代码表示为:
// 这个函数用来设计学习过程
void learning() {
for (int i2 = 0; i2 < 700; i2++) {
// 首先计算每一组的当前期望值:
for (int i = 0; i < 7; i++) {
ex_house_price[i] = get_expect_house_price(i);
System.out.println("第" + i + "种情况的预期房价为" + ex_house_price[i]);
}
System.out.println();
System.out.println("第" + i2 + "次迭代------------------------->");
// 开始修正参数
double a1 = 0, a2 = 0, a3 = 0, a4 = 0;
for (int i = 0; i < 7; i++) {
// 这里需要累加,然后求平均值
a1 += updata_weight(wei_distans_center, i);
a2 += updata_weight2(wei_education, i);
a3 += updata_weight3(wei_population, i, 3);
a4 += updata_weight4(wei_traffic_jam, i);
}
wei_distans_center = a1 / 7;
wei_education = a2 / 7;
wei_population = a3 / 7;
wei_traffic_jam = a4 / 7;
System.out.println("城心距离权重为" + wei_distans_center);
System.out.println("教育资源权重为" + wei_education);
System.out.println("城市人口权重为" + wei_population);
System.out.println("交通堵塞权重为" + wei_traffic_jam);
}
有关神经网络的入门知识就介绍这么多啦;如果有什么问题,大家可以私戳我哦~!QQ :823811845