最近工作需要用到深度学习,之前仅有一些python基础(也忘得差不多了),于是找到教程 零基础入门深度学习准备快速上手。第一部分是使用python训练一个感知器(即网络中的神经元)模型,原文中使用python2.7实现,改为了python3.6,未加载numpy库。
from functools import reduce
class Perception(object):
def __init__(self, input_num, activator):
'''
初始化感知器,设置输入参数的个数以及激活函数
输出公式:y = f ( w * x + b )
'''
self.activator= activator #'初始化激活函数'
self.weights = [ 0.0 for _ in range (input_num) ] #'权重和输入维度相同'
self.bias = 0.0 #'偏置b初始化为0'
def __str__(self):
#'打印学习到的权重w和偏置项b'
return 'weights\t:%s\nbias\t:%f\n' % (self.weights, self.bias)
def predict (self, input_vec):
#'输入向量x,输出感知器的计算结果'
return self.activator(
reduce (lambda a, b: a + b,
list(map(lambda x: x[0] * x[1],
list(zip(input_vec, self.weights))))
, 0.0) + self.bias)
def train (self, input_vecs, labels, iteration, rate):
for i in range(iteration):
self._one_iteration(input_vecs, labels, rate)
def _one_iteration(self, input_vecs, labels, rate):
#'label为实际值'
samples = zip(input_vecs, labels)
for (input_vec,label) in samples:
output = self.predict(input_vec) #'计算更新后的y值'
self._update_weights(input_vec, output, label, rate) #'更新权值w'
def _update_weights(self, input_vec, output, label, rate):
delta = label - output
self.weights = list(map(
lambda x: x[1] + rate * delta * x[0], #'wi = wi + wi''
list(zip (input_vec, self.weights))
))
self.bias += rate * delta
'''
一个感知器(神经元)模型
'''
def f(x):
return 1 if x > 0 else 0 #'定义激活函数为阶跃函数'
def get_training_dataset():
#'基于and真值表构建训练数据'
input_vecs = [[1,1], [0,0], [1,0],[0,1]]
labels = [1,0,0,0]
return input_vecs, labels
def train_and_perception():
#'使用and真值表训练感知器'
p = Perception (2,f) #'input_num & activator'
input_vecs, labels = get_training_dataset()
p.train (input_vecs, labels, 10, 0.1)
return p
if __name__ == '__main__':
and_perception = train_and_perception()
print (and_perception)
print ('1 and 1 = %d' % and_perception.predict([1,1]))
print ('0 and 0 = %d' % and_perception.predict([0,0]))
print ('1 and 0 = %d' % and_perception.predict([1,0]))
print ('0 and 1 = %d' % and_perception.predict([0,1]))
打印更新后的权值、偏置和输出。
weights :[0.1, 0.2]
bias :-0.200000
1 and 1 = 1
0 and 0 = 0
1 and 0 = 0
0 and 1 = 0