目录
课堂笔记
代码实现1
from cgi import print_environ
from functools import reduce
import numpy as np
#定义感知器类
class perceptron(object):
#感知器初始化函数(参数个数,激活函数)
def __init__(self, input_num, activator):
self.activator = activator
self.weights = [0.0 for _ in range(input_num)] #将每个参数对应权值设为0
self.bias = 0.0 #偏置值设为0
#输出训练所得权值与偏置值
def __str__(self):
return 'weights\t:%s\nbias\t:%f\n' % (self.weights, self.bias)
#计算激活函数值
def predict(self, input_vec):
return self.activator(reduce(lambda a, b : a + b, map(lambda xw : xw[0] * xw[1], zip(input_vec, self.weights)), 0.0) + self.bias)
#更新权重与偏置值
def _update_weights(self, input_vec, output, label, rate):
delta = label - output #计算预测值与真值之差
self.weights = list(map(lambda xw : xw[1] + rate * delta * xw[0], zip(input_vec, self.weights))) #更新权重
self.bias = rate + delta #更新偏置
#迭代计算
def _one_iteration(self, input_vecs, labels, rate):
samples = zip(input_vecs, labels)
for(input_vec, label) in samples:
output = self.predict(input_vec) #计算激活函数值
self._update_weights(input_vec, output, label, rate) #更新权值与偏置值
#训练数据得权值、偏置值
def train(self, input_vecs, labels, iteration, rate):
for _ in range(iteration):
self._one_iteration(input_vecs, labels, rate)
#定义激活函数 f = lambda x: x
def f(x):
return x
#def get_training_dataset():
# input_vecs = [[0.2104,3],[0.1600,3],[0.2400,3],[0.1416,2],[0.3000,4]]
# labels = [0.400,0.330,0.369,0.232,0.540]
# return input_vecs, labels
#def get_training_dataset():
input_vecs = [[0,0],[-0.886041760444641,0],[0.520373702049255,0],[-1.20951724052429,-1.58113884925842],[1.57518529891968,1.58113884925842]]
labels = [0.257084310054779,-0.440431356430054,-0.0518154688179493,-1.41695320606232,1.65211570262909]
return input_vecs, labels
#def get_training_dataset():
input_vecs = [[0.434343457221985,0.5],[0.116161584854126,0.5],[0.621212124824524,0.5],[0,0],[1,1]]
labels = [0.545454561710358,0.318181812763214,0.444805204868317,0,1]
return input_vecs, labels
#构建训练集
def get_training_dataset():
input_vecs = [[5], [3], [8], [1.4], [10.1]] #输入向量列表,每一项是员工的工作年限
labels = [5500, 2300, 7600, 1800, 11400] # 期望的输出列表,月薪,注意要与输入一一对应
return input_vecs, labels
#构建训练集训练感知器
def train_linear_unit():
p = perceptron(1, f)
input_vecs, labels = get_training_dataset()
p.train(input_vecs, labels, 5, 0.0001) #初始设置:迭代10轮,学习率是0.01
return p
#主函数
if __name__ == '__main__':
linear_unit = train_linear_unit()
print(linear_unit)
#顺便与标记对比一下 大概看一下训练效果
print ('Work 5 years, monthly salary = %.2f' % linear_unit.predict([5]))
print ('Work 3 years, monthly salary = %.2f' % linear_unit.predict([3]))
print ('Work 8 years, monthly salary = %.2f' % linear_unit.predict([8]))
print ('Work 1.4 years, monthly salary = %.2f' % linear_unit.predict([1.4]))
print ('Work 3.4 years, monthly salary = %.2f' % linear_unit.predict([3.4]))
print ('Work 1.5 years, monthly salary = %.2f' % linear_unit.predict([1.5]))
print ('Work 15 years, monthly salary = %.2f' % linear_unit.predict([15]))
print ('Work 6.3 years, monthly salary = %.2f' % linear_unit.predict([6.3]))
#if __name__ == '__main__':
# linear_unit = train_linear_unit()
# print(linear_unit)
# print('[居住面积,房间数量] = [2104,3] 最终价格为:%.2f' % linear_unit.predict([2104,3]))
# print('[居住面积,房间数量] = [1600,3] 最终价格为:%.2f' % linear_unit.predict([1600,3]))
# print('[居住面积,房间数量] = [2400,3] 最终价格为:%.2f' % linear_unit.predict([2400,3]))
# print('[居住面积,房间数量] = [1416,2] 最终价格为:%.2f' % linear_unit.predict([1416,2]))
# print('[居住面积,房间数量] = [3000,4] 最终价格为:%.2f' % linear_unit.predict([3000,4]))
运行结果1
代码实现2
评定一个学生的综合绩点
影响该学生综合绩点的特征向量包括学业绩点、学生工作、科研成绩和实践表现,每个特征参与评定的比重不同,即参数不同,还规定有基础分,我们通过训练样本数据集得到可预测下一届学生的综合绩点。
from cgi import print_environ
from functools import reduce
import numpy as np
#定义感知器类
class perceptron(object):
#感知器初始化函数(参数个数,激活函数)
def __init__(self, input_num, activator):
self.activator = activator
self.weights = [0.0 for _ in range(input_num)] #将每个参数对应权值设为0
self.bias = 0.0 #偏置值设为0
#输出训练所得权值与偏置值
def __str__(self):
return 'weights\t:%s\nbias\t:%f\n' % (self.weights, self.bias)
#计算激活函数值
def predict(self, input_vec):
return self.activator(reduce(lambda a, b : a + b, map(lambda xw : xw[0] * xw[1], zip(input_vec, self.weights)), 0.0) + self.bias)
#更新权重与偏置值
def _update_weights(self, input_vec, output, label, rate):
delta = label - output #计算预测值与真值之差
self.weights = list(map(lambda xw : xw[1] + rate * delta * xw[0], zip(input_vec, self.weights))) #更新权重
self.bias = rate + delta #更新偏置
#迭代计算
def _one_iteration(self, input_vecs, labels, rate):
samples = zip(input_vecs, labels)
for(input_vec, label) in samples:
output = self.predict(input_vec) #计算激活函数值
self._update_weights(input_vec, output, label, rate) #更新权值与偏置值
#训练数据得权值、偏置值
def train(self, input_vecs, labels, iteration, rate):
for _ in range(iteration):
self._one_iteration(input_vecs, labels, rate)
#定义激活函数 f = lambda x: x
def f(x):
return x
#def get_training_dataset():
# input_vecs = [[0.2104,3],[0.1600,3],[0.2400,3],[0.1416,2],[0.3000,4]]
# labels = [0.400,0.330,0.369,0.232,0.540]
# return input_vecs, labels
#def get_training_dataset():
# input_vecs = [[0,0],[-0.886041760444641,0],[0.520373702049255,0],[-1.20951724052429,-1.58113884925842],[1.57518529891968,1.58113884925842]]
# labels = [0.257084310054779,-0.440431356430054,-0.0518154688179493,-1.41695320606232,1.65211570262909]
# return input_vecs, labels
#def get_training_dataset():
# input_vecs = [[0.434343457221985,0.5],[0.116161584854126,0.5],[0.621212124824524,0.5],[0,0],[1,1]]
# labels = [0.545454561710358,0.318181812763214,0.444805204868317,0,1]
# return input_vecs, labels
#构建训练集
#def get_training_dataset():
# input_vecs = [[5], [3], [8], [1.4], [10.1]] #输入向量列表,每一项是员工的工作年限
# labels = [5500, 2300, 7600, 1800, 11400] # 期望的输出列表,月薪,注意要与输入一一对应
# return input_vecs, labels
#构建训练集
def get_training_dataset():
input_vecs = [[4.5,4.7,4.6,4.8],[4.5,4.0,4.6,4.7],[4.5,4.7,4.3,4.2],[4.7,4.6,4.5,4.3],[4.9,4.0,4.0,4.1],[4.1,4.9,4.8,4.9]]
labels = [4.57,4.49,4.45,4.61,4.55,4.40]
return input_vecs, labels
#构建训练集训练感知器
def train_linear_unit():
p = perceptron(4, f)
input_vecs, labels = get_training_dataset()
p.train(input_vecs, labels, 5000, 0.1) #初始设置:迭代10轮,学习率是0.01
return p
#主函数
if __name__ == '__main__':
linear_unit = train_linear_unit()
print(linear_unit)
print('[学业绩点,学生工作,科研成绩,实践表现] = [4.5,4.7,4.6,4.8] 最终得分为:%.2f' % linear_unit.predict([4.5,4.7,4.6,4.8]))
print('[学业绩点,学生工作,科研成绩,实践表现] = [4.5,4.0,4.6,4.7] 最终得分为:%.2f' % linear_unit.predict([4.5,4.0,4.6,4.7]))
print('[学业绩点,学生工作,科研成绩,实践表现] = [4.5,4.7,4.3,4.2] 最终得分为:%.2f' % linear_unit.predict([4.5,4.7,4.3,4.2]))
print('[学业绩点,学生工作,科研成绩,实践表现] = [5.0,5.0,5.0,5.0] 最终得分为:%.2f' % linear_unit.predict([5.0,5.0,5.0,5.0]))
运行结果2
weights :[0.592273500292354,0.09907395106529637,0.196892896023883,0.09729332034785503]
bias :0.064500
[绩点,学生工作,科研成绩,实践表现] = [4.5,4.7,4.6,4.8] 最终得分为:4.57
[绩点,学生工作,科研成绩,实践表现] = [4.5,4.0,4.6,4.7] 最终得分为:4.49
[绩点,学生工作,科研成绩,实践表现] = [4.5,4.7,4.3,4.2] 最终得分为:4.45
[绩点,学生工作,科研成绩,实践表现] = [5.0,5.0,5.0,5.0] 最终得分为:4.99
小结
事实上,一个机器学习算法其实只有两部分
模型 从输入特征预测输出的那个函数
目标函数 目标函数取最小(最大)值时所对应的参数值,就是模型的参数的最优值。很多时候我们只能获得目标函数的局部最小(最大)值,因此也只能得到模型参数的局部最优值。
运用优化算法去求取目标函数的最小(最大)值。[随机]梯度{下降|上升}算法就是一个优化算法。针对同一个目标函数,不同的优化算法会推导出不同的训练规则。
其实在机器学习中,算法往往并不是关键,真正的关键之处在于选取特征。选取特征需要我们人类对问题的深刻理解,经验、以及思考。而神经网络算法的一个优势,就在于它能够自动学习到应该提取什么特征,从而使算法不再那么依赖人类,而这也是神经网络之所以吸引人的一个方面。