有关自然语言处理的深度学习知识有哪些?( 三 )


然后就可以开始学习过程了 。 通过向系统输入许多不同的样本 , 并根据神经元的输出是否是我们想要的结果来对权重进行微小的调整 。 当有足够的样本(且在正确的条件下) , 误差应该逐渐趋于零 , 系统就经过了学习 。
其中最关键的一个诀窍是 , 每个权重都是根据它对结果误差的贡献程度来进行调整 。 权重越大(对结果影响越大) , 那么该权重对给定输入的感知机输出的正确性/错误性就负有越大的责任 。
假设之前的输入example_input对应的结果是0:
>>> expected_output = 0>>> new_weights = []>>> for i, x in enumerate(example_input):...new_weights.append(weights[i] + (expected_output -\...perceptron_output) * x)?--- 例如 , 在上述的第一次计算中 , new_weight = 0.2 + (0 - 1) × 1 = ?0.8 >>> weights = np.array(new_weights)>>> example_weights?--- 初始权重[0.2, 0.12, 0.4, 0.6, 0.9]>>> weights?--- 新的权重[-0.8-0.080.30.550.7]这个处理方法将同一份训练集反复输入网络中 , 在适当的情况下 , 即使是对于之前没见过的数据 , 感知机也能做出正确的预测 。
3.有趣的逻辑学习问题上面使用了一些随机数字做例子 。 我们把这个方法应用到一个具体问题上 , 来看看如何通过仅向计算机展示一些标记样本来教它学会一个概念 。
接下来 , 我们将让计算机理解逻辑或(OR) 。 如果一个表达式的一边或另一边为真(或两边都为真) , 则逻辑或语句的结果为真 。 这个逻辑非常简单 。 对于以下这个问题 , 我们可以手动构造所有可能的样本(在现实中很少出现这种情况) , 每个样本由两个信号组成 , 其中每个信号都为真(1)或假(0) , 如代码清单5-1所示 。
代码清单5-1 逻辑或问题
>>> sample_data = http://kandian.youth.cn/index/[[0, 0],# False, False...[0, 1],# False, True...[1, 0],# True, False...[1, 1]]# True, True>>> expected_results = [0,# (False OR False) gives False...1,# (False OR True ) gives True...1,# (TrueOR False) gives True...1]# (TrueOR True ) gives True>>> activation_threshold = 0.5我们需要一些工具 , numpy可以用来做向量(数组)乘法 , random用来初始化权重:
>>> from random import random>>> import numpy as np>>> weights = np.random.random(2)/1000# Small random float 0 < w < .001>>> weights[5.62332144e-04 7.69468028e-05]这里还需要一个偏置:
>>> bias_weight = np.random.random() / 1000>>> bias_weight0.0009984699077277136然后将其传递到流水线中 , 计算得到4个样本的预测结果 , 如代码清单5-2所示 。
代码清单5-2 感知机随机预测
>>> for idx, sample in enumerate(sample_data):...input_vector = np.array(sample)...activation_level = np.dot(input_vector, weights) +\...(bias_weight * 1)...if activation_level > activation_threshold:...perceptron_output = 1...else:...perceptron_output = 0...print('Predicted {}'.format(perceptron_output))...print('Expected: {}'.format(expected_results[idx]))...print()Predicted 0Expected: 0Predicted 0Expected: 1Predicted 0Expected: 1Predicted 0Expected: 1随机的权重值对这个神经元没有多大帮助 , 我们得到1个正确、3个错误的预测结果 。 接下来我们让网络继续学习 , 并在每次迭代中不只是打印1或0 , 而是同时更新权重值 , 如代码清单5-3所示 。
代码清单5-3 感知机学习
>>> for iteration_num in range(5):...correct_answers = 0...for idx, sample in enumerate(sample_data):...input_vector = np.array(sample)...weights = np.array(weights)...activation_level = np.dot(input_vector, weights) +\...(bias_weight * 1)...if activation_level > activation_threshold:...perceptron_output = 1...else:...perceptron_output = 0...if perceptron_output == expected_results[idx]:...correct_answers += 1...new_weights = []...for i, x in enumerate(sample):?--- 这就是使用魔法的地方 。 当然还有一些更高效的方法来实现 , 不过我们还是通过循环来强调每个权重是由其输入(xi)更新的 。 如果输入数据很小或为零 , 则无论误差大小 , 该输入对该权重的影响都将会很小 。 相反 , 如果输入数据很大 , 则影响会很大...new_weights.append(weights[i] + (expected_results[idx] -\...perceptron_output) * x)...bias_weight = bias_weight + ((expected_results[idx] -\...perceptron_output) * 1)?--- 偏置权重也会随着输入一起更新...weights = np.array(new_weights)...print('{} correct answers out of 4, for iteration {}'\....format(correct_answers, iteration_num))3 correct answers out of 4, for iteration 02 correct answers out of 4, for iteration 13 correct answers out of 4, for iteration 24 correct answers out of 4, for iteration 34 correct answers out of 4, for iteration 4