1.逻辑运算“AND”、“OR”原理解释
(1)感知器实现逻辑运算 - AND (“与”)
(2)感知器实现逻辑运算 - OR (“或”)
(3)实现“与”运算改成“或”运算可以增大权重或减小偏差
2.逻辑运算“AND”、“OR”、“NOT”代码实现
import pandas as pd
# TODO: Set weight1, weight2, and bias
weight1 = 1
weight2 = 1
bias =-1.2
# AND:1、1、-1.2 OR:1、1、-0.2 NOT:0、-0.5、0
test_inputs = [(0, 0), (0, 1), (1, 0), (1, 1)]
correct_outputs = [False, False, False, True]
outputs = []
# 生成并检查输出
# zip()函数将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,返回由这些元组组成的列表 int()取整
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])
# 打印输出
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))
3.逻辑运算“XOR” 原理解释
实现“XOR”运算需要多层感知器,这里A为AND,B为OR,C为NOT。这样多层感知器就构成了神经网络。具体代码实现稍后放出~~