用感知器实现逻辑运算 - AND (“与”)
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 21 12:01:05 2020
@author: 陨星落云
"""
import pandas as pd
# 设置权重与偏置项
weight1 = 1
weight2 = 1
bias = -1.5
# 输入与输出
test_inputs = [(0,0),(0,1),(1,0),(1,1)]
correct_outputs = [False,False,False,True]
outputs = []
# 生成并检查输出结果
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=['Input1','Input2','linear combination','Activation Output','Is Correct'])
if not num_wrong:
print('Nice! You got it all correct.\n')
else:
print('You got {} wrong. Keep tring!\n'.format(num_wrong))
print(output_frame.to_string(index=False))
Nice! You got it all correct.
Input1 Input2 linear combination Activation Output Is Correct
0 0 -1.0 0 Yes
0 1 -0.1 0 Yes
1 0 -0.1 0 Yes
1 1 0.8 1 Yes
用感知器实现逻辑运算 - OR (“或”)
OR 感知器和 AND 感知器很相似。在下图中,OR 感知器和 AND 感知器的直线一样,只是直线往下移动了。你可以如何处理权重和/或偏差以实现这一效果?请使用 AND 感知器来创建一个 OR 感知器。