09. 用感知器实现简单逻辑运算
逻辑运算感知器
在这节课,我们要用感知器实现简单的逻辑运算。你将会为最常见的逻辑运算符创建感知器:AND(与)、OR(或) 和 NOT (非)。然后,我们将看看如何处理比较难的 XOR(异或)运算符。我们开始吧!
用感知器实现逻辑运算 - AND (“与”)
AND 和 OR 感知器

AND 感知器的权重和偏差是什么?
将权重(weight1
、weight2
)和偏差 bias
设为正确的值,以便如上所示地计算 AND 运算。
Start Quiz:
import pandas as pd
# TODO: Set weight1, weight2, and bias
weight1 = 0.0
weight2 = 0.0
bias = 0.0
# DON'T CHANGE ANYTHING BELOW
# Inputs and outputs
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [False, False, False, True]
outputs = []
# Generate and check output
for test_input, correct_output in zip(test_inputs, correct_outputs):
linear_combination = weight1 * test_input[0] + weight2 * test_input[1] + bias
output = int(linear_combination >= 0)
is_correct_string = 'Yes' if output == correct_output else 'No'
outputs.append([test_input[0], test_input[1], linear_combination, output, is_correct_string])
# Print output
num_wrong = len([output[4] for output in outputs if output[4] == 'No'])
output_frame = pd.DataFrame(outputs, columns=['Input 1', ' Input 2', ' Linear Combination', ' Activation Output', ' Is Correct'])
if not num_wrong:
print('Nice! You got it all correct.\n')
else:
print('You got {} wrong. Keep trying!\n'.format(num_wrong))
print(output_frame.to_string(index=False))
用感知器实现逻辑运算 - OR (“或”)

OR 感知器和 AND 感知器很相似。在下图中,OR 感知器和 AND 感知器的直线一样,只是直线往下移动了。你可以如何处理权重和/或偏差以实现这一效果?请使用下面的 AND 感知器来创建一个 OR 感知器。

OR 感知器测验
SOLUTION:
- 增大权重
- 减小偏差大小
用感知器实现逻辑运算 - NOT ("非”)
和我们刚刚研究的其他感知器不一样,NOT 运算仅关心一个输入。如果输入是 1
,则运算返回 0
,如果输入是 0
,则返回 1
。感知器的其他输入被忽略了。
在此测验中,你将设置权重(weight1
、weight2
)和偏差 bias
,以便对第二个输入进行 NOT 运算,并忽略第一个输入。
Start Quiz:
import pandas as pd
# TODO: Set weight1, weight2, and bias
weight1 = 0.0
weight2 = 0.0
bias = 0.0
# DON'T CHANGE ANYTHING BELOW
# Inputs and outputs
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [True, False, True, False]
outputs = []
# Generate and check output
for test_input, correct_output in zip(test_inputs, correct_outputs):
linear_combination = weight1 * test_input[0] + weight2 * test_input[1] + bias
output = int(linear_combination >= 0)
is_correct_string = 'Yes' if output == correct_output else 'No'
outputs.append([test_input[0], test_input[1], linear_combination, output, is_correct_string])
# Print output
num_wrong = len([output[4] for output in outputs if output[4] == 'No'])
output_frame = pd.DataFrame(outputs, columns=['Input 1', ' Input 2', ' Linear Combination', ' Activation Output', ' Is Correct'])
if not num_wrong:
print('Nice! You got it all correct.\n')
else:
print('You got {} wrong. Keep trying!\n'.format(num_wrong))
print(output_frame.to_string(index=False))
DL 09 XOR 感知器
用感知器实现逻辑运算 - XOR (“异或”)

测验:构建一个 XOR 多层感知器
现在我们使用 AND、NOT 和 OR 感知器构建一个多层感知器,以便创建 XOR 逻辑!
下面的神经网络包含三个感知器:A、B 和 C。最后一个 (AND) 已经提供给你了。神经网络的输入来自第一个节点。输出来自最后一个节点。
上面的多层感知器计算出 XOR。每个感知器都是 AND、OR 和 NOT 的逻辑运算。但是,感知器 A、B、C 和 D 并不表明它们的运算。在下面的测验中,请为四个感知器设置正确的运算,以便计算 XOR。

QUIZ QUESTION::
在 XOR 神经网络中为感知器设置运算。
ANSWER CHOICES:
感知器 |
运算符 |
---|---|
AND |
|
NOT |
|
OR |
SOLUTION:
感知器 |
运算符 |
---|---|
AND |
|
NOT |
|
OR |